C++ 数字的矩形

C++ 数字的矩形,c++,shapes,C++,Shapes,但我真的希望我的代码像这样打印出来,我尝试了很多改变,但都没有成功。所以,我期待着你们 1 2 3 4 5 5 9 5 9 5 9 13 12 11 10 9 如果你在控制台上打印一些东西,回到队列和回车将非常混乱 诀窍是将问题分为3个阶段: 阶段1:打印顶行,足够简单 第二阶段:打印环绕的最大数字,然后打印一些空白,最后以数字结束,确保相应地增加和减少数字 阶段3:打印最后一行 下面是我刚才描述的算法的代码: 1 2 3 4 5 16

但我真的希望我的代码像这样打印出来,我尝试了很多改变,但都没有成功。所以,我期待着你们

1 2 3 4 5
5          9
5          9
5          9
13 12 11 10 9

如果你在控制台上打印一些东西,回到队列和回车将非常混乱

诀窍是将问题分为3个阶段:

阶段1:打印顶行,足够简单

第二阶段:打印环绕的最大数字,然后打印一些空白,最后以数字结束,确保相应地增加和减少数字

阶段3:打印最后一行

下面是我刚才描述的算法的代码:

1 2 3 4 5
16          6
15          7
14          8
13 12 11 10 9
#包括
使用名称空间std;
int main()
{
常数int宽度=6;
const int height=6;
整数正面=(高度-1)*2+(宽度-1)*2;
int numberAtTheEnd=宽度;

对于(int i=1;i,使用
#defines
const
变量有助于避免代码中的幻数。这使它更可读、更可扩展。例如,如果您想制作一个20x20的正方形,您的代码将需要完全重写

从这个有效的解决方案开始,在编码中实现这个原则

#include <iostream>

using namespace std;


int main()
{
    const int width=6;

    const int height=6;

    int numberInFront=(height-1)*2 + (width-1)*2;
    int numberAtTheEnd= width;
    for(int i=1; i<width; ++i) cout<<i<<"\t";  //print top line
    cout<<endl;

    for(int i=0; i<height-1; ++i)
    {
       cout<<numberInFront<<"\t";
       for(int j=0; j<width-3; j++) cout<<"\t"; //print inner space
       cout<<numberAtTheEnd<<endl;
       numberInFront--;
       numberAtTheEnd++;
    }


    //print last line:
    int counter = numberInFront;
    while(counter!=numberAtTheEnd-1)
    {
        cout<<counter<<"\t";
        counter--;

    }
    return 0;
}
#包括
使用名称空间std;
#定义第4面
int main(){
int周长=侧面*4;
对于(int width=0;width,这里是解决方案

#include <iostream>

using namespace std;

#define SIDE 4

int main(){

   int perimeter = SIDE * 4;

   for(int width=0; width<=SIDE; width++)
   {
      if(width < 1) {
         for(int width=0; width <= SIDE; width++) {
            cout<<" "<<width + 1<<" ";
         }
         cout<< endl;
      }
      else if(width < SIDE)
      {
         cout<<" "<<perimeter - width + 1 << "\t\t" << (SIDE + width) + 1;
         cout<< endl;
      }
      else
      {
         for(int width3 = perimeter - SIDE; width3 >= perimeter - 2 * SIDE; width3--) {
            cout<<" "<<width3 + 1<<" ";
         }
         cout<< endl;
      }
   }
   return 0;
}
int-width=6;
整数总计=(宽度-1)*4;

对于(int row=1;row为什么在外部和嵌套循环中使用
width
作为循环计数器?实际上,我首先使用用户输入创建了stars框,然后将其更改为数字框。在编写任何代码之前,请绘制3、4、5、6的宽度,并查找模式。您的代码看起来像是试图通过编写代码来找到解决方案首先在纸上绘制公式生成每一行。例如,第一行下方的行始终从数字
宽度*4-4
开始,相应的右侧从
宽度+1
开始。始终有
宽度-2
中间行要绘制。最后一行始终从
宽度*3-2
开始。因此,有3个循环,分开和分开互操作(无需嵌套)——顶行、中间行、最后一行。
#include <iostream>

using namespace std;

#define SIDE 4

int main(){

   int perimeter = SIDE * 4;

   for(int width=0; width<=SIDE; width++)
   {
      if(width < 1) {
         for(int width=0; width <= SIDE; width++) {
            cout<<" "<<width + 1<<" ";
         }
         cout<< endl;
      }
      else if(width < SIDE)
      {
         cout<<" "<<perimeter - width + 1 << "\t\t" << (SIDE + width) + 1;
         cout<< endl;
      }
      else
      {
         for(int width3 = perimeter - SIDE; width3 >= perimeter - 2 * SIDE; width3--) {
            cout<<" "<<width3 + 1<<" ";
         }
         cout<< endl;
      }
   }
   return 0;
}
  int  width =6;
  int total =  (width-1)*4;
  for(int row=1; row <=width; row++)
  {
    if(row == 1 )
    {
        for(int pr=1; pr<=width; pr++)
        {
            cout<<" "<<pr<<" ";
        }
        cout<<"\n";
    }
    else if( row == width)
    {
        for(int pr=1; pr<=width; pr++)
        {
            cout<<" "<<(total-row-pr+3)<<" ";
        }
    }
    else 
    {
         for(int pr=1; pr<=width; pr++)
        {

            if(pr ==1 )
                cout<<" "<<(total-row+2)<<" ";
            else if(pr ==width)
                cout<<" "<<(width+row-1)<<" ";
            else
                cout<<" "<<" "<<" ";
        }
         cout<<"\n";
    }
  }