LightOJ-1105 Fi Binary Number(斐波那契数列)

F - Fi Binary Number
Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

Description

A Fi-binary number is a number that contains only 0 and 1. It does not contain any leading 0. And also it does not contain 2 consecutive 1. The first few such number are 1, 10, 100, 101, 1000, 1001, 1010, 10000, 10001, 10010, 10100, 10101 and so on. You are given n. You have to calculate the nth Fi-Binary number.

Input

Input starts with an integer T (≤ 10000), denoting the number of test cases.

Each case contains an integer n (1 ≤ n ≤ 109).

Output

For each case, print the case number and the nth Fi-Binary number

Sample Input

4

10

20

30

40

Sample Output

Case 1: 10010

Case 2: 101010

Case 3: 1010001

Case 4: 10001001


题意: Fi-binary number 是只含0和1且没有前导零的数,并且2个1不能连续出现,问第n个Fi-binary number 是什么
题解:其实如果列出前几个数的话可以发现这其实就是个斐波那契数列,设数的长度为len,那么该长度下Fi-binary number 的个数就是f[len],
2个1不能连续出现那是因为这样的话就可以合成下一个斐波那契数了,f[n]=f[n-1]+f[n-2]
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 100;
const int tot = 1e9;
int a[maxn];
int main(){
    int n=2,T,x;
    a[1]=a[2]=1;
    while(a[n]<=tot){
        n++;
        a[n]=a[n-2]+a[n-1];
    }
   // freopen("in.txt","r",stdin);
    scanf("%d",&T);
    for(int cas=1;cas<=T;cas++){
        printf("Case %d: ",cas);
        scanf("%d",&x);
        int pos=n,flag=1;
        while(pos>1){
            if(flag){
                if(a[pos]<=x){
                    x-=a[pos];
                    flag=0;
                    printf("1");
                }
            }
            else{
                if(a[pos]<=x) {
                    x-=a[pos];
                    printf("1");
                }
                else printf("0");
            }
            pos--;
        }
        printf("\n");
    }
    return 0;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值