簡體   English   中英

用C ++中心Pascal的三角形輸出

[英]Centering Pascal's Triangle Output in C++

我已成功編寫了一個代碼,使用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(數組),但兩者都沒有效果。 (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);
}

我想到了。 您必須使用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();
}

使用std::setw使所有輸出恆定寬度:

cout << setw(5);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM