内部比赛C Convert Kilometers to …
2013-09-11 14:07
363 查看
Convert Kilometers to Miles
Time Limit: 1000MS Memory limit: 65536K
题目描述
This year, Bruce Force spends his vacation inFlagstaff, 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 = 0for 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.
输入
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.
输出
For each distance x in kilometers outputthe distance y in miles calculated according to Bruce\'s
algorithm.
示例输入
5 42 100 180 300 360
示例输出
26 62 111 185 222
这个题纠结了好一会,自己英语水平太弱题意没读懂,其实就是任意一个数可以表示成两个Fibonacci numbers的和,而每个Fibonacci number的KM可以转化成mile数值就是Fibonacci number的下一个
好了其他的自己理解吧
#include<stdio.h> int num,n; int f[400]; void Fib() { int i; f[0]=0; f[1]=1; f[2]=2; for(i=3;;i++) { f[i]=f[i-1]+f[i-2]; if(f[i]>=25000) break; } num=i; } void zhuhuan() { int sum=0; int i; i=num; while(1) { if(n>=f[i]) { n-=f[i]; sum+=f[i-1]; i--; if(n==0) break; } i--; } printf("%d\n",sum);
} int main() { Fib(); int t; while(scanf("%d",&t)!=EOF) { while(t--) { scanf("%d",&n); zhuhuan(); } } return 0; }
相关文章推荐
- 内部比赛 A Alfredo\'s Pizza Res…
- 内部比赛 E El Dorado
- 内部比赛 G A Game with Marbles
- VMware&nbsp;给debian6.0&nbsp;安装VMware&nbsp;To…
- 【转载】Genymotion提示To fi…
- eclipse Unable to execute dex: GC overhead limit exceeded GC overhead limit exceeded解决办法
- Cannot convert value '0000-00-00 00:00:00' from column 1 to TIMESTAMP .
- 解决:Unable to connec…
- ACM: 简单DP 动态规划题 toj 1547
- 1607:Unable&nbsp;to&nbsp;install&nbsp;InstallSh…
- Unable&nbsp;to&nbsp;compile&nbsp;class&nbsp;for&nbsp;JSP
- It&nbsp;is&nbsp;never&nbsp;too&nbsp;late&nbsp;to&nbsp;make&nbsp;a&nbsp;c…
- Simple&nbsp;test&nbsp;to&nbsp;hide&nbsp;Information&nbsp;…
- 《Springboot极简教程》MappingMongoConverter:Failed to convert from type [java.lang.String] to type [long] for value 'null'; ne
- OracleserviceSID to write to net…
- EBS: How to clone?
- How&nbsp;to&nbsp;pass&nbsp;a&nbsp;hash&nbsp;to&nbsp;a&nbsp;subrouti…
- error C2664: 'cvSetMouseCallback' : cannot convert parameter 2 from ....to'CvMouseback'
- How&nbsp;to&nbsp;Install&nbsp;and&nbsp;Run&nbsp;Network&nbsp;S…
- 类型转换 -- 使用Unicode下的cannot convert parameter 1 from 'CString' to 'const char *' 解决办法