题解

陈未一  •  3个月前


include <bits/stdc++.h>

using namespace std; int main(){

int n;
cin >> n;
int a[n + 1][n + 1];
int t = 1;
memset(a, 0, sizeof(a));
for(int i = n * 2 + 1; i >= 1; i--){
	int y = i, x = 1;
	while(y >= 1){
		if(x <= n && y <= n){
			if(i % 2 != 0){
				a[x][y] = t++;
			}
			else{
				a[y][x] = t++;
			}
		}
		y--;
		x++;
	}
}
for(int i = 1; i <= n / 2; i++){
	for(int j = 1; j <= n; j++){
		swap(a[i][j], a[n - i + 1][j]);
	}
}
for(int i = 1; i <= n; i++){
	for(int j = 1; j <= n; j++){
		cout << setw(5) << a[i][j];
	}
	cout << endl;
}
return 0;

}


评论: