HDU ACM Fibonacci
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.
Sample Input
4
5
6
7
100
Sample Output
5=5
6=1+5
7=2+5
100=3+8+89 题解:贪心求解,此题需要注意的是相邻的两个数不能选择;
#include<string.h>
#include<stdio.h>
#include<math.h>
using namespace std;
long long int arr[]={,,};
int main()
{ long long int i,j,a,b[],k,m,l,kk;
for(i=;i<=;i++)
arr[i]=arr[i-]+arr[i-];//先打下表
long long int count=;
while(scanf("%lld",&m)!=-)
{
for(kk=;kk<m;kk++){
scanf("%lld",&a);
k=;
l=;count=;int pp=;
for(i=;i>=;i-=)
{
pp=;
count+=arr[i];
if(count>a)
{
count-=arr[i];
pp=;
}
else if(count==a)
{
k=;
b[l++]=arr[i];
break;
}
else if(count<a)
b[l++]=arr[i];
if(pp==)
i++;//判断一下上一个数是否选择,若没选,下一个数可以选择
}
if(k==)
{
printf("%d=",a);
for(i=l-;i>=;i--)
{ if(i==)
printf("%lld\n",b[i]);
else
printf("%lld+",b[i]);
}
}
else
printf("-1\n");
}
}
return ;
}
HDU ACM Fibonacci的更多相关文章
- HDU 3117 Fibonacci Numbers(围绕四个租赁斐波那契,通过计++乘坐高速动力矩阵)
HDU 3117 Fibonacci Numbers(斐波那契前后四位,打表+取对+矩阵高速幂) ACM 题目地址:HDU 3117 Fibonacci Numbers 题意: 求第n个斐波那契数的 ...
- hdu acm 1028 数字拆分Ignatius and the Princess III
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- hdu 3117 Fibonacci Numbers 矩阵快速幂+公式
斐波那契数列后四位可以用快速幂取模(模10000)算出.前四位要用公式推 HDU 3117 Fibonacci Numbers(矩阵快速幂+公式) f(n)=(((1+√5)/2)^n+((1-√5) ...
- ACM HDU 1021 Fibonacci Again
#include<iostream> using namespace std; int main() { int n; while(cin>>n) { if((n+1)%4== ...
- hdu 4786 Fibonacci Tree (2013ACMICPC 成都站 F)
http://acm.hdu.edu.cn/showproblem.php?pid=4786 Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others) ...
- hdu 1021 Fibonacci Again(找规律)
http://acm.hdu.edu.cn/showproblem.php?pid=1021 Fibonacci Again Time Limit: 2000/1000 MS (Java/Others ...
- HDU 4786 Fibonacci Tree 最小生成树
Fibonacci Tree 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4786 Description Coach Pang is intere ...
- hdu 1021 Fibonacci Again(变形的斐波那契)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1021 Fibonacci Again Time Limit: 2000/1000 MS (Java/Ot ...
- HDU 4786 Fibonacci Tree(生成树,YY乱搞)
http://acm.hdu.edu.cn/showproblem.php? pid=4786 Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others ...
随机推荐
- LeetCode OJ:Jump Game(跳跃游戏)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- NSSet基本使用
int main(int argc, const char * argv[]) { @autoreleasepool { //创建一个集合对象 注:如果集合中写了两次或多次同一个对象 打印只能看到一个 ...
- New Concept English three (42)
21 33 Cave exploration, or pot-holing, as it has come to be known, is a relatively new sport. Perhap ...
- 【SQL查询】视图_view
转自:http://database.e800.com.cn/articles/2009/719/1248015564465_1.html 视图是从一个或几个基本表(或视图)导出的表.它与基本表不同, ...
- 【ES6】箭头函数
let getPrices = () => 4.55 console.log(getPrices()) let arr = ['apple', 'banana', 'orange'] arr.f ...
- 安装PL/SQL客户端来访问操作步骤
1 2 3.改变路径 4 5 6 7 8 9.选择注册机 10
- Web应用程序与Web网站及部署在IIS中
在Visual Studio可以创建 Web 应用程序项目或网站项目.通过选择 新建项目 或 打开项目 创建或打开一个 Web 应用程序项目在Visual Studio 文件 菜单. 通过选择 新建网 ...
- 在TreeView 控件上,如果双击任何一个节点的checkbox 只会收到一次After_Check事件 但是check属性变化两次(从false到true 再从true到false),请问该如何解决,谢谢!
在TreeView 控件上,如果双击任何一个节点的checkbox 只会收到一次After_Check事件 但是check属性变化两次(从false到true 再从true到false),请问该如何解 ...
- shell中把大写字母转换成小写字母
shell中把大写字母转换成小写字母 参考:http://www.jb51.net/article/40257.htm echo "AABBCC" | tr "[:upp ...
- php设计模式之单例(多例),注册器,观察者模式
单例(Singleton)模式和不常见的多例(Multiton)模式控制着应用程序中类的数量.如模式名称,单例只能实例化一次,只有一个对象,多例模式可以多次实例化. 基于Singleton的特性,我们 ...