序列

Description
数列A满足An = An-1 + An-2 + An-3, n >= 3

编写程序,给定A0, A1 和 A2, 计算A99
Input
输入包含多行数据

每行数据包含3个整数A0, A1, A2 (0 <= A0, A1, A2 <= 32767)
数据以EOF结束
Output
对于输入的每一行输出A99的值
Sample Input
1 1 1
Sample Output
69087442470169316923566147

看到样例就应该知道是一个大数模拟。想起之前cf一道题上,我还傻乎乎的用了大数模拟,其实long long完全装得下。一看这么多位数,吓得我开了4个long long,然而有的时候后几位开头可能是0,因此需要用printf(“%.10I64d”,a[4]);的姿势。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
long long a[4], b[4], c[4], d[4], temp, pre;
int main()
{
    int i, j, m, n, ans, a0, a1, a2;
    temp = 1;
    for (i = 1; i <= 10; i++)
        temp = temp * 10;
    while (scanf("%d%d%d", &a0, &a1, &a2) != EOF)
    {
        memset(a, 0, sizeof(a));
        memset(b, 0, sizeof(b));
        memset(c, 0, sizeof(c));
        memset(d, 0, sizeof(d));
        d[0] = a0; d[1] = a1; d[2] = a2;
        for (i = 3; i <= 99; i++)
        {
            d[3] = d[0] + d[1] + d[2];
            pre = 0;
            if (d[3] >= temp)
            {
                pre = d[3] / temp;
                d[3] = d[3] - pre* temp;
            }
            c[3] = c[0] + c[1] + c[2] + pre;
            pre = 0;
            if (c[3] >= temp)
            {
                pre = c[3] / temp;
                c[3] = c[3] - pre* temp;
            }
            b[3] = b[1] + b[2] + b[0] + pre;
            pre = 0;
            if (b[3] >= temp)
            {
                pre = b[3] / temp;
                b[3] = b[3] - pre* temp;
            }
            a[3] = a[0] + a[1] + a[2] + pre;
            a[0] = a[1]; b[0] = b[1]; c[0] = c[1]; d[0] = d[1];
            a[1] = a[2]; b[1] = b[2]; c[1] = c[2]; d[1] = d[2];
            a[2] = a[3]; b[2] = b[3]; c[2] = c[3]; d[2] = d[3];
        }
        if (a[3])
        {
            cout << a[3];
            printf("%.10I64d", b[3]);
            printf("%.10I64d", c[3]);
            printf("%.10I64d", d[3]);
            cout << endl;
        }
        else if (b[3])
        {
            cout << b[3];
            printf("%.10I64d", c[3]);
            printf("%.10I64d", d[3]);
            cout << endl;
        }
        else if (c[3])
        {
            cout << c[3];
            printf("%.10I64d", d[3]);
            cout << endl;
        }
        else
            cout << d[3] << endl;

    }

    return 0;
}

看到学长的java简单的通过,心中十万头草泥马……

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值