求详细帮分析这段代码的算法
#include <iostream>#include <iomanip>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
const int row = 4;
int temp;
int i,j;
int sz = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
cout<<"初始状态"<<endl;
for(i = 0;i<row;i++)
{
for(j = 0;j < row;j++)
{
cout<<setw(4)<<sz;
}
cout<<endl;
}
for(i = 0;i < row;i++)
{
for(j = i + 1;j < row; j++)
{
temp = sz;
sz = sz;
sz = temp;
}
}
cout<<"翻转后的状态:"<<endl;
for(i = 0;i < row; i++)
{
for(j = 0;j < row; j++)
{
cout<<setw(4)<<sz;
}
cout<<endl;
}
return 0;
}
我刚学,遇这道题都把我看蒙了,谁帮着一步步分析下。 本帖最后由 susan1121 于 2020-2-24 22:57 编辑
[ 1,2,3,4]
[ 5,6,7,8]
[ 9, 10,11,12]
这个程序应该是将这个方阵转置,如果你学过线性代数的话
结果应该是
[ 1,5,9,13]
[ 2,6,10,8]
[ 3,7,11,15]
[ 4, 14, 12,16]
不知道结果对不对
我没有学过C++
有些语句我不太明白比如cout<<setw(4)里面的<<是什么意思,但是我大概能看懂整个程序要干什么 susan1121 发表于 2020-2-24 22:54
[ 1,2,3,4]
[ 5,6,7,8]
[ 9, 10,11,12]
setw相当于键盘上的tab键,让格式对齐用的,主要是这段算法。for的我有点晕。 〃忝書γě渎ぐ 发表于 2020-2-24 23:50
setw相当于键盘上的tab键,让格式对齐用的,主要是这段算法。for的我有点晕。
运行的话,结果和我说的一样吗
页:
[1]