社区讨论
为啥全红?
P5731【深基5.习6】蛇形方阵参与者 3已保存回复 2
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 2 条
- 当前快照
- 1 份
- 快照标识符
- @m3o3ktwt
- 此快照首次捕获于
- 2024/11/19 14:52 去年
- 此快照最后确认于
- 2025/11/04 14:25 4 个月前
CPP
#include<iostream>
using namespace std;
// 1 2 3
// 8 9 4
// 7 6 5
int main(){
int k=1;
int n;
cout<<"输入"<<endl;
cin>>n;
int arr[n][n];
int left=0; int right=n-1; int top=0; int bottom=n-1;
while(k<=n*n&&left<=right&&top<=bottom){
for(int i=left;i<=right;i++){
arr[top][i]=k++;
}
for(int i=top+1;i<=bottom;i++){
arr[i][right]=k++;
}
for(int i=right-1;i>=left;i--){
arr[bottom][i]=k++;
}
for(int i=bottom-1;i>top;i--){
arr[i][left]=k++;
}
left++;
top++;
right--;
bottom--;
}
int m=0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cout<<arr[i][j]<<" ";
m++;
}
cout<<endl;
}
return 0;
}
回复
共 2 条回复,欢迎继续交流。
正在加载回复...