山東省第7屆省賽 B題 Fibonacci

來源:互聯網
上載者:User
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遞減就ok了 ACcode:
#include <bits/stdc++.h>#define maxn 1005#define mod 1000000007#define ll long longusing namespace std;ll dp[55],ans[55];int main(){    dp[0]=dp[1]=1;    dp[2]=2;    for(int i=3;;++i){        dp[i]=dp[i-1]+dp[i-2];        if(dp[i]>1e9)break;    }    int n,loop;    scanf("%d",&loop);    while(loop--){        scanf("%d",&n);        printf("%d=",n);        int i=44,tot=0;        while(n){            if(n>=dp[i]){                ans[++tot]=dp[i];                n-=dp[i];            }            --i;        }    for(int i=tot;i>1;--i)cout<<ans[i]<<"+";    cout<<ans[1]<<'\12';    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.