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).
输出
示例输入
4
5
6
7
100
示例输出
5=5
6=1+5
7=2+5
100=3+8+89
题意:给出一个数 , 这个数是 斐波那契数列中的数 的和, 按斐波那契数列的顺序输出, 同时这些数不可以相邻。
因为数据最大是 10^9, 打个斐波那契数列数列的表, 就45个, 然后暴力搜索, 每个数都可以表示成斐波那契数 的和, 所以不存在输出 -1 的情况。
当时在做题的时候一点儿也没想到, 然后队友用搜索做的, 而且当时以为任一种顺序都可以, 提交WA后改了好几次都没想到哪错了, 幸亏最后又看了一遍题,过了,以后一定要仔细读完题, 代码如下:
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
using namespace std;
int a[46];
int result[46];
int vis[47];
int main(){
int T, i, N;
a[1] = 1;
a[2] = 2;
for(i = 3; i < 46; ++i)
a[i] = a[i - 1] + a[i - 2];
cin >> T;
while(T--){
cin >> N;
cout << N << "=";
memset(vis, 0, sizeof(vis));
memset(result, 0, sizeof(result));
int k = 1;
for(i = 45; i > 0; --i){
if(N >= a[i] && vis[i - 1] == 0 && vis[i + 1] == 0){
vis[i] = 1;
N -= a[i];
result[k++] = a[i];
if(N == 0)
break;
}
}
for(i = --k; i > 1; --i){
cout << result[i] << "+";
}
cout << result[1] << endl;
}
return 0;
}
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).
输出
示例输入
4 5 6 7 100
示例输出
5=5 6=1+5 7=2+5 100=3+8+89