UVA 11636 Hello World!

本文介绍了一种通过二进制复制的方式实现快速打印指定行数“Hello World!”的方法。核心思想在于利用二进制表示来减少重复的复制粘贴操作,从而找到达到特定行数所需的最少复制次数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题面:

    When you first made the computer to print the sentence “Hello World!”, you felt so happy, notknowing 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 tocopy and paste some lines. If you were intelligent enough, you could make a code that prints “HelloWorld!” 7 times, using just 3 paste commands. Note that we are not interested about the numberof copy commands required. A simple program that prints “Hello World!” is shown in Figure 1. Bycopying 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 aprogram that prints seven “Hello World!” lines (Figure 4). So three pastes commands are needed intotal and Of course you are not allowed to delete any line after pasting. Given the number of “HelloWorld!” lines you need to print, you will have to find out the minimum number of pastes required tomake 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 ’ whereX is the serial of output and Y denotes the minimum number of paste commands required to make aprogram that prints N lines of “Hello World!”.


Sample Input

2

10

-1


Sample Output

Case 1: 1

Case 2: 4


题目大意:

    给定一个数字问最少复制几次可以达到要求。


解题:

    用二进制复制最快,找到小于等于他的最大的那个二进制表示由1左移相应位数得到的数的位数.(有没有很绕,哈哈)


代码:

#include <iostream>
#include <cstdio>
using namespace std;
int ans[20010];
int main()
{
	int cnt=1,n,p1,p2;
	ans[1]=0;
	for(int i=1;i<=14;i++)
	{
		p1=(1<<(i-1))+1;
		p2=1<<i;
		for(int j=p1;j<=p2;j++)
		  ans[j]=i;
	}
	while(cin>>n&&n>0)
	{
		cout<<"Case "<<cnt++<<": ";
		cout<<ans[n]<<endl;
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值