求一个三行三列的转置矩阵。
输入描述:
第一行一个整数n<20,表示有n组测试数据,下面是n组数据; 每组测试数据是九个整型数(每个数都不大于10000),分别为矩阵的的每项;
输出描述:
每组测试数据的转置矩阵; 请在每组输出之后加一个换行
样例输入:
复制
2 1 2 3 4 5 6 7 8 9 2 3 4 5 6 7 8 9 1
样例输出:
1 4 7 2 5 8 3 6 9 2 5 8 3 6 9 4 7 1
自己的:
#include<iostream>
using namespace std;
int ju[9];
int main(){
int n,j;
cin>>n;
while(n--){
for(int i=0;i<9;i++){
cin>>ju[i];
}
for(int i=0;i<3;i++){
for(j=i;j<6;j+=3){
cout<<ju[j]<<" ";
}
cout<<ju[j]<<endl;
}
cout<<endl;
}
}
优化的:
#include<iostream>
using namespace std;
int main()
{
int num,a,b,c,d,e,f,g,h,i;
cin>>num;
while(num--)
{
cin>>a>>b>>c>>d>>e>>f>>g>>h>>i;
cout<<a<<" "<<d<<" "<<g<<endl<<b<<" "<<e<<" "<<h<<endl<<c<<" "<<f<<" "<<i<<endl<<endl;
}
}