山东省第七届ACM大学生程序设计竞赛 B Fibonacci

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.

示例输入

4567100

示例输出

5=56=1+57=2+5100=3+8+89

超级水题 少加一个0,改了好长时间,没发现悲催

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    int n;
     int F[100];
    F[0]=1;
    F[1]=1;
    int i,k;
    for(i=2;i<=60;i++)
    {
        F[i]=F[i-1]+F[i-2];
        if(F[i]>=1000000000)break;//少加一个0,改了好长时间
    }
    k=i;
    scanf("%d ",&n);
    while(n--)
    {
         int a,b[200],j=0,q,p=1;
        memset(b,0,sizeof(b));
        scanf("%d",&a);
        q=a;
        while(a!=0)
        {
            for(i=1;i<=k;i++)
                if(a<F[i])break;
                j++;
            a-=F[i-1];
            b[j]=F[i-1];
            if(a<1)break;
        }
        for(i=1;i<j;i++)
            if(b[i]==b[i+1])p=0;
        if(a<0)printf("-1");
        else if(p==0)printf("-1");
        else{
                printf("%d=",q);
                if(j==1)printf("%d\n",q);
            else {
                    printf("%lld",b[j]);
                     j--;
                    for(;j>1;j--)
                printf("+%d",b[j]);
            printf("+%d\n",b[1]);}
        }


    }
    return 0;


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值