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 ...
随机推荐
- 抓https包
一.charles抓https 1.打开charles,打开Help--SSL Proxy--Install Charles Root Certificate,charles安装证书,傻瓜式安装即可 ...
- => 应用在js回调函数中
=> 可以简化以前的回调函数的调用,具体来说: 今后,几乎所有的回调函数都可用箭头函数简化 比如: 1. 所有回调函数都可: 去function改=> 2. 如果函数体只有一句话: 可省略 ...
- 伪元素:target
:target 伪类选择器 :target 是 CSS3 新增的一个伪类,可用于选取当前活动的目标元素.当 URL 末尾带有锚名称 #,就可以指向文档内某个具体的元素.这个被链接的元素就是目标元素(t ...
- HihoCoder1620: 股票价格3 (单调队列 or DP)
股票价格3 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi最近在关注股票,为了计算股票可能的盈利,他获取了一只股票最近N天的价格A1~AN. 小Hi想知道,对于第 ...
- Python之os.path
os.path模块主要用于常用路径名操作,在编程中使用频率特高 os.path常用函数 abspath(path) 返回路径名path的规范化的绝对路径 split(path) 将path分割成目录和 ...
- 使用OpenCV对图像进行缩放
OpenCV:图片缩放和图像金字塔 对图像进行缩放的最简单方法当然是调用resize函数啦! resize函数可以将源图像精确地转化为指定尺寸的目标图像. 要缩小图像,一般推荐使用CV_INETR_A ...
- VS 生成 dll、exe 版本号与SVN版本号一致
1.VS 可自动生成版本号 注释掉以下两行代码 [assembly: AssemblyVersion("1.0.0.0")][assembly: AssemblyFileVersi ...
- 【java规则引擎】drools6.5.0版本api简介
在有些术语使用的时候,我有时候会用KIE项目.KIE引擎或者Drools项目.Drools引擎,大家应该理解KIE是Drools等项目的一个统称,所以在大多数情况下KIE或者特指Drools都是差不多 ...
- fragment在水平/垂直时的应用
直接看代码 public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedIns ...
- java 执行bat批处理文件 并关闭cmd窗口
java 执行bat批处理文件 并关闭cmd窗口 import java.io.IOException; public class CmdMain { public static void main( ...