Fibonacci
Time Limit: 2000MS Memory Limit: 131072KB
Problem 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.
Example Input
4567100
Example Output
5=56=1+57=2+5100=3+8+89
Hint
Author
“浪潮杯”山东省第七届ACM大学生程序设计竞赛
题意:判断所给的数能不能由斐波那契数组成,如果能输出任意一种组成形式,不能输出-1
#include <stdio.h> long long num[10000]; long long count[100]; int main (){ num[1]=1; num[2]=2; for (int i=3;i<=50;i++){ num[i]=num[i-1]+num[i-2]; } int n; scanf ("%d",&n); while (n--){ long long T; scanf("%lld",&T); int k=0; long long c=T; for(int i=50;i>=0;i--){ if (num[i]<=T&&T){ count[k++]=num[i]; T=T-num[i]; } } if (!T){ printf ("%lld=",c); for (int i=k-1;i>0;i--){ printf ("%lld+",count[i]); } printf ("%lld\n",count[0]); } else{ printf ("-1"); } } return 0; }