1 条题解

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

    C :

    #include<stdio.h>
    int a[100][100];
    int main(){
        int n,i,j;
        scanf("%d",&n);
        for(j=1;j<=n;j++){
            for(i=1;i<=n;i++){
                if(i+j==n+1){
                    a[i][j] = j;
                }else if(i+j>n+1){
                    a[i][j] = a[i-1][j] + a[i][j-1];
                }
            }
        }   
        for(i=1;i<=n;i++){
            for(j=1;j<=n;j++){
                if(a[i][j]==0){
                    printf("%5s","");
                }else{
                    printf("%5d",a[i][j]);
                }
            }
            printf("\n");
        }
    
        return 0;
    }
    
    

    C++ :

    #include <bits/stdc++.h>
    using namespace std;
    
    int main(){
    	int a[100][100] = {0};
    	int i,j,n;
    	cin>>n;
    	
    	for (i = 0;i < n;i++){
    		for (j = 0;j < n;j++){
    			if (i + j + 1 == n){
    				a[i][j] = n - i;
    			}
    		}
    	}
    	
    	for (i = 1;i < n;i++){
    		for (j = 0;j < i;j++){
    			a[i][j + n - i] = a[i][j + n - i - 1] + a[i - 1][j + n - i];
    		}
    	}
    	
    	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 :

    n = int(input())
    a = [[0 for j in range(n)] for i in range(n)]
    # 先赋值右对角线,i+j=n-1
    for i in range(n):
        a[i][n - i - 1] = n - i
    # 赋值有对角线下面部分,以"列"为大方向从1~n-1,正方向;以"行"为小方向从n-j~n-1,正方向
    for j in range(1, n):
        for i in range(n - j, n):
            a[i][j] = a[i][j - 1] + a[i - 1][j]
    # 打印输出
    for i in range(n):
        for j in range(n):
            if a[i][j] == 0:
                print('%5s' % '', end='')
            else:
                print('%5d' % a[i][j], end='')
        print()
    
    • 1

    信息

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