ustc_1003_Fibonacci Numbers

来源:互联网 发布:社交网络在线观看 编辑:程序博客网 时间:2024/05/22 16:59
More than Fibonacci
Time Limit: 1000ms
Memory Limit: 65536kb
Description
The New Fibonacci numbers (0, 1, 2, 3, 6, 11, 20, 37, 68, ...) are defined by the recurrence:
           F(0) = 0; F(1) = 1; F(2) = 2;            F(n) = F(n-1) + F(n-2) + F(n-3)   for all n>2
Write a program to calculate the New Fibonacci numbers.
Input
The input is a sequence of integers not more than 36, each on a separate line, specifying which New Fibonacci number to calculate.
Output
Print the New Fibonacci numbers in the format shown in Sample Output.
Sample Input
5711
Sample Output
The New Fibonacci number for 5 is 11The New Fibonacci number for 7 is 37The New Fibonacci number for 11 is 423
很简单的题,注意输出就可以了。。
#include<cstdio>#include<iostream>#include<cstring>using namespace std;int main(){   long  long int a,b,c,d;    long long int resu;    while(cin>>a)   {resu=0;        if(a==0)        cout<<"The New Fibonacci number for 0 is 0"<<endl;        else if(a==1)        cout<<"The New Fibonacci number for 1 is 1"<<endl;        else if(a==2)        cout<<"The New Fibonacci number for 2 is 2"<<endl;        else if(a>=3)        {            int temp=a;            b=0;            c=1;            d=2;            a=a-2;            while(a--)            {                resu=b+c+d;                b=c;                c=d;                d=resu;            }            cout<<"The New Fibonacci number for "<<temp<<" is "<<resu<<endl;        }    }    return 0;}

0 0
原创粉丝点击