假设有 n=2 个完全一样的鸡蛋, 有 k=36 层楼。 存在一个楼层 m,把鸡蛋从低于m的楼层扔下去的时候,鸡蛋不会摔破;把鸡蛋从高于等于m的楼层扔下去的时候,鸡蛋会摔破。题目要求,找出临界楼层m,并且扔鸡蛋的次数要做到最少。

思路:

最简单的方法是,从第 1 层往上,一层一层 的扔鸡蛋,直到鸡蛋摔碎。这样需要扔鸡蛋 m 次。

下面用递归的方法解决该问题。假设鸡蛋从 x 层扔下去。(1)如果鸡蛋摔破,那么还剩 n-1 个鸡蛋,临界楼层在 [1, x] 之间;(2)如果鸡蛋没破,那么还剩 n 个鸡蛋,临界楼层在 [k, x+1] z之间。 设 eggDrop(n,k) 是问题的解,那么递归表达式是

eggDrop(n,k) = 1+ min (   max( eggDrop(n-1, x-1), eggDrop(n, k-x) ) for all x in [1,k] )

代码如下:

# include <stdio.h>
# include <limits.h>// A utility function to get maximum of two integers
int max(int a, int b) { return (a > b)? a: b; }/* Function to get minimum number of trails needed in worstcase with n eggs and k floors */
int eggDrop(int n, int k)
{// If there are no floors, then no trials needed. OR if there is// one floor, one trial needed.if (k == 1 || k == 0)return k;// We need k trials for one egg and k floorsif (n == 1)return k;int min = INT_MAX, x, res;// Consider all droppings from 1st floor to kth floor and// return the minimum of these values plus 1.for (x = 1; x <= k; x++){res = max(eggDrop(n-1, x-1), eggDrop(n, k-x));if (res < min)min = res;}return min + 1;
}/* Driver program to test to pront printDups*/
int main()
{int n = 2, k = 10;printf ("\nMinimum number of trials in worst case with %d eggs and ""%d floors is %d \n", n, k, eggDrop(n, k));return 0;
}

动态规划的代码如下:

# include <stdio.h>
# include <limits.h>// A utility function to get maximum of two integers
int max(int a, int b) { return (a > b)? a: b; }/* Function to get minimum number of trails needed in worstcase with n eggs and k floors */
int eggDrop(int n, int k)
{/* A 2D table where entery eggFloor[i][j] will represent minimumnumber of trials needed for i eggs and j floors. */int eggFloor[n+1][k+1];int res;int i, j, x;// We need one trial for one floor and0 trials for 0 floorsfor (i = 1; i <= n; i++){eggFloor[i][1] = 1;eggFloor[i][0] = 0;}// We always need j trials for one egg and j floors.for (j = 1; j <= k; j++)eggFloor[1][j] = j;// Fill rest of the entries in table using optimal substructure// propertyfor (i = 2; i <= n; i++){for (j = 2; j <= k; j++){eggFloor[i][j] = INT_MAX;for (x = 1; x <= j; x++){res = 1 + max(eggFloor[i-1][x-1], eggFloor[i][j-x]);if (res < eggFloor[i][j])eggFloor[i][j] = res;}}}// eggFloor[n][k] holds the resultreturn eggFloor[n][k];
}/* Driver program to test to pront printDups*/
int main()
{int n = 2, k = 36;printf ("\nMinimum number of trials in worst case with %d eggs and ""%d floors is %d \n", n, k, eggDrop(n, k));return 0;
}

