1 条题解

  • 0
    @ 2023-6-11 12:16:28

    C :

    #include <stdio.h>
    int main()
    {
    int n,i,j;
    
    scanf("%d" , &n );
    for( i=0;i<n ; i++ )
    {
    for( j=0;j<i; j++ )
    printf("%5c" , ' ' );
    for( j=1;j<=n-i; j++ )
    printf("%5d" , j );
    printf("\n");
    }
    return 0;
    } 
    

    C++ :

    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    int main(){
    	int a[10][10] = {0};
    	int i,j,n;
    	cin>>n;
    	
    	for (i = 0;i < n;i++){
    		for (j = 0;j < n - i;j++){
    			a[i][j + i] = j + 1;
    		}
    	}
    	
    	for (i = 0;i < n;i++){
    		for (j = 0;j < n;j++){
    			if (a[i][j] == 0){
    				cout<<setw(5)<<" ";
    			}else{
    				cout<<setw(5)<<a[i][j];
    			}
    		}
    		cout<<endl;
    	}
    }
    

    Python :

    def getList(m,n):
        '''
          m 行数
          n 列数
          返回的是m行n列的二维数组,默认值为0
        '''
        ma = [ [0 for i in range(n)] for i in range(m) ]
        return ma
    
    def printList(arr):
        for i in arr:
            for j in i:
                if  j != 0:
                   print('%5d' %(j) ,end='')
                else:
                   print('%5s' %('') ,end='')
            print();
    
    n = int( input() );
    a = getList(n,n)
    for i in range(0,n):
        k = 1
        for j in range(i,n):
            if j >= i:
              a[i][j] = k;
              k += 1
    
    printList(a)
    
    
    • 1

    信息

    ID
    2140
    时间
    1000ms
    内存
    16MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者