1 条题解

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

    C :

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

    C++ :

    #include<iostream>
    using namespace std;
    int main(){
    	int n,i,j,s;
    	cin>>n;
    	int a[2*n+1][2*n+1];
    	for(i=0;i<n+1;i++){
    		s=n;
    		for(j=0;j<n+1;j++){
    			a[i][j]=s;
    			if(j<i) s--;
    		}
    		for(j=n+1;j<2*n+1;j++){
    			if(j>2*n-i) s++;
    			a[i][j]=s;
    		}
    	}
    	for(i=n+1;i<2*n+1;i++){
    		s=n;
    		for(j=0;j<n+1;j++){
    			a[i][j]=s;
    			if(j<2*n-i) s--;
    		}
    		for(j=n+1;j<2*n+1;j++){
    			if(j>i) s++;
    			a[i][j]=s;
    		}
    	}
    	for(i=0;i<2*n+1;i++){
    		for(j=0;j<2*n+1;j++) printf("%2d",a[i][j]);
    		cout<<endl;
    	}
    }
    

    Python :

    n=int(input())
    a=[[0 for i in range(2*n+1)] for j in range(2*n+1)]
    k=n
    n=2*n+1
    for i in range(n//2+1):
        for j in range(i,n-i):
            a[i][j]=k
        for j in range(i+1,n-i):
            a[j][n-i-1]=k
        for j in range(n-i-2,i-1,-1):
            a[n-i-1][j]=k
        for j in range(n-i-2,i,-1):
            a[j][i]=k
        k=k-1
    for i in range(n):
        for j in range(n):
            print("%2d"%a[i][j],end="")
        print()
    
    • 1

    信息

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