Square Matrix II (simulate multiple methods!)

Input an integer N and output a two-dimensional array of order N.

Refer to the example for the format of the array.

Input format The
input contains multiple lines, and each line contains an integer N.

When the input line N=0, it means the input is over, and the line does not need to be processed.

Output format
For each input integer N, output a two-dimensional array of order N that meets the requirements.

Each array occupies N rows, and each row contains N integers separated by spaces.

After each array is output, a blank line is output.

数据范围
0≤N≤100
输入样例:
1
2
3
4
5
0
输出样例:
1

1 2
2 1

1 2 3
2 1 2
3 2 1

1 2 3 4
2 1 2 3
3 2 1 2
4 3 2 1

1 2 3 4 5
2 1 2 3 4
3 2 1 2 3
4 3 2 1 2
5 4 3 2 1
#include<bits/stdc++.h>
using namespace std;
int main(){
    
    
    int n;
    while(cin>>n,n){
    
    
        vector<vector<int> >a(n+1,vector<int>(n+1));
         for(int i=1;i<=n;i++){
    
    
            for(int j=i,k=1;j<=n;j++,k++){
    
    //循环次数
                a[i][j]=k; //横着填
                a[j][i]=k;//数着填
            }
        }
        for(int i=1;i<=n;i++){
    
    
         for(int j=1;j<=n;j++){
    
    
          cout<<a[i][j]<<' ';
         }
          cout<<endl;
       }
       cout<<endl;
    }
    return 0;
}
#include<bits/stdc++.h>
using namespace std;
int main(){
    
    
    int n;
    while(cin>>n,n){
    
    
         for(int i=1;i<=n;i++){
    
    
            for(int j=i;j>=1;j--) cout<<j<<' ';
              for(int k=1,j=2;k<=n-i;k++,j++) cout<<j<<' ';
              cout<<endl;
          }
         cout<<endl;
        }
    return 0;
}
#include<bits/stdc++.h>
using namespace std;
int main(){
    
    
    int n;
    while(cin>>n,n){
    
    
         for(int i=1;i<=n;i++){
    
    
            for(int j=1;j<=n;j++) 
              cout<<abs(i-j)+1<<' ';
              cout<<endl;
          }
         cout<<endl;
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_43738331/article/details/112981998