1 条题解

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

    C :

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

    C++ :

    #include<bits/stdc++.h>
    using namespace std;
    int a[110][110];
    int main()
    {
    	int n;
    	cin>>n;
    	for(int i=0;i<n;i++)
    	{
    		for(int j=i;j<n;j++)
    		{
    			for(int k=0;k<n-i;k++)
    			{
    				a[j][k]++;
    			}
    		}
    	} 
    	for(int i=0;i<n;i++)
    	{
    		for(int j=0;j<n;j++)
    		{
    			cout<<setw(3)<<a[i][j];
    		}
    		cout<<endl;
    	}
    	return 0;
    }
    
    
    

    Python :

    n = int(input())
    a = [[0 for j in range(n)] for i in range(n)]
    
    for i in range(n):
        # 先定好第一列数字
        a[i][0] = i + 1
        for j in range(1, n - i):
            a[i][j] = a[i][j - 1]
        for j in range(n - i, n):
            a[i][j] = a[i][j - 1] - 1
    
    for i in range(n):
        for j in range(n):
            print('%3d' % a[i][j], end='')
        print()
    
    
    • 1

    信息

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