题目链接 : http://acm.tju.edu.cn/toj/showp3015.html
This year, Bruce Force spends his vacation in Flagstaff, Arizona, where he wants to practice for his next half marathon (a race over 21 km). At his first training he runs to his friend Greedy Gonzales’ home which is 21 miles away from Flagstaff.
Arriving there, he is very tired and realizes that 21 miles are much more than 21 km. Greedy Gonzales tells him that 21 km equals 13 miles. 21, 13? Bruce realizes immediately that there must be a deeper relation! Both, 13 and 21 are Fibonacci numbers!Fibonacci numbers can be defined as follows:
······
比较水的一道题目,题目挺长的读前几行愣是没读懂啥意思(大概就是说某某某要马拉松然后发现长度和他想的不一样然后莫名其妙的就想到了斐波那契数列并莫名其妙的要进行转化。。。),不过不要紧看题目给出的例子很好理解,就是把一个数用斐波那契数列为底表示出来,然后把最右边的那一位去掉,在用菲波那切数列转化回去,类似进制一样,很简单,直接上代码。
#include<stdio.h>
#include<cstring>
using namespace std;
int main()
{
int fib[23],tmp[23],t,x,sum;
fib[1]=1;fib[2]=2;
for(int i=3;i<23;i++)
fib[i]=fib[i-1] + fib[i-2];
//printf("%d\n",fib[22]);
scanf("%d",&t);
while(t--){
scanf("%d",&x);
memset(tmp,0,sizeof(tmp));
for(int i=22;i>=1;i--)
if(x>=fib[i]){
x-=fib[i];tmp[i]=1;
}
sum=0;
for(int i=2;i<23;i++)
if(tmp[i])
sum+=fib[i-1];
printf("%d\n",sum);
}
}