TOJ 3015.Convert Kilometers to Miles

题目链接 : http://acm.tju.edu.cn/toj/showp3015.html

This year, Bruce Force spends his vacation in Flagstaff, Arizona, where he wants to practice for his next half marathon (a race over 21 km). At his first training he runs to his friend Greedy Gonzales’ home which is 21 miles away from Flagstaff.
Arriving there, he is very tired and realizes that 21 miles are much more than 21 km. Greedy Gonzales tells him that 21 km equals 13 miles. 21, 13? Bruce realizes immediately that there must be a deeper relation! Both, 13 and 21 are Fibonacci numbers!

Fibonacci numbers can be defined as follows:
······

比较水的一道题目,题目挺长的读前几行愣是没读懂啥意思(大概就是说某某某要马拉松然后发现长度和他想的不一样然后莫名其妙的就想到了斐波那契数列并莫名其妙的要进行转化。。。),不过不要紧看题目给出的例子很好理解,就是把一个数用斐波那契数列为底表示出来,然后把最右边的那一位去掉,在用菲波那切数列转化回去,类似进制一样,很简单,直接上代码。

#include<stdio.h>
#include<cstring>
using namespace std;
int main()
{
    int fib[23],tmp[23],t,x,sum;
    fib[1]=1;fib[2]=2;
    for(int i=3;i<23;i++)
        fib[i]=fib[i-1] + fib[i-2];
    //printf("%d\n",fib[22]);
    scanf("%d",&t);
    while(t--){
        scanf("%d",&x);
        memset(tmp,0,sizeof(tmp));
        for(int i=22;i>=1;i--)
            if(x>=fib[i]){
                x-=fib[i];tmp[i]=1;
            }
        sum=0;
        for(int i=2;i<23;i++)
            if(tmp[i])
                sum+=fib[i-1];
        printf("%d\n",sum);
    }
 } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值