zoukankan      html  css  js  c++  java
  • HDU 2370 || SDUT 2382 Convert Kilometers to Miles(简单题)

    Convert Kilometers to Miles

    Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 394    Accepted Submission(s): 276

    Problem Description

    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: 

    F1 = 1 
    F2 = 2 
    Fn+1 = Fn+Fn-1 for n>1. 

    Bruce has just learned about the Fibonacci number system at his university. Each positive integer x can be written as the sum of different Fibonacci numbers, so this means that there exists numbers k and b1, b2, ..., bk such that x = ∑i=1..k bi * Fi, where bk = 1 and bi (1 ≤ i < k) is either 0 or 1. In short, we can write the representation as: b(x) = (bk, bk-1, ..., b1). To make the representation unique, we require that bi * bi-1 = 0 for all i > 1. 

    For example 21 can be represented as (1,0,0,0,0,0,0) and 13 as (1,0,0,0,0,0) in the Fibonacci system. Bruce notices that one can convert a distance x in kilometers into a corresponding distance y to miles as follows: First, write down x in its Fibonacci system representation b(x). Second, shift the bits of b(x) one position to the right (the last bit is deleted) and get b(y). Third, calculate y from b(y) by evaluating the sum given above. 

    For example, the number 42 written in the Fibonacci system is (1,0,0,1,0,0,0,0). In step two we would shift the bits one position to the right and get (1,0,0,1,0,0,0). In the third step, we would calculate 0*1 + 0*2 + 0*3 + 1*5 + 0*8 + 0*13 + 1*21 = 26. 

    Now it's your turn to write a program for Bruce that converts kilometers into miles according to Bruce's algorithm. 

     

    Input

    The first line of the input contains t, the number of distances Bruce wants to convert from kilometers to miles (0<t<25000). Each of the next t lines contains an integer distance x (2 < x < 25000) in kilometers. 

     

    Output

    For each distance x in kilometers output the distance y in miles calculated according to Bruce's algorithm.

     

    Sample Input

    5

    42

    100

    180

    300

    360

     

    Sample Output

    26

    62

    111

    185

    222

     

    Source

    2008水题公开赛(比速度,OJ压力测试)

    Recommend

    lcy

     解题报告:这道题是告诉我们一个数可以由Fibonacci数列得某些数相加而得到,例如42 = 5 + 21 = F(5) +F(8);这样42可以用(1,0,0,1,0,0,0,0)表示即第四位和第七位为1,再去除最后一位,再按照规律计算即可;

    代码如下:

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    using namespace std;
    const int N = 25010;
    int a[N], f[26], ans[N], flag[26];//flag数组记录的是01序列
    void Fibonacci()//先算出Fibonacci数列
    {
    int i;
    f[1] = 1;
    f[2] = 2;
    for (i = 3; i <= 26; ++i)
    {
    f[i] = f[i - 1] +f[i - 2];
    }
    }
    int main()
    {
    int n, i, j, p;
    scanf("%d", &n);
    memset(ans, 0, sizeof(ans));
    Fibonacci();
    for (i = 1; i <= n; ++i)
    {
    memset(flag, 0, sizeof(flag));
    scanf("%d", &a[i]);
    for(j = 1; j <= 25; ++j)//找到第一个大于等于它本身的数
    {
    if (f[j] >= a[i])
    {
    p = j;
    break;
    }
    }
    for (j = p; j >= 0; --j)
    {
    if (f[j] == a[i] && !flag[j + 1])
    {
    flag[j] = 1;
    break;
    }
    else if (f[j] <= a[i] && !flag[j + 1])
    {
    flag[j] = 1;
    a[i] -= f[j];
    }
    }
    for (j = 2; j <= p; j++)//从第2个开始
    {
    ans[i] += flag[j] * f[j - 1];
    }
    printf("%d\n", ans[i]);
    }
    return 0;
    }



  • 相关阅读:
    javascript Ajax类
    C# 使注册表修改后立即生效
    MSSQL:创建临时表并赋值
    sql语句创建[表][列]带描述
    flash调用js中的方法,让js传递变量给flash (兼容 IE & FF)
    SQL中日期转换方法大全
    MSSQL:表变量的声明和赋值
    各种编程方面的CHM参考帮助手册(ADO参考手册、JavaScript参考手册、DHTML参考手册、TransactSQL参考手册、等等)
    vim命令的使用技巧
    Linux 如何启动mail邮件服务
  • 原文地址:https://www.cnblogs.com/lidaojian/p/2380886.html
Copyright © 2011-2022 走看看