大整数相加与斐波那契数列

斐波那契数列(Fibonacci sequence),又称黄金分割数列、因数学家莱昂纳多·斐波那契(Leonardoda
Fibonacci)以兔子繁殖为例子而引入,故又称为“兔子数列”,指的是这样一个数列:0、1、1、2、3、5、8、13、21、34、……在数学上,斐波那契数列以如下被以递推的方法定义:F(0)=0,F(1)=1,F(2)=1,
F(n)=F(n - 1)+F(n - 2)(n ≥ 3,n ∈
N*)在现代物理、准晶体结构、化学等领域,斐波纳契数列都有直接的应用,为此,美国数学会从 1963
年起出版了以《斐波纳契数列季刊》为名的一份数学杂志,用于专门刊载这方面的研究成果。

大数相加意味着,long long int也容不下,所以需要通过其他思路完成相加。比如使用数组a,10个数分割放在数组中,即从a[0]至a[9]。

这里用的是int型数组来完成的大数相加,也可以使用char型数组,这道题并不难,但不知为何就是想不明白怎么连续进位,慢慢通过调试程序,一点一点改程序,才终于得到了如下AC代码(先发个题)。

链接:https://ac.nowcoder.com/acm/contest/7818/J 来源:牛客网

The State Veterinary Services Department recently reported an outbreak of a newly found cow disease. All cows found to have affected by the disease have since euthanized because of the risk to the industry. Number of affected cows increased to 21, 34 and reached 55 after eight, nine and ten hours respectively.
You have been assigned by the authority to study the pattern of the outbreak and develop a program to predict the next number of affected cows so that the authorities could prepare and act accordingly.
输入描述:
Input will consist of a series of positive integer numbers no greater than 490 each on a separate line represent the hours. The input process will be terminated by a line containing -1.
输出描述:
For each input value, the output contains a line in the format: Hour X:Y cow(s) affected \text { Hour } \mathbf{X}: \mathbf{Y} \text { cow(s) affected } Hour X:Y cow(s) affected , where X\mathbf{X}X is the hour, and Y\mathbf{Y}Y is the total affected cows that need to be euthanized based on the hour given by X\mathbf{X}X.

示例

输入
1
4
6
11
-1

输出
Hour: 1: 1 cow(s) affected
Hour: 4: 3 cow(s) affected
Hour: 6: 8 cow(s) affected
Hour: 11: 89 cow(s) affected

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;
int main()
{
    int i,j,n,t,k,p;
    int a[500][500];    //用于计算斐波那契数列
    int b[500][500];    //用于逆转a数组
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    a[0][0]=0;
    b[1][0]=a[1][0]=1;
    b[1][1]=-1;
    k=0;
    for(i=2;i<=495;i++)     //题目要求求出的第n个斐波那契数的n<=490
    {
        for(j=0;j<=k;j++)
        {
            a[i][j]+=a[i-1][j]+a[i-2][j];
            if(a[i][k]>9)      //k代表目前的最高位数
                k++;
            if(a[i][j]>9)       //进位计算
            {
                a[i][j+1]+=1;
                a[i][j]%=10;
            }
        }
        for(p=0;p<=k;p++)       //逆转
            b[i][p]=a[i][k-p];
        b[i][p]=-1;     //设置停止条件,
    }
    while(1)
    {
        scanf("%d",&n);
        if(n==-1)
            break;
        printf("Hour: %d: ",n);
        for(i=0;;i++)
        {
            if(b[n][i]==-1)
                break;
            printf("%d",b[n][i]);
        }
        printf(" cow(s) affected\n");
    }
    return 0;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值