5 solutions

  • 1
    @ 2023-12-2 16:15:21
    #include<bits/stdc++.h>
    using namespace std;
    int n;
    int x=0;//控制每行填几个 
    int main(){
    	cin>>n;
    	int w=n;//存n 
    	int a[n*2+1][n*2+1];//题目没有给数据保险起见直接定义 
    	while(w){//一个一个填 
    		for(int i=x;i<n*2+1-x;i++){
    			for(int j=x;j<n*2+1-x;j++){
    				a[i][j]=w;
    			}
    		}
    		w--;
    		x++;
    	}
    	a[(n*2+1)/2][(n*2+1)/2]=0;
    	for(int i=0;i<n*2+1;i++){
    		for(int j=0;j<n*2+1;j++){
    			cout<<' '<<a[i][j];//虽然不知道为什么空格放前面但是不这样会错 
    		}
    		cout<<endl;
    	}
    	return 0;//不必多言 
    }
    

    可以打表

    • 0
      @ 2024-11-22 18:30:14
      #include <bits/stdc++.h>
      using namespace std;
      int ppp[50][50];//输出数组
      int main(){
      	int n;
      	cin>>n;
      	int pp=n*2+1;
      	for(int x=0;x<pp;x++)for(int y=0;y<pp;y++)ppp[x][y]=0;
      	for(int i=n,j=1;i>=0;i--,j++)for(int x=j-1;x<pp-j+1;x++)for(int y=j-1;y<pp-j+1;y++)ppp[x][y]=i;
      	for(int x=0;x<pp;x++){
      		for(int y=0;y<pp;y++)printf("%2d",ppp[x][y]);
      		cout<<endl;
      	}
      	return 0;
      }
      
      • 0
        @ 2023-10-6 10:42:38
        #include <bits/stdc++.h>
        using namespace std;
        int main(int argc, char **argv){
        	int n;
        	cin >> n;
        	int a[n * 2 + 1][n * 2 + 1];
        
        	for (int i = 0;i < n * 2 + 1;i++){
        		for (int j = 0;j < n * 2 + 1;j++){
        			a[i][j] = 0;
        		}
        	}
        
        	int w = n;
        	int idx = 0;
        	while (w >= 0){
        		for (int i = idx;i < n * 2 + 1 - idx;i++){
        			for (int j = idx;j < n * 2 + 1 - idx;j++){
        				a[i][j] = w;
        			}
        		}
        		w--;idx++;
        	}
        
        	for (int i = 0;i < n * 2 + 1;i++){
        		for (int j = 0;j < n * 2 + 1;j++){
        			cout << setw(2) << a[i][j];	// 注意这里不用设置靠左 
        		}
        		cout << endl;
        	}
        	// cout << "8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8\n";
        	// cout << "8 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8\n";
        	// cout << "8 7 6 6 6 6 6 6 6 6 6 6 6 6 6 7 8\n";
        	// cout << "8 7 6 5 5 5 5 5 5 5 5 5 5 5 6 7 8\n";
        	// cout << "8 7 6 5 4 4 4 4 4 4 4 4 4 5 6 7 8\n";
        	// cout << "8 7 6 5 4 3 3 3 3 3 3 3 4 5 6 7 8\n";
        	// cout << "8 7 6 5 4 3 2 2 2 2 2 3 4 5 6 7 8\n";
        	// cout << "8 7 6 5 4 3 2 1 1 1 2 3 4 5 6 7 8\n";
        	// cout << "8 7 6 5 4 3 2 1 0 1 2 3 4 5 6 7 8\n";
        	// cout << "8 7 6 5 4 3 2 1 1 1 2 3 4 5 6 7 8\n";
        	// cout << "8 7 6 5 4 3 2 2 2 2 2 3 4 5 6 7 8\n";
        	// cout << "8 7 6 5 4 3 3 3 3 3 3 3 4 5 6 7 8\n";
        	// cout << "8 7 6 5 4 4 4 4 4 4 4 4 4 5 6 7 8\n";
        	// cout << "8 7 6 5 5 5 5 5 5 5 5 5 5 5 6 7 8\n";
        	// cout << "8 7 6 6 6 6 6 6 6 6 6 6 6 6 6 7 8\n";
        	// cout << "8 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8\n";
        	// cout << "8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8";
        	return 0;
        }
        
      • 0
        @ 2023-10-2 15:23:08
        using namespace std;
        int num[105][105];
        int main(){
        	int n,w;
        	cin>>n;
        	w = n;
        	num[1][1] = n;
        	for(int i=1,j=1;w >= 0;){//一直填到n*n个数填完
        		while(++j<=2*n+1&&!num[i][j])num[i][j]=w;--j;//向右 
        		while(++i<=2*n+1&&!num[i][j])num[i][j]=w;--i;//向下
        		while(--j> 0&&!num[i][j])num[i][j]=w;++j;//向左 
        		while(--i> 0&&!num[i][j])num[i][j]=w;++i;//向上
        		w--;
        		}
        		
        	for(int i=1;i<=2 * n + 1;++i,cout<<endl)for(int j=1;j<=2 * n + 1;++j)
        	cout<<setw(2)<< num[i][j];//输出
        	return 0;
        }`
        
        • -1
          @ 2024-10-15 18:46:28

          一个方阵,每个数字的场宽为2!

          场宽是从左到右

          输出样例每一行前面都要加一个空格 用printf("%2d", ?)

          #include<bits/stdc++.h>
          using namespace std;
          int n;
          int x=0;
          int main()
          {
          	cin>>n;//输入n 
          	int w=n;//副本 
          	int a[n*2+1][n*2+1];
          	while(w!=0)//0代表输入完成 
          	{
          		for(int i=x;i<n*2+1-x;i++)
          		{
          			for(int j=x;j<n*2+1-x;j++)
          			{
          				a[i][j]=w;
          			}
          		}
          		w--;
          		x++;
          	}//这种方法中间的数是1
          	a[(n*2+1)/2][(n*2+1)/2]=0;//最中间的数是0 //特判
          	for(int i=0;i<n*2+1;i++)
          	{
          		for(int j=0;j<n*2+1;j++)
          		{
          			 
          			printf("%2d", a[i][j]);//输出 
          		}
          		cout<<endl;
          	}
          	return 0;
          }
          //缺点:会重复输入
          //8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 //2*8+1 
          //8 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8
          //8 7 6 6 6 6 6 6 6 6 6 6 6 6 6 7 8
          //8 7 6 5 5 5 5 5 5 5 5 5 5 5 6 7 8
          //8 7 6 5 4 4 4 4 4 4 4 4 4 5 6 7 8
          //8 7 6 5 4 3 3 3 3 3 3 3 4 5 6 7 8
          //8 7 6 5 4 3 2 2 2 2 2 3 4 5 6 7 8
          //8 7 6 5 4 3 2 1 1 1 2 3 4 5 6 7 8
          //8 7 6 5 4 3 2 1 0 1 2 3 4 5 6 7 8
          //8 7 6 5 4 3 2 1 1 1 2 3 4 5 6 7 8
          //8 7 6 5 4 3 2 2 2 2 2 3 4 5 6 7 8
          //8 7 6 5 4 3 3 3 3 3 3 3 4 5 6 7 8
          //8 7 6 5 4 4 4 4 4 4 4 4 4 5 6 7 8
          //8 7 6 5 5 5 5 5 5 5 5 5 5 5 6 7 8
          //8 7 6 6 6 6 6 6 6 6 6 6 6 6 6 7 8
          //8 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8
          //8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8
          
          • 1

          Information

          ID
          297
          Time
          1000ms
          Memory
          16MiB
          Difficulty
          8
          Tags
          # Submissions
          268
          Accepted
          45
          Uploaded By