题目链接:https://www.luogu.com.cn/problem/P5732
思路:
一个数组为“上一行”数组,另一个数组更新当前行的数
代码如下
#include <iostream>
using namespace std;
int main()
{
int n, ans[20], ANS[20], i, j = 0, x;
cin >> n;
for (i = 0; i < 20; ++i)
{
ans[i] = 1;
ANS[i] = 1;
}
cout << 1 << endl;
for (i = 2; i <= n; ++i)
{
cout << 1 << ' ';
x = i - 1;
for (j = 1; j < x; ++j)
{
cout << ans[j] + ans[j - 1] << ' ';
ANS[j] = ans[j] + ans[j - 1];
}
cout << 1 << endl;
for (j = 1; j <= i; ++j)
{
ans[j] = ANS[j];
}
}
return 0;
}
查错了一个小时,结果错误可能是编译器抽风了......
至于没有使用 fill函数进行赋值,因为发现好像还不如直接for循环的快,具体哪个速度更快,没有具体测试过。
再说一句,fill 函数在 algorithm 头文件里面,用法类似这样
fill ( ans, ans + 20, sizeof ( int ) );