3982 序列

序列

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


代码:

#include <iostream>

#include <string.h>
#include<stdio.h>


using namespace std;
const int maxn = 10000+10;
int dp[101][maxn];
int count[101];  //记录大数的位数
void solve()
{
    int t = 0;
    count[0] = count[1] = count[2] = 5;//前三个数的位数最大为5;
    for(int i = 3; i < 100; i++)
    {
        t = 0;
        int tmp_count1 = count[i-1], tmp_count2 = count[i-2];
        int tmp_count3 = count[i-3];//定义临时变量,记录大数的位数
        for(int j = 100; tmp_count1 || tmp_count2 || tmp_count3 || t; j--)//如果临时变量的位数都为0且进位=0,结束循环
        {
            if(tmp_count1 > 0)tmp_count1--;
            if(tmp_count2 > 0)tmp_count2--;
            if(tmp_count3 > 0)tmp_count3--;//如果位数不等于0,位数减1;
            count[i]++;//位数加一
            int x = dp[i-1][j]+dp[i-2][j]+dp[i-3][j]+t;
            dp[i][j] = x%10;
            t = x/10;
        }
    }
}
int main()
{
    int x = 0, y = 0, z = 0;
    while(scanf("%d%d%d",&x,&y,&z)!=EOF)
    {
        memset(dp,0,sizeof(dp));
        memset(count,0,sizeof(count));
        dp[0][100] = x; dp[1][100] = y;
        dp[2][100] = z;
        solve();
        int i = 0;
        //for(i = 0; !dp[99][i]; i++);
        if(dp[0][100] == 0 &&(dp[1][100] == 0) &&(dp[2][100] == 0))
        {
            printf("0\n");continue;
        }
        for(i = 0; i <= 100; i++)
        {
            if(dp[99][i] != 0)break;
        }
        for(; i <=100; i++)
        {
            printf("%d",dp[99][i]);
        }
        printf("\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值