【发布时间】:2015-01-06 19:14:32
【问题描述】:
我必须编写一个代码来生成一个有 12 行的帕斯卡三角形。
我自己写了所有东西,除了一部分,那是我们用来生成数字的公式。问题是我不明白我们的计数器和生成的数字之间有什么联系(因为我们正在使用我们的计数器。)。
#include <iostream>
#include <string>
using namespace std;
int main() {
const int rows=12;
int padding, value, fxValue;
for(int rowCounter=0; rowCounter<rows; rowCounter++)
{
fxValue=1;
cout << string((rows-rowCounter)*6, ' ');
for(int fxCounter=0; fxCounter<=rowCounter; fxCounter++)
{
value=fxValue;
fxValue = fxValue*(rowCounter-fxCounter)/(fxCounter+1);
// cout << "fxCounter: "<< fxCounter << endl
// << "rowCounter: " << rowCounter << endl
// << "fxCounter: " << fxCounter << endl
// << "fxValue: " << fxValue << endl;
padding=fxValue/10;
if(padding==0) cout << value << string(11, ' ');
else if(10>padding) cout << value << string(10, ' ');
else if(padding>10) cout << value << string(9, ' ');
}
cout << endl;
}
return 0;
}
问题来了:
fxValue = fxValue*(rowCounter-fxCounter)/(fxCounter+1);
有人可以解释一下作者是如何想到使用这些变量的想法以及它如何正常工作的吗?
【问题讨论】:
-
很好的例子说明为什么好的变量名很重要。
-
I've written everything on my own except one part, That's the formula we use to generate numbers这不是你作业中最重要的部分吗? -
@PaulMcKenzie 哈哈是的,这就是我问你的原因
标签: c++ pascals-triangle