螺旋方阵,输入1 5;输出结果应为1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9

为啥我 输出的这个?貌似是数组自增自减运算符有问题,求解。

#include <iostream>
#include <iomanip>
using namespace std;
void create(int a[][20],int num, int n)
{ int endnum = n*n + num -1;
// a[0][0] == num;
for(int i = 0,j = 0; num <endnum ; i++)
{
do a[i][j++] = num++; while(i+j != n-1);
do a[i++][j] = num++; while (i != j );
do a[i][j--] = num++; while (i+j != n-1);
do a[--i][j] = num++; while (i!= j);
}
//if( n%2 == 0) a[i][j] = num;
}
int main()
{
int a[20][20],startnum,n;
cout<<"Please input startnum and n:";
cin>>startnum>>n;
create(a,startnum,n);
for(int i = 0; i<n; i++)
{
for(int j=0; j<n; j++)
cout<<setw(8)<<a[i][j];
cout<<endl;
}
return 0;
}
输出应为
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9

螺旋方阵,输入1 5;输出结果应为1 2 3 4 5 16 ...
温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-06-19
不对劲……

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网