Convert Kilometers to Miles 2010.3.6

Convert Kilometers to Miles 2010.3.6

赤果果的x进制

 

Convert Kilometers to Miles

 

Time Limit:1000MS  Memory Limit:65536K

Total Submit:21 Accepted:11

 

Description

 

This year, Bruce Force spends his vacationin Flagstaff, Arizona, where he wants to practice for hisnext half marathon (a race over 21 km). At his first training he runs to hisfriend Greedy Gonzales' home which is 21 miles away from Flagstaff.

Arriving there, he is very tired andrealizes that 21 miles are much more than 21 km. Greedy Gonzales tells him that21 km equals 13 miles. 21, 13? Bruce realizes immediately that there must be adeeper relation! Both, 13 and 21 are Fibonacci numbers!

Fibonacci numbers can be defined asfollows:

• F1 = 1

• F2 = 2

• Fn+1 = Fn+Fn-1 for n>1.

Bruce has just learned about the Fibonaccinumber system at his university. Each positive integer x can be written as thesum of different Fibonacci numbers, so this means that there exists numbers kand 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 therepresentation as: b(x) = (bk, bk-1, ..., b1). To make the representationunique, 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 noticesthat one can convert a distance x in kilometers into a corresponding distance yto miles as follows: First, write down x in its Fibonacci system representationb(x). Second, shift the bits of b(x) one position to the right (the last bit isdeleted) and get b(y). Third, calculate y from b(y) by evaluating the sum givenabove.

For example, the number 42 written in the Fibonaccisystem is (1,0,0,1,0,0,0,0). In step two we would shift the bits one positionto 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 forBruce that converts kilometers into miles according to Bruce's algorithm.

 

 

Input

 

The first line of the input contains t, thenumber 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 outputthe 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

 

ULM 2008

#include <stdio.h>
#include <string.h>

#define MAXN 22

int num[MAXN+1]={0,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711,28657};
int t,n,m;
int ans[MAXN+1],goal;

void main()
{
	int i,j;
	scanf("%d",&t);
	for(i=1;i<=t;i++)
	{
		scanf("%d",&n);
		goal=0;
		memset(ans,0,sizeof(ans));
		for(j=MAXN;j>=1;j--)
			if (num[j]<=n)
			{
				n-=num[j];
				ans[j]=1;
			}
		for(j=1;j<MAXN;j++)
			goal+=ans[j+1]*num[j];
		printf("%d\n",goal);
	}
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值