uva11636 - Hello World!

题目大意:一开始有一条语句printf("Hello World!\n");你可以选择复制粘贴,问要输出n行Hello World!最少要复制几次?

分析:大水题,每次复制粘贴当前所有的语句则可以使次数最少,所以答案就是ceil(l(logn)/(log2))。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 using namespace std;
 6 int main()
 7 {
 8     int n,Case=1;
 9     while(scanf("%d",&n)==1)
10     {
11         if(n<0) break;
12         printf("Case %d: %d\n",Case++,(int)ceil(log(n)/log(2)));
13     }
14     return 0;
15 }
View Code

有一道题目跟这个很像:VIJOS-P1361

Description

有n个球,从外表上看不出差别,但有一个球比其他球重,其他N-1个球质量相等。请问:在地球上(MS废话),用天平最少称几次可以称出来?

Input

一个自然数N(0< N< =2^24)

Output

输出用天平最小的称量数m(m< 30000)

Sample Input

8

Sample Output

2
这个题目如何产生最优?我们可以发现称一次最多可以分辨出3个球里的那个假球,称两次则是9,三次则是27,n次为3^n,所以结果就是ceil(l(logn)/(log3))。
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 using namespace std;
 6 long long n;
 7 int main ()
 8 {
 9     while(scanf("%lld",&n)!=EOF)
10     {
11         printf("%d\n",(int)ceil(log(n)/log(3)));
12     }
13 }
View Code

 

posted @ 2013-07-12 19:16  默默如潮  阅读(312)  评论(0编辑  收藏  举报