toj 3761 Egg Problem (好题~~)

本文探讨了一个经典的算法问题——利用有限数量的鸡蛋找出一栋高楼中鸡蛋不会摔碎的最高楼层。通过动态规划的方法,给出了求解任意层数和任意鸡蛋数目的最优解法,并附带了完整的C语言实现代码。

Egg Problem

时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte
总提交: 22 测试通过: 7

描述

 

There is a very interesting problem described as follows:

You are given two eggs.

You have access to a 100-storey building.

An egg that survives a fall can be used again.

A broken egg must be discarded.

The effect of a fall is the same for all eggs.

If an egg breaks when dropped, then it would break if dropped from a higher window.

If an egg survives a fall then it would survive a shorter fall.

It is not ruled out that the first-floor windows break eggs, nor is it ruled out that the 100th-floor windows do not cause an egg to break.

You need to figure out the highest floor an egg can be dropped without breaking. The question is how many drops you need to make.

 

Now, I want to know how to solve this problem for any number of eggs and any storeys. Can you help me?

 

输入

 

In the first line there is an integer T (T <= 10000), indicates the number of test cases.

In each case, there are two integers n and m (1 <= n <= 15, 1 <= m <= 100000), which are the number of eggs and storeys.

 

输出

 

For each case, the output format is “Case c: ans”.

c is the case number start from 1.

ans is the answer of this problem.

 

样例输入

 

2
2 100
3 100

 

样例输出

 

Case 1: 14
Case 2: 9

 

题目来源

2011年绍兴市赛

 

原理:http://blog.sina.com.cn/s/blog_6c813dbd0101bh98.html

推导过程:http://blog.youkuaiyun.com/joylnwang/article/details/6769160

 

 1 #include <stdio.h>
 2 int dp[20][100010];
 3 void Init(){
 4     for(int i=1; i<=15; i++){
 5         for(int j=1; j<=100000; j++){
 6             if(i == 1) dp[i][j] = j;
 7             else if(i == j) dp[i][j] = (1<<i)-1;
 8             else if(i >= j) dp[i][j] = dp[j][j];
 9             else if(i < j)  dp[i][j] = dp[i-1][j-1] + 1 + dp[i][j-1];
10             if(dp[i][j]>=100000){
11                 break;
12             }
13         }
14     }
15 }
16 int main()
17 {
18     int T;
19     Init();
20     scanf("%d", &T);
21     for(int cas=1; cas<=T; cas++){
22         int n, m;
23         scanf("%d %d", &n, &m);
24         for(int i=1; ; i++){
25             if(dp[n][i] >= m){
26                 printf("Case %d: %d\n", cas, i);
27                 break;
28             }
29         }
30     }
31     return 0;
32 }

 

转载于:https://www.cnblogs.com/luotinghao/p/3418427.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值