1 条题解

  • 1
    @ 2023-4-10 12:46:01
    # include <bits/stdc++.h>
    using namespace std;
    int a[45][45];
    int main() {
    	int N;
    	cin >> N;
    	int k = 1;
    	int x = 1;
    	int y = (N + 1) / 2;
    	a[x][y] = k;
    	k++;
    	while (k <= N * N) {
    		if (x == 1 && y != N) {
    			x = N;
    			y += 1;
    			a[x][y] = k;
    			k++;
    		} else if (y == N && x != 1) {
    			y = 1;
    			x -= 1;
    			a[x][y] = k;
    			k++;
    		} else if (x == 1 && y == N) {
    			x += 1;
    			a[x][y] = k;
    			k++;
    		} else {
    			if (a[x - 1][y + 1] == 0) {
    				x -= 1;
    				y += 1;
    				a[x][y] = k;
    				k++;
    			} else {
    				x += 1;
    				a[x][y] = k;
    				k++;
    			}
    		}
    	}
    	for (int i = 1; i <= N; i++) {
    		for (int j = 1; j <= N; j++) {
    			printf("%d ", a[i][j]);
    		}
    		printf("\n");
    	}
    	return 0;
    }
    
    • 1

    信息

    ID
    464
    时间
    1000ms
    内存
    128MiB
    难度
    10
    标签
    递交数
    1
    已通过
    1
    上传者