hdu 2369
来源:互联网 发布:ios8录屏软件 编辑:程序博客网 时间:2024/06/17 01:28
Convert Kilometers to Miles
Time Limit: 1000msMemory Limit: 32768KB This problem will be judged on HDU. Original ID: 2370
64-bit integer IO format: %I64d Java class name: Main
Prev Submit Status Statistics Next
Type:
None
Tag it!
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
#include<algorithm>#include<iostream>#include<cstring>#include<cstdio>using namespace std;int num[23]={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[23],goal; int 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=22;j>=1;j--) if (num[j]<=n) { n-=num[j]; ans[j]=1; } for(j=1;j<22;j++) goal+=ans[j+1]*num[j]; printf("%d\n",goal); } }
- hdu 2369
- HDU 2369 尺取法
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- hdu
- hdu-
- hdu
- hdu
- HDU 4185 Oil Skimming (最大匹配)
- Web局部刷新的实现
- Android——Bitmap及其BitmapFactory的常用方法
- Intent传输数据-3
- 3D Systems Geomagic Design X 2016.1.0 x64\
- hdu 2369
- notepad++运行python/C/C++/perl32
- Python练习17-----pytz 模块的安装
- Bentley MicroStation CONNECT Edition Update 3 10.03.00.30 Full Win64 1DVD
- 转:PC上 python 通过串口和 arduino 读写交互成功,哈哈,真好玩
- 算法概论第八章练习题 8.19
- 268.Missing Number leetcode
- 【iOS】基于Realm数据库的记账软件--前言
- Arisa20.0航天 服装服装CAD软件破解版.rar