UVA 11636-Hello World!(水题,猜结论) UVA11636-Hello World!

UVA11636-Hello World!

Time limit: 1.000 seconds

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.

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 2

10

-1 Sample Output Case 1: 1

Case 2: 4

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2683

题目大意:给定n行,求要复制的次数!

分析:这样题意可能比较难理解,(我英文不太好)我是看样例猜答案的,输入的数很有规律,比如样例1,输入2,输出为1,我就这样想,2/2=1,很有道理,再看样例2,输入10,输出按照前面的想法应该是3才对啊,10/2=5,5/2=2,2/2=1->3,稍微拿脑袋想一想就知道不对了,如果向上取整的话是不是就是正解呢,样例1->(2+1)/2=1,样例2->(10+1)/2=5,(5+1)/2=3,(3+1)/2=2,(2+1)/2=1,刚好就等于4,输入小于0的数进行特判即可,于是我就进行了大胆的猜测题意:输入的数每次进行向上取整,求这个数需要经过多少次才能变成1!

下面给出AC代码:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int n;
 4 int main()
 5 {
 6     int p=1;
 7     while(scanf("%d",&n)!=EOF)
 8     {
 9         if(n<0)
10             break;
11         int ans=0;
12         while(n>1)
13         {
14             ans++;
15             n=(n+1)/2;
16         }
17         printf("Case %d: %d\n",p++,ans);
18     }
19     return 0;
20 }

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏数据结构与算法

P3809 【模版】后缀排序

题目背景 这是一道模版题。 题目描述 读入一个长度为 nn 的由大小写英文字母或数字组成的字符串,请把这个字符串的所有非空后缀按字典序从小到大排序,然后按顺序输...

26980
来自专栏owent

最长单调子序列 复杂度nlog(n)

7910
来自专栏机器学习和数学

[编程经验] Python 中列表list介绍

列表是Python中非常重要的一种数据结构,使用频率非常高,本文主要介绍对于学习python的新手来说,需要掌握的一些基础知识。 1. 创建列表 ? 列表用中括...

38650
来自专栏python3

python random模块

随机输出一个0~4的数字和range()输出的数字,去比较。猜对了,就是字母,否则是数字

8720
来自专栏Bingo的深度学习杂货店

Q198 House Robber

You are a professional robber planning to rob houses along a street. Each house ...

29450
来自专栏老秦求学

题目1054:字符串内排序

题目描述: 输入一个字符串,长度小于等于200,然后将输出按字符顺序升序排序后的字符串。 输入: 测试数据有多组,输入字符串。 输出: 对于每组输入,输出处理后...

34970
来自专栏大闲人柴毛毛

动态规划法(三)——最长公共子序列

问题描述 给定两个序列,求出它们的最长公共子序列。 如:序列X={a,b,c,b,d,a,b},Y={b,d,c,a,b,a},则X和Y的最长公共子...

37140
来自专栏小樱的经验随笔

51 Nod 1791 合法括号子段【分治+字符串】

1791 合法括号子段 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 有一个括号序列,现在要计算一下它有多少非空子段是合法...

30860
来自专栏数据结构与算法

1696:逆波兰表达式

1696:逆波兰表达式 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB描述逆波兰表达式是一种把运算符前置的算术表达式,例如普通的...

38170
来自专栏和蔼的张星的图像处理专栏

488. 快乐数

一个数是不是快乐是这么定义的:对于一个正整数,每一次将该数替换为他每个位置上的数字的平方和,然后重复这个过程直到这个数变为1,或是无限循环但始终变不到1。如果可...

16630

扫码关注云+社区

领取腾讯云代金券