右对齐三角形的间距

时间:2019-06-24 07:15:14

标签: c++ loops for-loop

分配:

编写一个程序,提示用户输入一个代表三角形高度的整数值。然后,程序应打印出该高度的O的三角形,其右边缘垂直对齐。

我的问题:

我已经找到执行正常三角形的代码,但是我在编写代码时遇到一些困难,要在我的“ 0”之前留出空格以使其向右对齐。

#include <iostream>

using namespace std;

int main()
{
    int triHeight;
    int c = 0;
    int r = 0;
    int k = 0;

    cout << "Enter the triangle height: " << endl;
    cin >> triHeight;

    for (c = 0; c <= triHeight; c = c+1)
    {
        for (r = 0; r < c; r = r + 1)
        {
            cout << "0";
        }
        for (k = 0; k <= c; k = k - 1)
        {
            cout << " ";
        }
        cout << endl;
    }

    system("pause");
    return 0;
}

3 个答案:

答案 0 :(得分:2)

您有两个主要问题:

  • 您要在空格后 而不是之前写空格

  • for (k = 0; k <= c; k = k - 1)永远不会导致溢出的可能结果

我还建议您检查>>的结果,以确保输入了有效的整数,例如if (!(cin >> triHeight)) cerr << "invalid height" << endl; else { ... }

对所有人都使用循环的正确方法是(假设您想要金字塔):

#include <iostream>

using namespace std; 

int main()
{
  int triHeight;

  cout << "Enter the triangle height: " << endl;
  if (!(cin >> triHeight))
    cerr << "invalid height" << endl;
  else {
    for (int h = 1; h <= triHeight; h += 1) {
      for (int s = triHeight - h; s != 0; s -= 1)
        cout << ' ';
      for (int z = 2*(h-1)+1; z >0; z -=1)
        cout << '0';
      cout << endl;
    }
  }
}

编译和执行:

/tmp % g++ -pedantic -Wextra -Wall c.cc
/tmp % ./a.out
Enter the triangle height: 
5
    0
   000
  00000
 0000000
000000000
/tmp % 

如果您想要半个金字塔:

 #include <iostream>

using namespace std; 

int main()
{
  int triHeight;

  cout << "Enter the triangle height: " << endl;
  if (!(cin >> triHeight))
    cerr << "invalid height" << endl;
  else {
    for (int h = 1; h <= triHeight; h += 1) {
      for (int s = triHeight - h; s != 0; s -= 1)
        cout << ' ';
      for (int z = 0; z < h; z += 1)
        cout << '0';
      cout << endl;
    }
  }
}

编译和执行:

/tmp % g++ -pedantic -Wall -Wextra c.cc
/tmp % ./a.out
Enter the triangle height: 
5
    0
   00
  000
 0000
00000
/tmp % 

您也不能自己进行两个内部循环:

#include <iostream>

using namespace std; 

int main()
{
  int triHeight;

  cout << "Enter the triangle height: " << endl;
  if (!(cin >> triHeight))
    cerr << "invalid height" << endl;
  else {
    for (int h = 1; h <= triHeight; h += 1)
      cout << string(triHeight - h, ' ') << string(h, '0') << endl;
  }
}

编译和执行:

/tmp % g++ -pedantic -Wall -Wextra c.cc
/tmp % ./a.out
Enter the triangle height: 
5
    0
   00
  000
 0000
00000
/tmp % 

该解决方案较短,但会创建临时字符串

答案 1 :(得分:0)

最简单的方法是使用std::iostream的内置功能:

#include <iostream>
#include <iomanip>
#include <string>

int main()
{
    int triHeight;
    int c = 0;

    std::cout << "Enter the triangle height: \n";
    std::cin >> triHeight;

    std::cout << std::setfill(' ') << std::right;
    for (c = 0; c < triHeight; c++)
    {
        std::cout << std::setw(triHeight) << std::string(c+1,'0') << '\n';
    }
    return 0;
}

只需将std::right更改为std::left,即可返回到左对齐的三角形。

答案 2 :(得分:0)

您的程序逻辑错误。首先,您尝试输出符号“ O”

    for (r = 0; r < c; r = r + 1)
    {
        cout << "0";
    }

然后您要输出空格

    for (k = 0; k <= c; k = k - 1)
    {
        cout << " ";
    }

此外,此循环无效,因为变量k从0开始递减。

只能使用一个循环和在头文件<iomanip>中声明的标准i / o操作器来编写程序。

这是一个演示程序

#include <iostream>
#include <iomanip>

int main() 
{
    while ( true )
    {
        const char c = 'O';

        std::cout << "Enter the height of a triangle (0 - exit): ";

        int height = 0;

        if ( not ( std::cin >> height ) or ( height <= 0 ) ) break;

        std::cout << '\n';

        for ( int i = 0; i < height; i++ )
        {
            std::cout << std::setw( height - i ) << std::setfill( ' ' ) << c;
            std::cout << std::setw( i + 1 ) << std::setfill( c ) << '\n';
        }

        std::cout << '\n';
    }

    return 0;
}

程序输出可能如下所示

Enter the height of a triangle (0 - exit): 1

O

Enter the height of a triangle (0 - exit): 2

 O
OO

Enter the height of a triangle (0 - exit): 3

  O
 OO
OOO

Enter the height of a triangle (0 - exit): 4

   O
  OO
 OOO
OOOO

Enter the height of a triangle (0 - exit): 5

    O
   OO
  OOO
 OOOO
OOOOO

Enter the height of a triangle (0 - exit): 6

     O
    OO
   OOO
  OOOO
 OOOOO
OOOOOO

Enter the height of a triangle (0 - exit): 7

      O
     OO
    OOO
   OOOO
  OOOOO
 OOOOOO
OOOOOOO

Enter the height of a triangle (0 - exit): 8

       O
      OO
     OOO
    OOOO
   OOOOO
  OOOOOO
 OOOOOOO
OOOOOOOO

Enter the height of a triangle (0 - exit): 9

        O
       OO
      OOO
     OOOO
    OOOOO
   OOOOOO
  OOOOOOO
 OOOOOOOO
OOOOOOOOO

Enter the height of a triangle (0 - exit): 10

         O
        OO
       OOO
      OOOO
     OOOOO
    OOOOOO
   OOOOOOO
  OOOOOOOO
 OOOOOOOOO
OOOOOOOOOO

Enter the height of a triangle (0 - exit): 0