2909. [ACM][2018新生赛]

GNU C 1000 MS,32768 KB
GNU C++ 1000 MS,32768 KB
Java 2000 MS,262144 KB
Python 2 1000 MS,65536 KB
Python 3 1000 MS,65536 KB

Brother Ming likes to chew gum while playing games.
“Chewing gum is good for concentration, for achieving high levels of concentration quickly, for relieving stress and so on.”
"Brother Ming said to me as he chewed his gum and looked at the lovely black and white picture.
It is known that brother Ming has n boxes of chewing gum, and the number of chewing gum in the boxes is 1-n.
Each day, he chooses the number m and eats m pieces of gum in all the boxes with the number greater than or equal to m.
So how soon can Ming eat all the gum in the box?

在这里插入图片描述
Input
Brother Ming likes to chew gum while playing games.
There are multiple sets of input data
Enter a T in the first row to indicate that there is a T set of data next
The second row contains an integer n (1 <= n <= 231), representing the number of boxes.

Output
Each output takes two lines.
The first line outputs Case # I:, where I represents the set of samples
The second line outputs the number of days Ming ate all the gum

Examples

Input
4
1
2
3
4

Output
Case #1:
1
Case #2:
2
Case #3:
2
Case #4:
3

思路
这是一道水题
由于这n个盒子中有1-n个口香糖
因此,我们只要确保所有盒子中,口香糖数目最多的为0就好
我们假设t为每一次取完m个口香糖后所有盒子中最多的口香糖数
倘若m = 1或者m = t,不难想象,次数就是n
所以我们应尽量往两边取值
当n为奇数时,我们可以直接取m = (t + 1) / 2
则剩下为t = t - (t + 1) / 2 = (t - 1) / 2 = t // 2
当n为偶数时,我们可以直接取m = t / 2
则剩下为t = t - t / 2 = t / 2
而我们已知
当t = 1时,次数为1
当t = 2时,次数为2
当t = 3时,次数为2
而任何大于3的整数在除2的过程中肯定会变成2或3
因此,我们可以以2和3为循环终点

程序演示

#include <stdio.h>

int main(int argc, char *argv[]) {
	int T,a;
	while(~scanf("%d",&T)){
		for(int i=1; i<=T; i++){
			int times = 2,b = 1;
			scanf("%d",&a);
			if(a == 1){
				times = 1;
				b = 0;
			}
			while((a-2) && (a-3) && b){
				times += 1;
				a /= 2;
			}
			printf("Case #%d:\n%d\n",i,times);
		}
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值