Problem A
Hello World! 
Input: 
Standard Input

Output: Standard Output

When you first made the computer to print the sentence “Hello World!”, you felt so happy, not knowing how complex and interesting the world of programming and algorithm will turn out to be. Then you did not know anything about loops, so to print 7 lines of “Hello World!”, you just had to copy and paste some lines. If you were intelligent enough, you could make a code that prints “Hello World!” 7 times, using just 3 paste commands. Note that we are not interested about the number of copy commands required. A simple program that prints “Hello World!” is shown in Figure 1. By copying the single print statement and pasting it we get a program that prints two “Hello World!” lines. Then copying these two print statements and pasting them, we get a program that prints four “Hello World!” lines. Then copying three of these four statements and pasting them we can get a program that prints seven “Hello World!” lines (Figure 4). So three pastes commands are needed in total and Of course you are not allowed to delete any line after pasting. Given the number of “Hello World!” lines you need to print, you will have to find out the minimum number of pastes required to make that program from the origin program shown in Figure 1.

Figure 1

Figure 2

Figure3

Figure 4

Input

The input file can contain up to 2000 lines of inputs. Each line contains an integer N (0<N<10001) that denotes the number of “Hello World!” lines are required to be printed.

Input is terminated by a line containing a negative integer.

Output

For each line of input except the last one, produce one line of output of the form “Case X: Y” where X is the serial of output and Y denotes the minimum number of paste commands required to make a program that prints N lines of “Hello World!”.

 

 

Sample Input                             Output for Sample Input

2

10

-1

Case 1: 1

Case 2: 4

题意:给定n行,求要复制的次数。

思路:贪心,前面都尽量两倍两倍复制,最后在复制一次剩下的即可。

代码:

#include <stdio.h>
#include <math.h> double n;
int main() {
int cas = 0;
while (~scanf("%lf", &n) && n > 0) {
printf("Case %d: %d\n", ++cas, n == 1 ? 0 : (int)log2(n - 1) + 1);
}
return 0;
}

11636 - Hello World! (贪心法)的更多相关文章

  1. 贪心法基础题目 HDU

    贪心算法的基本步骤: 1.从问题的某个初始解出发.2.采用循环语句,当可以向求解目标前进一步时,就根据局部最优策略,得到一个部分解,缩小问题的范围或规模.3.将所有部分解综合起来,得到问题的最终解. ...

  2. 杭电OJ1789、南阳OJ236(贪心法)解题报告

    杭电OJ1789http://acm.hdu.edu.cn/showproblem.php?pid=1789 南阳OJ236http://59.69.128.203/JudgeOnline/probl ...

  3. NPC问题及其解决方法(回溯法、动态规划、贪心法、深度优先遍历)

    NP问题(Non-deterministic Polynomial ):多项式复杂程度的非确定性问题,这些问题无法根据公式直接地计算出来.比如,找大质数的问题(有没有一个公式,你一套公式,就可以一步步 ...

  4. 《挑战程序设计竞赛》2.2 贪心法-其它 POJ3617 3069 3253 2393 1017 3040 1862 3262

    POJ3617 Best Cow Line 题意 给定长度为N的字符串S,要构造一个长度为N的字符串T.起初,T是一个空串,随后反复进行下列任意操作: 从S的头部(或尾部)删除一个字符,加到T的尾部 ...

  5. 《挑战程序设计竞赛》2.2 贪心法-区间 POJ2376 POJ1328 POJ3190

    POJ2376 Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14585   Accepte ...

  6. 货币兑换问题(贪心法)——Python实现

      # 贪心算法求解货币兑换问题 # 货币系统有 n 种硬币,面值为 v1,v2,v3...vn,其中 v1=1,使用总值money与之兑换,求如何使硬币的数目最少,即 x1,x2,x3...xn 之 ...

  7. HDU 1661 Assigments 贪心法题解

    Problem Description In a factory, there are N workers to finish two types of tasks (A and B). Each t ...

  8. LeetCode刷题笔记-贪心法-格雷编码

    题目描述: 格雷编码是一个二进制数字系统,在该系统中,两个连续的数值仅有一个位数的差异. 给定一个代表编码总位数的非负整数 n,打印其格雷编码序列.格雷编码序列必须以 0 开头. 来源:力扣(Leet ...

  9. 贪心法-------Saruman's army

    此题的策略是选取可用范围最右边的点,一般来说该点辐射两边,左侧辐射,右侧辐射,所以用两个循环,第一个循环找出该点,第二个循环求出最右边的点 源代码: #include<iostream># ...

  10. LEETCODE —— Best Time to Buy and Sell Stock II [贪心算法]

    Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...

随机推荐

  1. 关于IIS部署成功后,局域网其他用户无法访问的问题解决方法

    关于win7部署iis后,局域网的其他用户不能访问的问题.   在win7系统中,部署好iis后,自己本地可以发布和预览.但在局域网中的其他用户不可以访问.下面说一下这个原因. 这是因为win7自带的 ...

  2. nyoj 214

    //nyoj 214 这个题目和字符串的问题类似,都是给出一组数据,寻找最长的单调递增字符 这一题一开始我用dp做,发现超时,看了下时间,n*n的复杂度,换过一种思路 用类似于栈的方式,来存储每次更新 ...

  3. Svg操作

    SVG文件的JavaScript操作 获取SVG DOM 如果使用img标签插入SVG文件,则无法获取SVG DOM.使用object.iframe.embed标签,可以获取SVG DOM. var ...

  4. Shell语法中的test命令用法

    test命令用法.功能:检查文件和比较值   1)判断表达式 if test (表达式为真) if test !表达式为假 test 表达式1 –a 表达式2 两个表达式都为真 test 表达式1 – ...

  5. WARNING OGG-01223 TCP/IP error 111 (Connection refused)

    一:问题描述 GGSCI (source_pc) 64> info all Program     Status      Group       Lag at Chkpt  Time Sinc ...

  6. php中iconv函数的一个小bug--转载

    iconv转换字符集很好用,但是有时候你会发现iconv转换的时候会返回false或者空字符串,严格说来这算不上是iconv的问题,这其实是字符集的问题,但是实际编码中应该算是iconv的bug了. ...

  7. HashTable 及应用

    HashTable-散列表/哈希表,是根据关键字(key)而直接访问在内存存储位置的数据结构. 它通过一个关键值的函数将所需的数据映射到表中的位置来访问数据,这个映射函数叫做散列函数,存放记录的数组叫 ...

  8. noip201506 Message 信息传递

    试题描述: 有 n 个同学(编号为 1 到 n )正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其中,编号为 i 的同学的信息传递对象是编号为 T_i 的同学.游戏开始时,每人都只 ...

  9. JS浏览器关闭时清空cookie

    function addCookie(objName,objValue,objHours){    var str = objName + "=" + escape(objValu ...

  10. js插件zClip实现复制到剪贴板功能

    相信这个功能大家平时上网经常能碰到,以前也没怎么留意怎么实现的,直到项目中需要. 网上一搜一大堆,单纯使用js方法也不是没有,但是由于各浏览器的安全机制不同,不是跨浏览器的.去看了几个常用的网站,都是 ...