分奇偶来写

#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;

}

全部评论

相关推荐

我已成为0offer的糕手:别惯着,胆子都是练出来的,这里认怂了,那以后被裁应届被拖工资还敢抗争?
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务