为啥全红?
查看原帖
为啥全红?
1483980
youtao233楼主2024/11/19 14:51
#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;
}
2024/11/19 14:51
加载中...