| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 619 人关注过本帖
标题:纠结了一天,不知道答案,请大家给点思路^_^
只看楼主 加入收藏
hyp_eagle
Rank: 2
等 级:论坛游民
帖 子:11
专家分:21
注 册:2010-10-6
收藏
 问题点数:0 回复次数:1 
纠结了一天,不知道答案,请大家给点思路^_^
Problem Description
Do you know the famous Fibonacci sequence? It is defined by the recurrence
F0 = 0, F1 = 1 and Fn = Fn-1 + Fn-2 for n ≥ 2.
The Fibonacci numbers have many interesting properties. One of them is that the Fibonacci numbers can be used to represent integers. Every positive integer has a unique representation of the form
n = Fk1 + Fk2 + … + Fkm, ki ≥ ki-1 + 2 for 2 ≤ i ≤ m and k1 ≥ 2
For example, 6 can be represented as F2+F5 and 12 can be represented as F2+F4+F6.
Now you know how to represent positive integers with the Fibonacci numbers, can you add them? Given two Fibonacci formed integers, you should calculate the sum of them.
 

Input
The first line contains a single integer T, indicating the number of test cases.
Each test case contains two lines; each line contains a single integer m followed by m integers k1, k2, …, km indicate a Fibonacci formed integer Fk1+Fk2+…+Fkm.
The input will be always correct.

Technical Specification

1. 1 <= T <= 100
2. 1 <= m <= 100
3. 2 <= ki <= 1000000
 

Output
For each test case, output the case number first, then a single line indicates the sum of the two Fibonacci formed integer. The sum should be Fibonacci formed like the input.
 

Sample Input
2
1 2
2 2 4
3 2 4 6
2 2 5
 

Sample Output
Case 1:
1 5
Case 2:
2 5 7
搜索更多相关主题的帖子: 答案 
2011-05-22 19:49
hyp_eagle
Rank: 2
等 级:论坛游民
帖 子:11
专家分:21
注 册:2010-10-6
收藏
得分:0 
我终于找到答案了 原来: 2*F(n) = F(n-2)+F(n+1) ;
程序代码:
#include <iostream>
#include <algorithm>
using namespace std;
int a[500];
void solve(int m)
{
    int i;
    int n;
    int flag=0;
    while(!flag)
    {
        flag=1;
        n=m;
        for(i=0;i<(n-1);i++)
        {
            if(a[i]==0) continue;
            if(a[i]==2 && a[i]==a[i+1])
            {
                a[i]=0;
                a[i+1]++;
                flag=0;
            }
            else if(a[i]==3 && a[i]==a[i+1])
            {
                a[i]=2;
                a[i+1]=4;
                flag=0;
            }
            else if(a[i]==a[i+1])
            {
                a[i]=a[i]-2;
                a[i+1]++;
                flag=0;
            }
            else if((a[i+1]-a[i])==1)
            {
                a[i+1]++;
                a[i]=0;
                flag=0;
            }
            else if((a[i+1]-a[i])==-1)
            {
                a[i+1]+=2;
                a[i]=0;
                flag=0;
            }
        }
        sort(a,a+m);
    }
    for(i=0;i<m;i++)
    {
        if(a[i]) break;
    }
    printf("%d",m-i);
    for(;i<m;i++)
    {
        printf(" %d",a[i]);
    }
    printf("\n");
}

int main()
{
    int T,i,j;
    int m1,m2;
    scanf("%d",&T);
    for(i=1;i<=T;i++)
    {
        scanf("%d",&m1);
        for(j=0;j<m1;j++)
        {
            scanf("%d",&a[j]);
        }
        scanf("%d",&m2);
        for(j=0;j<m2;j++)
        {
            scanf("%d",&a[m1]);
            m1++;
        }
        sort(a,a+m1);
        printf("Case %d:\n",i);
        solve(m1);
    }
    return 0;
}
2011-05-24 16:58
快速回复:纠结了一天,不知道答案,请大家给点思路^_^
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.085723 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved