Fibonacci
Time Limit: 2000MS Memory limit: 131072K
题目描述
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.
输入
Each test case is a line with an integer N (1<=N<=109).
输出
示例输入
4567100
示例输出
5=56=1+57=2+5100=3+8+89
超级水题 少加一个0,改了好长时间,没发现悲催
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
int n;
int F[100];
F[0]=1;
F[1]=1;
int i,k;
for(i=2;i<=60;i++)
{
F[i]=F[i-1]+F[i-2];
if(F[i]>=1000000000)break;//少加一个0,改了好长时间
}
k=i;
scanf("%d ",&n);
while(n--)
{
int a,b[200],j=0,q,p=1;
memset(b,0,sizeof(b));
scanf("%d",&a);
q=a;
while(a!=0)
{
for(i=1;i<=k;i++)
if(a<F[i])break;
j++;
a-=F[i-1];
b[j]=F[i-1];
if(a<1)break;
}
for(i=1;i<j;i++)
if(b[i]==b[i+1])p=0;
if(a<0)printf("-1");
else if(p==0)printf("-1");
else{
printf("%d=",q);
if(j==1)printf("%d\n",q);
else {
printf("%lld",b[j]);
j--;
for(;j>1;j--)
printf("+%d",b[j]);
printf("+%d\n",b[1]);}
}
}
return 0;
}