1267: Pascal's Triangle of Death

本文介绍了一个编程挑战,即生成帕斯卡三角形(Pascal's Triangle),当三角形中的任何数字达到或超过10^60时,程序会停止生成。文中提供了一个使用大数处理的C++实现示例。

 1267: Pascal's Triangle of Death


ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE
3s8192K35889Standard

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
.
.
.
etc.
#include<iostream>
#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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值