用JAVA程序写出一个3*3矩阵的转置

2024-11-30 03:28:47
推荐回答(2个)
回答1:

/**
* @author 熙熙
* @2011-4-20
*/
public class JuZhenZhuanZhi {

/**
* @param args
*/
public static void main(String[] args) {
int a[][]={
{11,12,13}
,{21,22,23}
,{31,32,33}
};
System.out.println("转置前的矩阵:");
print(a);
zhuanZhi(a);
System.out.println("转置后的矩阵:");
print(a);
}

public static void zhuanZhi(int a[][]){
for(int i=0;i for(int j=i+1;j int tem = a[i][j];
a[i][j] = a[j][i];
a[j][i] = tem;
}
}
}

public static void print(int a[][]){
for(int i=0;i int j=0;
for(;j System.out.print(a[i][j]+",");
}
System.out.println(a[i][j]);
}
}

}

回答2:

public int[][] Transe(int[][] a){
int res[][] = new int[3][3];
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
res[i][j] = a[j][i];
}
}

return res;
}