分奇偶来写

#include<iostream>

#include<cstdio>

using namespace std;

const int N = 1010;

int arr[N][N];

int main()

{

int n;

cin >> n;

int l = 1;

int x = 1, y = 1;

if (n % 2 == 1) {

while (l <= n * n)

{

if (y == n && n % 2 == 0 && x % 2 == 1) { arr[x][y] = l++; x++; continue; }

if (y == n && n % 2 == 0 && x % 2 == 0) { arr[x][y] = l++; x++, y--; continue; }

if (y == n && n % 2 == 1 && x % 2 == 1) { arr[x][y] = l++; y--, x++; continue; }

if (y == n && n % 2 == 1 && x % 2 == 0) { arr[x][y] = l++; x++; continue; }

if (x == n && n % 2 == 1 && y % 2 == 1) { arr[x][y] = l++; y++; continue; }

if (x == n && n % 2 == 1 && y % 2 == 0) { arr[x][y] = l++; x--, y++; continue; }

if (x % 2 == 1 && y == 1) { arr[x][y] = l++; x++; continue; }

if (x % 2 == 0 && y == 1) { arr[x][y] = l++; y++, x--; continue; }

if (y % 2 == 0 && x == 1) { arr[x][y] = l++; y++; continue; }

if (y % 2 == 1 && x == 1) { arr[x][y] = l++; y--, x++; continue; }

if ((x + y) % 2 == 1) { arr[x][y] = l++; x--, y++; continue; }

if ((x + y) % 2 == 0) { arr[x][y] = l++; x++, y--; continue; }

}

}

else {

while (l <= n * n)

{

if (y == n && n % 2 == 0 && x % 2 == 1) { arr[x][y] = l++; x++; continue; }

if (y == n && n % 2 == 0 && x % 2 == 0) { arr[x][y] = l++; x++, y--; continue; }

if (y == n && n % 2 == 1 && x % 2 == 1) { arr[x][y] = l++; y--, x++; continue; }

if (y == n && n % 2 == 1 && x % 2 == 0) { arr[x][y] = l++; x++; continue; }

if (x == n && y % 2 == 1) { arr[x][y] = l++; x--, y++; continue; }

if (x == n && y % 2 == 0) { arr[x][y] = l++; y++; continue; }

if (x % 2 == 1 && y == 1) { arr[x][y] = l++; x++; continue; }

if (x % 2 == 0 && y == 1) { arr[x][y] = l++; y++, x--; continue; }

if (y % 2 == 0 && x == 1) { arr[x][y] = l++; y++; continue; }

if (y % 2 == 1 && x == 1) { arr[x][y] = l++; y--, x++; continue; }

if ((x + y) % 2 == 1) { arr[x][y] = l++; x--, y++; continue; }

if ((x + y) % 2 == 0) { arr[x][y] = l++; x++, y--; continue; }

}

}

for (int i = 1; i <= n; i++)

{

for (int j = 1; j <= n; j++)

printf("%d ", arr[j][i]);

puts("");

}

return 0;

}

全部评论

相关推荐

有没有友友知道hr面会问什么我应该反问什么?还有如何防止hr套话啊?还有应该如果催hr推进快一点#字节#OPPO#hr面
牛客989988346号:职业规划,优缺点,为什么选择这个岗,对应聘公司产品的了解和满意度,如果让你改进公司产品你会怎么做,对ai(新技术)的了解,有无其他offer,什么时候能到岗
投递OPPO等公司7个岗位 >
点赞 评论 收藏
分享
AAA专业长城贴瓷砖刘大爷:这样的简历我会直接丢进垃圾桶,花里胡哨的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务