蛇形填数(二)
题目描述
给出 n×n 的方格,按照如下方式进行填数,并输出
输入
一个整数 n, n<40
输出
n 行 n 列,每行数字间用空格间隔
样例
输入数据 1
4
输出数据 1
1 2 6 7
3 5 8 13
4 9 12 14
10 11 15 16
限制
1s, 512MB 每组测试数据
#include<iostream>
using namespace std;
int main(){
int n;
int k=1;
int a[1000][1000];
cin>>n;
for(int i=0;i<=2*n-2;i++)
{
for(int j=i;j>=0;j--)
{
if(j<n&&i-j<n)
{
if(i%2!=0)
a[i-j][j]=j++;
else
a[j][i-j]=j++;
}
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cout<<a[i][j];
}
cout<<endl;
}
}
Time Exceeded 2037.463835ms 4164KiB
foo.cc: In function 'int main()':
foo.cc:15:17: warning: operation on 'j' may be undefined [-Wsequence-point]
15 | a[i-j][j]=j++;
| ^
foo.cc:15:17: warning: operation on 'j' may be undefined [-Wsequence-point]
foo.cc:15:17: warning: operation on 'j' may be undefined [-Wsequence-point]
foo.cc:15:17: warning: operation on 'j' may be undefined [-Wsequence-point]
foo.cc:17:32: warning: operation on 'j' may be undefined [-Wsequence-point]
17 | a[j][i-j]=j++;
| ^
foo.cc:17:32: warning: operation on 'j' may be undefined [-Wsequence-point]
foo.cc:17:32: warning: operation on 'j' may be undefined [-Wsequence-point]
foo.cc:17:32: warning: operation on 'j' may be undefined [-Wsequence-point]
foo.cc:5:6: warning: unused variable 'k' [-Wunused-variable]
5 | int k=1;
| ^
试过去查资料
请各位帮帮忙