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; }