数字的矩形形状

Rectangle shape of numerics

本文关键字:数字      更新时间:2023-10-16
for(int width=1; width<=5; width++) {
    if(width <= 1) {
        for(int width=1; width<=5; width++) {
            cout<<" "<<width<<" ";
        }
    } else if(width<5) {
        cout<< endl;
        for(int width2=5; width2<=9; width2++) {
            if(width2==5 || width2==9)
                cout<<" "<<width2<<" ";
            else
                cout<< " ";
        }
    } else {
        cout<< endl;
        for(int width3=13; width3>=9; width3--) {
            cout<<" "<<width3<<" ";
        }
    }
}

我在上面发布的这段代码绘制了这个形状

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

但我实际上希望我的代码像这样打印它,我已经尝试了很多改变的东西,但都是徒劳的。 所以,我很期待你们。

1 2 3 4 5
16          6
15          7
14          8
13 12 11 10 9

如果您在控制台上打印某些内容,则返回行和回车将非常混乱。

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

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

Stage2:打印最大的数字环绕,然后打印一些空白区域并以末尾的数字结束,确保相应地增加和减少数字。

阶段 3:打印最后一行。

这是我刚刚描述的算法的代码:

#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;
}

它有助于避免使用#definesconst变量的代码中的幻数。 这使它更具可读性和可扩展性。 例如,如果你想制作一个 20x20 的正方形,你的代码需要完全重写!

从这个工作解决方案开始,将此原则实施到您的编码中。

#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 << "tt" << (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";
    }
  }