Egg Drop 扔鸡蛋相关推荐

  1. Coursera Algorithms week1 算法分析 练习测验: Egg drop 扔鸡蛋问题

    题目原文: Suppose that you have an n-story building (with floors 1 through n) and plenty of eggs. An egg ...

  2. java动态规划鸡蛋问题_教你彻底理解动态规划——扔鸡蛋问题 Drop Eggs2

    问题 有一个n层的建筑.如果一个鸡蛋从第k层及以上落下,它会碎掉.如果从低于这一层的任意层落下,都不会碎. 有m个鸡蛋,用最坏的情况下实验次数最少的方法去找到k, 返回最坏情况下所需的实验次数. 样例 ...

  3. 254. Drop Eggs (扔鸡蛋经典题)

    Drop Eggs 中文English There is a building of n floors. If an egg drops from the k th floor or above, i ...

  4. c++扔鸡蛋问题egg dropping puzzle(附完整源码)

    C++扔鸡蛋问题egg dropping puzzle 扔鸡蛋问题egg dropping puzzle算法的完整源码(定义,实现,main函数测试) 扔鸡蛋问题egg dropping puzzle ...

  5. [CareerCup] 6.5 Drop Eggs 扔鸡蛋问题

    6.5 There is a building of 100 floors. If an egg drops from the Nth floor or above, it will break. I ...

  6. 扔鸡蛋问题具体解释(Egg Dropping Puzzle)

    经典的动态规划问题,题设是这种: 假设你有2颗鸡蛋,和一栋36层高的楼,如今你想知道在哪一层楼之下,鸡蛋不会被摔碎,应该怎样用最少的測试次数对于不论什么答案楼层都可以使问题得到解决. 假设你从某一层楼 ...

  7. dp 扔鸡蛋_使用动态编程(DP)的鸡蛋掉落问题

    dp 扔鸡蛋 Problem statement: You are given N floor and K eggs. You have to minimize the number of times ...

  8. 彻底搞懂-扔鸡蛋问题-方程-动态规划

    1.题目: 2个鸡蛋,从100层楼上往下扔,以此来测试鸡蛋的硬度,比如鸡蛋在第9层没有摔碎而在第10层摔碎了,那么鸡蛋不会摔碎的零界点就是9层,如何用最少的尝试次数,测试出鸡蛋不会摔碎的临界点? 2. ...

  9. 扔鸡蛋问题-方程-动态规划

    参考:程序员小灰 https://blog.csdn.net/weixin_40564421/article/details/78988078 题目:2个鸡蛋,从100层楼上往下扔,以此来测试鸡蛋的硬 ...

  10. 算法第四版扔鸡蛋问题

    本题来源于算法第四版1.4.25.1.4.26. 同时好像记得看过的腾讯面经里也问到过类似题目,因此觉得有必要仔细做一下. 题目如下: 一幢 100 层的大楼,给你两枚鸡蛋.假设,在第 n 层扔下鸡蛋 ...

最新文章

  1. 饥荒计算机丢失xinput1 3.dll,《德军总部:旧血脉》Win8运行提示丢失XINPUT1_3.dll解决方法...
  2. 取得Repeater内部控件命令名与命令参数
  3. ubuntu14.04配置java jdk
  4. 011_Vue自定义指令
  5. 手把手干货教学Matlab载波调制
  6. php后台地址检测,[thinkphp] 隐藏后台地址
  7. java二维码生成并可以转换
  8. nohup命令输出日志_逼格高又实用的Linux高级命令,开发运维都要懂
  9. 一键提升多媒体内容质量:漫谈图像超分辨率技术
  10. http web 返回码概念
  11. 【Python实例第11讲】文本的核外分类
  12. 王小九用计算机弹桥边姑娘,抖音最火歌曲是哪首?QQ音乐开放平台《桥边姑娘》让“野狼”靠边站...
  13. 数据中台在企业数字化转型中的践行(下篇)
  14. 经典时间序列的学习(一)简单的认识时间序列
  15. 方舟无限琥珀服务器,方舟生存进化无限琥珀版
  16. data:image图片转png与jpg,png转data:image格式。
  17. 详解物联网常用协议:IIC和RS485通信协议
  18. SQL Sever2012安装错误——Windows Installer错误消息:打开安装日志文件的错误的原因及解决方案
  19. linux的sh脚本编程
  20. 教你手动编辑图像,提高ABBYY FineReader PDF 15识别准确性

热门文章

  1. narginchk的输出参数太多_MATLAB提示输出参数过多,如何解决?
  2. Layui全部笔记及相关代码
  3. A Unified Evaluation Benchmark and Adversarial Graph Learning
  4. android camera2点击监听,使用Android camera2 API打开/关闭闪光灯不起作用
  5. dijkstra之细节处理 ——PAT (Advanced Level) 1072 Gas Station (30 分)
  6. 毛书卿:5.21白银今日走势分析及黄金最新操作消息
  7. 2022年上半年我国人均存款破8万,这样的攒钱方式更快
  8. 内推 | 【泸州老窖 - 渠道数字化运营​】成都
  9. 环境搭建,8种基本类型,Static,package和import,log4j
  10. 山科c语言考试题库,山科大C语言不完全题库讲解.doc