码迷,mamicode.com
首页 > 其他好文 > 详细

[UVA - 11636] Hello World! 题解

时间:2017-10-10 22:28:00      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:链接   include   mat   tchar   algo   logs   turn   int   read   

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置。

题目链接(vjudge):https://vjudge.net/problem/UVA-11636

题目大意:

你有一条“hello world!”,你想要通过复制/粘贴得到n条相同的语句。

例如你可以第一次复制得到2条,第二次复制得到4条...当然,你也可以不全复制,例如第二次复制得到3条。

给出多个n(以一个负数表示结束),对每个n,输出最少需要复制多少次。0<n<10001。

 

样例输入:

2

10

-1

样例输出:

Case 1: 1

Case 2: 4

 

分析:

根据n的范围可知答案不超过15.

事先计算出2^i的值,然后对每一个n,二分查找第一个恰好大于它的2^i的值对应的i。

 

AC代码:

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<algorithm>
 5 #include<cmath>
 6 
 7 inline void read(int &x)
 8 {
 9     char ch = getchar(),c = ch;x = 0;
10     while(ch < 0 || ch > 9) c = ch,ch = getchar();
11     while(ch <= 9 && ch >= 0) x = (x<<1)+(x<<3)+ch-0,ch = getchar();
12     if(c == -) x = -x;
13 }
14 
15 int n,cnt,ans,f[20];
16 
17 inline void init()
18 {
19     f[0] = 1,f[1] = 2;
20     for(int i = 2;i <= 18;++ i)
21         f[i] = f[i-1]*2;
22 }
23 
24 int calc(int x)
25 {
26     int l = 1,r = 20,mid,Ans;
27     while(l <= r)
28     {
29         mid = (l+r)>>1;
30         if(f[mid] >= x) Ans = mid,r = mid-1;
31         else l = mid+1;
32     }
33     return Ans;
34 }
35 
36 int main()
37 {
38     init();
39     while(1)
40     {
41         read(n);
42         if(n < 0) break;
43         ++ cnt;
44         if(n == 0 || n == 1) printf("Case %d: %d\n",cnt,0);
45         else{
46             ans = calc(n);
47             printf("Case %d: %d\n",cnt,ans);
48         }
49     }
50     return 0;
51 }

 

[UVA - 11636] Hello World! 题解

标签:链接   include   mat   tchar   algo   logs   turn   int   read   

原文地址:http://www.cnblogs.com/shingen/p/7648086.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!