1 条题解

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

    C++ :

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

    Python :

    n = int(input())
    a = [[1 for j in range(n)] for i in range(n)]
    # 先把中心点赋值为0
    a[n // 2][n // 2] = 0
    # 循环遍历,找出规律
    for i in range(n):
        for j in range(n):
            if i + j == n // 2 or i - j == n // 2 or j - i == n // 2:
                a[i][j] = 0
            if n == 5 and i + j == n + 1:
                a[i][j] = 0
            if n == 7 and i + j == n + 2:
                a[i][j] = 0
            if n == 9 and i + j == n + 3:
                a[i][j] = 0
    # 打印输出
    for i in range(n):
        for j in range(n):
            print('%3d' % a[i][j], end='')
        print()
    
    • 1

    信息

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