Fibonacci
Time Limit: 2000 ms / Memory Limit: 131072 kb
Description
Fibonacci numbers are well-known as follow:

Now given an integer N, please find out whether N can be represented as the sum of several Fibonacci numbers in such a way that the sum does not include any two consecutive Fibonacci numbers.
Input
Multiple test cases, the first line is an integer T (T<=10000), indicating the number of test cases.
Each test case is a line with an integer N (1<=N<=109).
Output
One line per case. If the answer don’t exist, output “-1” (without quotes). Otherwise, your answer should be formatted as “N=f1+f2+…+fn”. N indicates the given number and f1, f2, … , fn indicating the Fibonacci numbers in ascending order. If there are multiple ways, you can output any of them.
Sample Input
4567100
Sample Output
5=56=1+57=2+5100=3+8+89
Source
“浪潮杯”山东省第七届ACM大学生程序设计竞赛
#include <stdio.h>
#include <string.h>
int fib[45]={0,1,2};
int Find(int x){
for(int i=44;i>=1;i--){
if(x >= fib[i]){
return i;
}
}
return 0;
}
int main(){
int t,n;
int ans[45];
for(int i=3;i<45;i++){
fib[i]=fib[i-1]+fib[i-2];
}
//printf("%d\n",fib[44]);
scanf("%d",&t);
while(t--){
scanf("%d",&n);
int s=0,j=0,d;
while(s!=n){
d=Find(n-s);
s+=fib[d];
ans[j]=d;
j++;
}
j--;
printf("%d=%d",n,fib[ans[j]]);
for(j--;j>=0;j--){
printf("+%d",fib[ans[j]]);
}
printf("\n");
}
return 0;
}
本文介绍了一个关于斐波那契数求和的问题,即如何将一个整数表示为若干个非连续斐波那契数之和。通过预计算前45个斐波那契数并运用一种特定的算法,文章给出了一个有效的解决方案。

1071

被折叠的 条评论
为什么被折叠?



