1267: Pascal's Triangle of Death
| Result | TIME Limit | MEMORY Limit | Run Times | AC Times | JUDGE |
|---|---|---|---|---|---|
| 3s | 8192K | 358 | 89 | Standard |
In this problem, you are asked to generate Pascal's Triangle. Pascal's Triangle is useful in many areas from probability to polynomials to programming contests. It is a triangle of integers with ``1'' on top and down the sides. Any number in the interior equals the sum of the two numbers above it. For example, here are the first 5 rows of the triangle.
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
In ``Pascal's Triangle of Death,'' you are to generate a left justified Pascal's Triangle. When any number in the triangle is exceeds or equals 1060, your program should finish printing the current row and exit. The output should have each row of the triangle on a separate line with one space between each element.
The final element of each line should be directly followed by a newline. There is no space after the last number on each line.
Sample Input
There is no input for this problem.
Sample Output
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 . . .
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn=70;
class bignum
{
int num[maxn+1];
public:
bignum(){memset(num,0,sizeof(bignum));}
friend bignum operator +(bignum &a,bignum &b);
friend ostream &operator <<(ostream & cout,const bignum &a);
bignum & operator =(int n);
bool operator >(int n)//因为只考虑n=0
{
for(int i=0;i<maxn+1;i++) if(num[i]) return 1;
return 0;
}
};
ostream &operator <<(ostream & cout,const bignum &a)
{
for(int i=0,flag=0;i<maxn+1;i++)
{
if(i==maxn||flag||a.num[i])
{
cout<<a.num[i];flag=1;
}
}
return cout;
}
bignum & bignum::operator =(int n)
{
memset(num,0,sizeof(num));
int i=maxn;
while(n)
{
num[i--]=n%10;
n/=10;
}
}
bignum operator +(bignum &a,bignum &b)
{
bignum temp;
int t=0;
for(int i=maxn;i>=0;i--)
{
temp.num[i]=a.num[i]+b.num[i]+t;
t=temp.num[i]/10;
temp.num[i]%=10;
}
return temp;
}
int main()
{
//freopen("111111.txt","w",stdout);
printf("1/n1 1/n");
bignum t,l,a[210];
a[0]=1;a[1]=1;
for(int ci=2,i;ci<205;ci++)
{
cout<<1;
l=0;
for(i=1;i<ci;i++)
{
t=a[i-1]+a[i];
cout<<" "<<t;
if(l>0) a[i-1]=l;
l=t;
}
a[i-1]=l;
a[ci]=1;
printf(" 1/n");
}
return 0;
}
本文介绍了一个编程挑战,即生成帕斯卡三角形(Pascal's Triangle),当三角形中的任何数字达到或超过10^60时,程序会停止生成。文中提供了一个使用大数处理的C++实现示例。
309

被折叠的 条评论
为什么被折叠?



