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

来源

 “浪潮杯”山东省第七届ACM大学生程序设计竞赛

题意

这道题的题意也很简单,就是给你一个数,让你输出这个数字是用那些斐波那契数的和所组成的,N的范围到10^9,也就是第45个斐波那契数左右吧!
打表求得前45个斐波那契数,然后,然后不难发现这些数字的组合有一个规律,比如100,比他小的第一个斐波那契数是89,然后100-89=11,比11小的第一个斐波那契数是8,就这样,总会找出一些斐波那契数的和与原数相等,保存在数组中,输出~
为什么每次比赛结束之后才会发现题目原来这么简单,

AC代码:
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
using namespace std;
int a[46]= {1,1};
int b[46];
void init()
{
    for(int i=2; i<46; i++)
        a[i]=a[i-1]+a[i-2];
}
int find(int n)
{
    for(int i=1; i<46; i++)
        if(a[i]>n)return a[i-1];
    return 0;
}
int main()
{
    init();
    int N;
    cin>>N;
    while(N--)
    {
        int n;
        cin>>n;
        int s=0,j=0;
        memset(b,0,sizeof(b));
        while(s!=n)
        {
            int d=find(n-s);
            b[j++]=d;
            s+=find(n-s);
        }
        printf("%d=%d",n,b[--j]);
        for(--j; j>=0; j--)
            printf("+%d",b[j]);
        printf("\n");
    }
}

山东省第七届ACM省赛------Fibonacci的更多相关文章

  1. 山东省第七届ACM省赛------Memory Leak

    Memory Leak Time Limit: 2000MS Memory limit: 131072K 题目描述 Memory Leak is a well-known kind of bug in ...

  2. 山东省第七届ACM省赛------Reversed Words

    Reversed Words Time Limit: 2000MS Memory limit: 131072K 题目描述 Some aliens are learning English. They ...

  3. 山东省第七届ACM省赛------Triple Nim

    Triple Nim Time Limit: 2000MS Memory limit: 65536K 题目描述 Alice and Bob are always playing all kinds o ...

  4. 山东省第七届ACM省赛------The Binding of Isaac

    The Binding of Isaac Time Limit: 2000MS Memory limit: 65536K 题目描述 Ok, now I will introduce this game ...

  5. 山东省第七届ACM省赛------Julyed

    Julyed Time Limit: 2000MS Memory limit: 65536K 题目描述 Julyed is preparing for her CET-6. She has N wor ...

  6. 山东省第七届ACM省赛

    ID Title Hint A Julyed 无 B Fibonacci 打表 C Proxy 最短路径 D Swiss-system tournament 归并排序 E The Binding of ...

  7. 山东省第十届ACM省赛参赛后的学期总结

    5.11,5.12两天的济南之旅结束了,我也参加了人生中第一次正式的acm比赛,虽然是以友情队的身份,但是我依旧十分兴奋. 其实一直想写博客来增加自己的能力的,但是一直拖到现在,正赶上老师要求写一份总 ...

  8. Rectangles(第七届ACM省赛原题+最长上升子序列)

    题目链接: http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=1255 描述 Given N (4 <= N <= 100)  rec ...

  9. 山东理工大学第七届ACM校赛-LCM的个数 分类: 比赛 2015-06-26 10:37 18人阅读 评论(0) 收藏

    LCM的个数 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 对于我们来说求两个数的LCM(最小公倍数)是很容易的事,现在我遇到了 ...

随机推荐

  1. Vim,极简使用教程,让你瞬间脱离键鼠切换的痛苦

    注:看大家对Vim仇恨极大,其实它只是一种文本操作方式,可以减少键鼠的切换,从而让编辑文本的操作更迅捷.并不等同于IDE,在我看来,它们是两个是包含关系,IDE可以有Vim编辑模式.Vim或许可以通过 ...

  2. escape,encodeURI,encodeURIComponent的区别

    escape是对字符串进行编码而另外两种是对URL. encodeURI方法不会对下列字符编码 ASCII字母 数字 ~!@#$&*()=:/,;?+'encodeURIComponent方法 ...

  3. UML之类图

    类(Class)封装了数据和行为,是具有相同属性.操作.关系的对象集合的总称. 类图(Class Dialog)使用系统中不同类来描述系统的静态结构,类图用来描述不同类和它们之间的关系. 类图由三部分 ...

  4. mongodb权限管理

    说到mongodb就得先谈谈mongodb的用户组,和传统的关系型数据库不一样,mongodb并没有在创建应用时就要求创建权限管理组,所以类似于Robomongo这样的数据库可视化工具在创建conne ...

  5. SQLSERVER2012 Audit (审核)功能

    数据库表结构和数据有时会被无意或者恶意,或者需要追踪最近的数据结构变更记录,以往必须通过日志查询,SQL Server2008开始提供了 审核(Audit )功能,SQL2012有所升级,利用它可以实 ...

  6. ax Mail

    SysMailer mailer = new SysMailer(); SysEmailParameters parameters = SysEmailParameters::find(); ; tr ...

  7. oracle 函数调用

    --带out的函数 create or replace function fun_try(v_name varchar,v_outname out varchar)return varchar2 is ...

  8. imx6 matrix keyboard

    imx6需要添加4x4的矩阵键盘.本文记录添加方法. 参考链接 http://processors.wiki.ti.com/index.php/TI-Android-JB-PortingGuide h ...

  9. sort

    sort -n -k2 filename 按第二列,按顺序排列 sort -nr -k2 filename 按第二列,按反向顺序排列

  10. 返回值是TEXT的阿贾克斯方法

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...