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. PHP和HTML代码混合编译的三种方法

    第一种是在HTML中加PHP. 大段大段的html代码中,在各个需要执行php的地方<?php .... ?> 比如 line7-9: 1 <head> 2 <meta ...

  2. System.Data.SqlClient.SqlException: 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: SQL Network Interfaces, error: 26 - 定位指定的服务器/实例时出错)

    A network-related or instance-specific error occurred while establishing a connection to SQL Server. ...

  3. MS SQL数据类型比较

    字符串数据类型 char此数据类型可存储1~8000个定长字符串,字符串长度在创建时指定:如未指定,默认为char(1).每个字符占用1byte存储空间.nchar此数据类型可存储1~4000个定长U ...

  4. LeetCode Power of Four

    原题链接在这里:https://leetcode.com/problems/power-of-four/ 题目: Given an integer (signed 32 bits), write a ...

  5. CSS样式常用属性整理

    web工程师是最近5年刚刚兴起的一门高薪职业,人们的专注度越来越高. 那么前端除了学习html标签之外还需要掌握什么知识点呢? 为大家整理了一个和HTML标签密不可分的知识要点--<CSS样式常 ...

  6. nginx 配置优化(简单)

    配置文件     正常运行的必备配置:         1.user username [groupname]:(推荐nginx)         以那个用户身份运行,以在configure指定的用户 ...

  7. C学习笔记 知识集锦(一)

    1.标识符  2.寄存器变量 3.全局变量 4.分配内存与初始化 5.变量定义与声明 6.作用域规则 跳转语句 7.&与&&,|和||的意义与区别 8.如何选择switch c ...

  8. SpringMVC常用配置-处理程序异常以及404错误

  9. [PCL]点云渐进形态学滤波

    PCL支持点云的形态学滤波,四种操作:侵蚀.膨胀.开(先侵蚀后膨胀).闭(先膨胀后侵蚀) 在#include <pcl/filters/morphological_filter.h>中定义 ...

  10. 使用Photoshop不改变图片尺寸,保存图片到30K以下的解决办法