山东省 第七届 ACM省赛 Fibonacci

Fibonacci

Time Limit: 2000 ms / Memory Limit: 131072 kb

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
4567100
Sample Output
5=56=1+57=2+5100=3+8+89
Source
 “浪潮杯”山东省第七届ACM大学生程序设计竞赛


这道题的题意不难,就是给你一个数,让你输出这个数字是用那些斐波那契数的和所组成的,N的范围到10^9,本来以为需要很大的数组,输出试了一下,45就可以,都没到50.
打表求得前45个斐波那契数,然后,看大神们的博客,然后不难发现这些数字的组合有一个规律,比如100,比他小的第一个斐波那契数是89,然后100-89=11,比11小的第一个斐波那契数是8,就这样,总会找出一些斐波那契数的和与原数相等,输出~
ac代码:
#include <stdio.h>
#include <string.h>

int fib[45]={0,1,2};

int Find(int x){
    for(int i=44;i>=1;i--){
        if(x >= fib[i]){
            return i;
        }
    }
    return 0;
}

int main(){
    int t,n;
    int ans[45];

    for(int i=3;i<45;i++){
        fib[i]=fib[i-1]+fib[i-2];
    }
    //printf("%d\n",fib[44]);
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        int s=0,j=0,d;
        while(s!=n){
            d=Find(n-s);
            s+=fib[d];
            ans[j]=d;
            j++;
        }
        j--;
        printf("%d=%d",n,fib[ans[j]]);
        for(j--;j>=0;j--){
            printf("+%d",fib[ans[j]]);
        }
        printf("\n");
    }



    return 0;
}





大牛们都是这么解的。那小编就不发表自己的观点了。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值