【问题标题】:Centering Pascal's Triangle Output in C++在 C++ 中将 Pascal 的三角形输出居中
【发布时间】:2011-11-22 01:46:02
【问题描述】:

我已经成功编写了一个代码,使用 cout.width(total_rows - current_row) 将 Pascal 的三角形输出为有点三角形的形状,但它看起来像这样:

               1 
              1 1 
             1 2 1 
            1 3 3 1 
           1 4 6 4 1 
          1 5 10 10 5 1 
         1 6 15 20 15 6 1 
        1 7 21 35 35 21 7 1 
       1 8 28 56 70 56 28 8 1 
      1 9 36 84 126 126 84 36 9 1 
     1 10 45 120 210 252 210 120 45 10 1 
    1 11 55 165 330 462 462 330 165 55 11 1 
   1 12 66 220 495 792 924 792 495 220 66 12 1 
  1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1 
 1 14 91 364 1001 2002 3003 3432 3003 2002 1001 364 91 14 1 
1 15 105 455 1365 3003 5005 6435 6435 5005 3003 1365 455 105 15 1

我希望它完全居中。我发现我可以取底行中的一个或多个字符,减去当前行中的字符数,然后将其除以 2 以获得每行所需的空格数 [看起来像:cout.width( (bottow_row_characters - current_row_characters) / 2) ],但我在实际实现这个想法时遇到了麻烦。

我尝试只计算最底行并在其中存储一个字符串或数组,然后使用 string.length() 或 sizeof(array),但都没有奏效。 (sizeof 总是返回 4,这是不正确的)

代码如下:

#include <iostream>
#include <string>
using namespace std;


// Forward declaration of a function.
int Pascal (int row, int column);




 /* The main function.
  *
  * Parameters:
  *    none
  *
  * Return value:
  *    0 if we complete successfully, 1 if there was an error.
  */

 int main ()
 {

  // introduction

  cout << "\nPascal's Triangle!\n";
  cout << "(Pascal's triangle is made by taking the sum of two numbers\n";
  cout << "and placing that number directly underneath the two numbers.\n";
  cout << "This creates a triangular array of binomial coefficients)\n\n";


  // for loops to calculate and print out pascal's triangle

     for (int row = 0; row <= 15; row++)
    {

      cout.width(16 - row);

      for (int column = 0; column <= row; column++)
        {
          cout << Pascal(row, column) << " ";
        }

      cout << endl;
    }

    cout << endl;
}




/* This function calculates Pascal's triangle based on row and column position.
 *
 * Parameters:
 *    row, column
 *
 * Return value:
 *    the numbers in Pascal's triangle
 */

int Pascal (int row, int column)
{

  // if statements to calculate pascal's triangle through recursion

  if (column == 0)
    return 1;

  else if (row == column)
    return 1;

  else
    return Pascal(row - 1, column - 1) + Pascal(row - 1, column);
}

【问题讨论】:

  • 伙计,只需使用“printf”并输出它... :-)
  • @Vlad,通常 printf 是更简单的解决方案,但这种情况并非如此。
  • @Mark:为什么不呢?如果字体是等宽字体,只需执行printf (" t\n ttt\n"); 等操作...有什么比调用单个 printf 更容易的呢?
  • 我不想只使用标签。我真的很想知道如何优化 cout.width

标签: c++ centering fixed-width pascals-triangle


【解决方案1】:

我想通了。您必须使用 stringstream 库将整数行从 Pascal 函数转换为字符串。然后,您可以使用 string.length() 来计算字符串中有多少个字符。然后你做我之前解释的数学来调整输出。

这是我的代码:

/*
 * Pascal's Triangle: Prints the first 15 rows of Pascal's triangle.
 *
 */


#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>
using namespace std;


// Forward declaration of a function.
int Pascal (int row, int column);
int rowLength (int row, int column);



/* The main function.
 *
 * Parameters:
 *    none
 *
 * Return value:
 *    0 if we complete successfully, 1 if there was an error.
 */

int main ()
{

  // introduction

  cout << "\nPascal's Triangle!\n";
  cout << "(Pascal's triangle is made by taking the sum of two numbers\n";
  cout << "and placing that number directly underneath the two numbers.\n";
  cout << "This creates a triangular array of binomial coefficients)\n\n";


  // determination of how long the bottom row is

  int bottom_row;
  string bottom_row_characters;
  stringstream out;

   for (int row = 15; row <= 15; row++)
    {

      for (int column = 0; column <= row; column++)
        {
          out << " " << Pascal(row, column) << " ";
        }
      bottom_row_characters += out.str();
    }


  // for loops to calculate and print out pascal's triangle

   for (int row = 0; row <= 15; row++)
    {
      cout.width((bottom_row_characters.length() - rowLength(row, 0)) / 2);

      for (int column = 0; column <= row; column++)
        {
          cout << " " << Pascal(row, column) << " ";
        }

      cout << endl;
    }

    cout << endl;
}


/* This function calculates Pascal's triangle based on row and column position.
 *
 * Parameters:
 *    row, column
 *
 * Return value:
 *    the numbers in Pascal's triangle
 */

int Pascal (int row, int column)
{

  // if statements to calculate pascal's triangle through recursion

  if (column == 0)
    return 1;

  else if (row == column)
    return 1;

  else
    return Pascal(row - 1, column - 1) + Pascal(row - 1, column);
}





/* This function converts a row from Pascal's Triangle from integers to a string
 *
 * Parameters:
 *    row, column
 *
 * Return value:
 *    a string representing a row in Pascal's triangle
 */

int rowLength (int row, int column)
{

  int current_row;
  string current_row_characters;
  stringstream out;

  for (int current_row = row; current_row <= row; current_row++)
    {
     for (int column = 0; column <= row; column++)
       {
         out << " " << Pascal(row, column) << " ";
       }
     current_row_characters += out.str();
    }

  return current_row_characters.length();
}

【讨论】:

    【解决方案2】:

    使用std::setw 使所有输出的宽度恒定:

    cout << setw(5);
    

    【讨论】:

    • +1 这很接近,但不会过于复杂。 :) 尽管它没有完全居中输出,因为数字在 5 个字符的窗口中是右对齐的。
    • 注意:对于这项工作,setw(5) 命令必须放在 for 循环中对cout 的调用之后,因为原始代码使用了cout.width(16 - row);缩进每一行
    • @e.James,我认为您唯一的解决方案是将数字转换为字符串在输出之前,并在前面和后面插入空格字符串以将其填充到正确的宽度。
    • 看起来更好,但还是没有居中:
    猜你喜欢
    • 1970-01-01
    • 2013-10-20
    • 2021-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多