- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我已经使用 cout.width(total_rows - current_row) 成功地编写了一段代码来输出有点像三角形的帕斯卡三角,但它看起来像这样:
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
我希望它完全居中。我发现我可以取底行中的数字或字符,减去当前行中的字符数,然后将其除以二以获得每行所需的空格数 [这看起来像: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);
}
最佳答案
我想通了。您必须使用 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();
}
关于c++ - 在 C++ 中居中 Pascal 的三角形输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7502344/
说我有两个文件,characters.pas和ogre.pas。食人魔是一个角色,但为了干净起见,我试图将两个文件分开。在characters.pas中,我有 unit Characters; {$m
我想要一个编程代码来打印范围之间的奇数来教学生。这里我取的范围是 1 到 10。所以我想打印 1 到 10 之间的奇数。 我写了这个代码来打印 1 到 10 之间的奇数 program printOd
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 4年前关闭。 Improve this
我创建了一个函数,其中包含 2 个具有相同数据类型的参数,对此我没有问题。 但是我在处理不同的数据类型时遇到了问题 这是我的代码: uses crt; function inputscore(name
我只是想知道如何在 pascal 中进行类型检查?我已经搜索了几个小时,但我找不到任何有用的东西。 例子: var number: Integer; begin write('Enter a nu
我正在实现 an assemblinker for the 16-bit DCPU来自游戏 0x10c。 有人向我建议的一种技术是使用“覆盖,就像过去在 Turbo Pascal 中一样”,以便在运行
Q1:这是什么意思:WriteLn (#$0b) ? $0b应该是十六进制,如 0x0b ,但是 # 呢?标志? 问题 2: x:=readkey; if ( x = #5) do... 是否#5意思
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 7年前关闭。 Improve t
对于在 Pascal 中返回值的函数,赋值 FunctionName := SomeVal;用来。我认为它不会像 return 那样在那个确切的地方停止函数执行在 C 中。有没有类似C的东西retur
我正在尝试在 Pascal 中创建一个类,我对声明和语法有点困惑。主要的事情是一个错误,我收到“前向声明未解决 Tetromino.Rotate(LongInt)”,我读到我需要在实现部分声明我的过程
我正在开展我的学校项目,我想使用动态(而不是静态)数组。我使用 ObjectPascal,所以我习惯了一些语法。但现在我在旧的 TurboPascal 中编程(我使用的是 Windows 版 Turb
对于这项任务,我有一个不太好的解决方案,但有人可以告诉我如何使用标准库或更优雅的方法。当前的解决方案基于这样的事实:对对象的过程的引用在内存中存储两个指针,一个指向过程代码的指针和一个指向对象的指针。
我正在运行一个 Fenuc Karel 机器人进行类作业,它使用 Pascal 的变体,但我们的机器人是在 1991-1993 年添加 random() 之前的。有谁知道如何在 Pascal 的旧 d
我正在尝试在 Pascal 中创建一个类,我对声明和语法有点困惑。主要的事情是一个错误,我收到“前向声明未解决 Tetromino.Rotate(LongInt)”,我读到我需要在实现部分声明我的过程
我认为展示问题的最简单方法是举个例子。编码: PROGRAM CONSTANTSTRING(OUTPUT); CONST C_MaxLength = 30; VAR small_str
我在 Windows 上运行 Lazarus。我真的很想制作“Beep”程序。看来你可以在 Pascal 中使用: windows.beep(300,500); 但不是在拉撒路!我可以使用另一个命令吗
我正在对一个 18 岁的 pascal 程序进行维护。为了帮助我理解一切是如何组合在一起的,我想绘制一个调用图。但是我找不到任何可以为 pascal 源绘制调用图的软件。我目前正在使用 Turbo P
我正在使用帕斯卡。我在处理读取文件时遇到问题。 我有一个带有整数的文件。我读取文件的 pascal 是: read(input, arr[i]); 如果我的文件内容是 1 2 3那么它很好,但如果它是
当分配运算符 := 时会发生什么?在 Object Pascal 中重载?我主要是指首先评估什么,更重要的是如何(如果可能)更改此顺序。这是一个让我烦恼的例子: 我声明TMyClass因此: TMyC
如何使用 writeln 在 Pascal 中打印撇号符号功能? 例子: writeln('My brother's book'); 无法工作,因为 s book没有“写”函数,所以编译器返回一个错误
我是一名优秀的程序员,十分优秀!