写一个函数,使给定的一个二维数组(3×3)转置,即行列互换

写一个函数,使给定的一个二维数组(3×3)转置,即行列互换

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 163  Solved: 120
[Submit][Status][Web Board]

Description

写一个函数,使给定的一个二维数组(3×3)转置,即行列互换。

Input

一个3x3的矩阵

Output

转置后的矩阵

Sample Input

1 2 34 5 67 8 9

Sample Output

1 4 7 2 5 8 3 6 9
#include<iostream>
using namespace std;
void zhuangzhi(int a[3][3])
{int m,n,i;
for(m=0;m<2;m++)
 for(i=0;i<3;i++)
 {if(m!=1||i!=0)
 {n=a[i][m];
 a[i][m]=a[m][i];
 a[m][i]=n;
 }
 }
}
int main()

{

  int a[3][3];

  int i,j;

  for(i=0; i<3; i++)

    for(j=0; j<3; j++)

     cin>>a[i][j];

  zhuangzhi(a);

  for(i=0; i<3; i++)

  {

    for(j=0; j<3; j++)

      cout<<a[i][j]<<" ";

    cout<<endl;

  }

  return 0;

}


原文地址:https://www.cnblogs.com/oversea201405/p/3766963.html