uva 11780 - Miles 2 Km(dp+Fibonacci)

9 篇文章 0 订阅

 Miles 2 Km 

It is a well known fact that you can use Fibonacci numbers to convert distances from miles 2 kilometers. Well, it's actually not a well known fact. It is, however, a well known fact that one mile is 1.6 km. Now let me explain how you can use Fibonacci numbers in order to convert (approximately) miles to kilometers. Lets first write down the first numbers of the Fibonacci sequence:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,...

So let's suppose you want to convert 5 miles into kms. Well, in the Fibonacci sequence the number following 5 is 8 so 5 miles is 8 kms. You don't believe me? ok, just do the maths: multiply 5 by 1.6... 5  x  1.6 = 8! Amazing right?. The bad thing is that it doesn't work all the time, for example, how do you convert 89 miles into kms?. According to the previous procedure it would be 144 km, but it is really 89  x  1.6 = 142.4, so there is an error of 1.6. Still amazing no?. Anyway, what would you do if you want to convert 6 miles into kilometers?. You could, for example, say 6 = 5 + 1, so we convert 5 and 1 to kms and then we add up. 5 miles is 8 kms and 1 miles is 2 kms, adding up we obtain 8 + 2 = 10 so 6 miles is approximately 10 kms. Of course, we could also say 6 = 2 + 2 + 2, so we convert 2 miles to kms and obtain 3. So 6 miles is approximately 3 + 3 + 3 = 9 kms. Which answer is closer to the real value?.


It is your task to write a program which finds the best conversion from miles to kilometers using the previous Fibonacci method. The best conversion is the one with the lowest error.

Input 

The input will consist of several test cases. Each case will be a single line containing one integer number 0 < N$ \le$1000 . The end of input is indicated by a test case with  N = 0 .

Output 

For each test case in the input, your program must print the lower error  e  for converting the corresponding input to kilometers (rounding up to 2 decimal digits).

Sample Input 

6
5
12
0

Sample Output 

0.40
0.00
0.20
 
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;

const int maxn = 20;
int F[maxn];
double next[maxn*100] , dp[maxn*100];

void ini(){
    F[0] = 1;
    F[1] = 2;
    for(int i = 2; i < maxn; i++){
        F[i] = F[i-1]+F[i-2];
    }
    for(int i = 0; i < maxn*100; i++){
        dp[i] = 10000000;
        next[i] = 0;
    }
    for(int i = 0; i < 16; i++) next[F[i]] = F[i+1];
}

void computing(){
    for(int i = 1; i <= 1000; i++){
        double standerd = i*1.6;
        dp[i] = min(dp[i] , fabs(standerd-next[i]));
        for(int j = 1; j < i; j++){
            dp[i] = min(dp[i] , dp[j]+dp[i-j]);
        }
    }
}

int main(){
    ini();
    computing();
    int n;
    while(cin >> n && n){
        printf("%.2lf\n" , dp[n]);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值