注意转置的时候,只对上三角或者下三角操作即可,如果对全部矩阵每一个元素都换一次,则操作回原矩阵,输出就会没区别
#include <iostream> using namespace std; #define N 100 int main(){ int n; while (cin >>n && n != 0){ int a[N][N]; int i,j; //输入 for (i = 0; i < n;i++){ for (j = 0; j < n; j++){ cin >>a[i][j]; } } //转置 for (i = 0; i < n;i++){ for (j = i; j < n; j++){ int temp = a[i][j]; a[i][j] = a[j][i]; a[j][i] = temp; } } //输出 for (i = 0; i < n;i++){ for (j = 0; j < n; j++){ cout <<a[i][j]<<" "; } cout <<endl; } } }