B Fibonacci

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.

输入

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).

输出

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.

示例输入

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.

输入

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).

输出

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.

示例输入

4
5
6
7
100

示例输出

5=5
6=1+5
7=2+5
100=3+8+89
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值