楼主: 〇〇

[每日一题] puzzleup 2020 11月开始

[复制链接]
论坛徽章:
407
紫蛋头
日期:2012-05-21 10:19:41迷宫蛋
日期:2012-06-06 16:02:49奥运会纪念徽章:足球
日期:2012-06-29 15:30:06奥运会纪念徽章:排球
日期:2012-07-10 21:24:24鲜花蛋
日期:2012-07-16 15:24:59奥运会纪念徽章:拳击
日期:2012-08-07 10:54:50奥运会纪念徽章:羽毛球
日期:2012-08-21 15:55:33奥运会纪念徽章:蹦床
日期:2012-08-21 21:09:51奥运会纪念徽章:篮球
日期:2012-08-24 10:29:11奥运会纪念徽章:体操
日期:2012-09-07 16:40:00
31#
 楼主| 发表于 2020-11-26 18:43 | 只看该作者
newkid 发表于 2020-11-25 23:40
#4 CANDIES There are two boxes each having 10 candies. You will randomly (with equal probability) se ...

1/8 ?

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<time.h>
  4. int f()
  5. {
  6. int a[2]={10,10};
  7. for(int j=0;j<15;j++)
  8. {
  9. a[rand()%2]--;
  10. if(a[0]==0 || a[1]==0) break;
  11. }
  12. return (a[1]==5 || a[0]==5);

  13. }
  14. int main()
  15. {
  16. int N=10000;
  17. int n=0;
  18. srand(time(NULL));
  19. for (int i=0;i<N;i++)
  20. n+=f();
  21. printf("%d/%d\n",n,N);
  22. }
复制代码

使用道具 举报

回复
论坛徽章:
519
奥运会纪念徽章:垒球
日期:2008-09-15 01:28:12生肖徽章2007版:鸡
日期:2008-11-17 23:40:58生肖徽章2007版:马
日期:2008-11-18 05:09:48数据库板块每日发贴之星
日期:2008-11-29 01:01:02数据库板块每日发贴之星
日期:2008-12-05 01:01:03生肖徽章2007版:虎
日期:2008-12-10 07:47:462009新春纪念徽章
日期:2009-01-04 14:52:28数据库板块每日发贴之星
日期:2009-02-08 01:01:03生肖徽章2007版:蛇
日期:2009-03-09 22:18:532009日食纪念
日期:2009-07-22 09:30:00
32#
发表于 2020-11-26 22:08 | 只看该作者
干嘛要用随机数模拟?总共也没多少种,SQL暴力解决。手算也不难。

使用道具 举报

回复
论坛徽章:
548
生肖徽章2007版:猴
日期:2008-05-16 11:28:59生肖徽章2007版:马
日期:2008-10-08 17:01:01SQL大赛参与纪念
日期:2011-04-13 12:08:17授权会员
日期:2011-06-17 16:14:53ITPUB元老
日期:2011-06-21 11:47:01ITPUB官方微博粉丝徽章
日期:2011-07-01 09:45:27ITPUB十周年纪念徽章
日期:2011-09-27 16:30:472012新春纪念徽章
日期:2012-01-04 11:51:222012新春纪念徽章
日期:2020-11-30 22:13:24海蓝宝石
日期:2012-02-20 19:24:27
33#
发表于 2020-11-27 22:16 | 只看该作者
# 4

                       
--method 1:
SQL>
SQL> with t as (select level n from dual connect by level<=20),
  2       s as (select substr(sys_connect_by_path(n,','),2) path
  3               from t
  4               where level = 5
  5               connect by nocycle prior n < n ),
  6       r as (select path,regexp_substr(path,'[^,]+',1,level) v
  7                from s
  8               connect by prior path = path
  9                          and level <= regexp_count(path,',')+1
10                          and prior dbms_random.value is not null),
11       p as (
12            select count(distinct a.path) cnt
13              from (
14            select path,v,
15                   count(case when v <=10 then 1 end) over(partition by path) lt,
16                   count(case when v >=11 then 1 end) over(partition by path) gt
17             from r
18             ) a
19            where (lt = 5 and gt = 0) or (lt=0 and gt=5)
20           ),
21       q as (
22             select cnt,(select count(*) from s) as total_cnt from p),
23      f(total_cnt,cnt) as ( select total_cnt,cnt from q
24                             union all
25                            select greatest(total_cnt - cnt,cnt),least(total_cnt - cnt,cnt)
26                              from f
27                            where total_cnt <> cnt)
28      select q.cnt/f.cnt||'/'||q.total_cnt/f.cnt as res
29        from q,f
30       where f.total_cnt = f.cnt
31  /

RES
--------------------------------------------------------------------------------
21/646

使用道具 举报

回复
论坛徽章:
548
生肖徽章2007版:猴
日期:2008-05-16 11:28:59生肖徽章2007版:马
日期:2008-10-08 17:01:01SQL大赛参与纪念
日期:2011-04-13 12:08:17授权会员
日期:2011-06-17 16:14:53ITPUB元老
日期:2011-06-21 11:47:01ITPUB官方微博粉丝徽章
日期:2011-07-01 09:45:27ITPUB十周年纪念徽章
日期:2011-09-27 16:30:472012新春纪念徽章
日期:2012-01-04 11:51:222012新春纪念徽章
日期:2020-11-30 22:13:24海蓝宝石
日期:2012-02-20 19:24:27
34#
发表于 2020-11-27 22:17 | 只看该作者
method 2:

SQL> with t as (select level n from dual connect by level<=20),
  2       s(lvl,nlist,lt,gt,last_n) as (select 1,cast(n as varchar2(100)),
  3                                      case when n<=10 then 1 else 0 end,
  4                                      case when n>=10 then 1 else 0 end,
  5                                      n
  6                                 from t
  7                               union all
  8                              select lvl+1,
  9                                     s.nlist||','||t.n,
10                                     s.lt + case when t.n<=10 then 1 else 0 end,
11                                     s.gt + case when t.n>11 then 1 else 0 end,
12                                     t.n
13                                from s,t
14                               where s.lvl < 5
15                                 and s.last_n < t.n
16                             ),
17                q as (
18                select cnt,total_cnt
19                  from (
20                        select count(nlist) cnt
21                          from s
22                         where lvl = 5
23                         and (lt=5 and gt=0 or lt=0 and gt=5)
24                      ) a, (select count(*) total_cnt from s where lvl=5) b
25                ),
26    f(total_cnt,cnt) as ( select total_cnt,cnt from q
27                               union all
28                              select greatest(total_cnt - cnt,cnt),least(total_cnt - cnt,cnt)
29                                from f
30                              where total_cnt <> cnt)
31        select q.cnt/f.cnt||'/'||q.total_cnt/f.cnt as res
32          from q,f
33         where f.total_cnt = f.cnt
34             ;

RES
--------------------------------------------------------------------------------
21/646          

使用道具 举报

回复
论坛徽章:
519
奥运会纪念徽章:垒球
日期:2008-09-15 01:28:12生肖徽章2007版:鸡
日期:2008-11-17 23:40:58生肖徽章2007版:马
日期:2008-11-18 05:09:48数据库板块每日发贴之星
日期:2008-11-29 01:01:02数据库板块每日发贴之星
日期:2008-12-05 01:01:03生肖徽章2007版:虎
日期:2008-12-10 07:47:462009新春纪念徽章
日期:2009-01-04 14:52:28数据库板块每日发贴之星
日期:2009-02-08 01:01:03生肖徽章2007版:蛇
日期:2009-03-09 22:18:532009日食纪念
日期:2009-07-22 09:30:00
35#
发表于 2020-11-29 02:50 | 只看该作者
本帖最后由 newkid 于 2020-11-29 02:54 编辑

我感觉你的方法有问题。每吃一颗糖就是一次随机的二选一,概率乘以1/2。
假设初始状态(10,10),
第一颗之后变成(10,9) 以及 (9,10),
各占1/2,

第二颗之后状态是(10,8)(9,9)(9,9)(8,10)各占1/4。(9,9)的概率是1/4+1/4=1/2
你不妨套用一下自己的算法看能否得到上述结果。
题目要求的最终状态是(0,5)和(5,0),每个状态概率1/2^15, 所以只要求出总共有多少条路径会到达这个状态。

使用道具 举报

回复
论坛徽章:
548
生肖徽章2007版:猴
日期:2008-05-16 11:28:59生肖徽章2007版:马
日期:2008-10-08 17:01:01SQL大赛参与纪念
日期:2011-04-13 12:08:17授权会员
日期:2011-06-17 16:14:53ITPUB元老
日期:2011-06-21 11:47:01ITPUB官方微博粉丝徽章
日期:2011-07-01 09:45:27ITPUB十周年纪念徽章
日期:2011-09-27 16:30:472012新春纪念徽章
日期:2012-01-04 11:51:222012新春纪念徽章
日期:2020-11-30 22:13:24海蓝宝石
日期:2012-02-20 19:24:27
36#
发表于 2020-11-29 10:01 | 只看该作者
newkid 发表于 2020-11-29 02:50
我感觉你的方法有问题。每吃一颗糖就是一次随机的二选一,概率乘以1/2。假设初始状态(10,10),第一颗之后变 ...

是啊,感觉做错了,我计算的是吃完15个糖,余下的5个都剩到一个盒子中的概率。理解错了!

使用道具 举报

回复
论坛徽章:
548
生肖徽章2007版:猴
日期:2008-05-16 11:28:59生肖徽章2007版:马
日期:2008-10-08 17:01:01SQL大赛参与纪念
日期:2011-04-13 12:08:17授权会员
日期:2011-06-17 16:14:53ITPUB元老
日期:2011-06-21 11:47:01ITPUB官方微博粉丝徽章
日期:2011-07-01 09:45:27ITPUB十周年纪念徽章
日期:2011-09-27 16:30:472012新春纪念徽章
日期:2012-01-04 11:51:222012新春纪念徽章
日期:2020-11-30 22:13:24海蓝宝石
日期:2012-02-20 19:24:27
37#
发表于 2020-11-29 10:38 | 只看该作者
newkid 发表于 2020-11-29 02:50
我感觉你的方法有问题。每吃一颗糖就是一次随机的二选一,概率乘以1/2。假设初始状态(10,10),第一颗之后变 ...

如果吃第11颗糖的时候前一状态为 (10,0),那么下一步的状态只能是 (9,0),不可能是 (10,-1)
再吃一颗,就只能是 (8,0)。。。所以这样 最终状态是(0,5)和(5,0),每个状态概率1/2^15,这好像也不是吧

使用道具 举报

回复
论坛徽章:
548
生肖徽章2007版:猴
日期:2008-05-16 11:28:59生肖徽章2007版:马
日期:2008-10-08 17:01:01SQL大赛参与纪念
日期:2011-04-13 12:08:17授权会员
日期:2011-06-17 16:14:53ITPUB元老
日期:2011-06-21 11:47:01ITPUB官方微博粉丝徽章
日期:2011-07-01 09:45:27ITPUB十周年纪念徽章
日期:2011-09-27 16:30:472012新春纪念徽章
日期:2012-01-04 11:51:222012新春纪念徽章
日期:2020-11-30 22:13:24海蓝宝石
日期:2012-02-20 19:24:27
38#
发表于 2020-11-29 10:45 | 只看该作者
# 4

SQL>
SQL> with t(lvl,A,B) as (select 0,10,10 from dual
  2                       union all
  3                      select lvl+1,
  4                             case when n=1 then A-1 else A end,
  5                             case when n=2 then B-1 else B end
  6                       from t,(select 1 n from dual union all select 2 from dual) s
  7                      where lvl < 15
  8                     ),
  9        s as (
10              select count(case when (A=5 and B=0) or (A=0 and B=5) then 1 end) cnt,
11                     count(*) total_cnt
12                from t
13                where lvl = 15 and A >=0 AND B>=0
14              ),
15  f(total_cnt,cnt) as ( select total_cnt,cnt from s
16                                 union all
17                                select greatest(total_cnt - cnt,cnt),least(total_cnt - cnt,cnt)
18                                  from f
19                                where total_cnt <> cnt)
20          select s.cnt/f.cnt||'/'||s.total_cnt/f.cnt as res
21            from s,f
22           where f.total_cnt = f.cnt
23  /

RES
--------------------------------------------------------------------------------
21/**

不知道这个是不是对的。。。

使用道具 举报

回复
论坛徽章:
548
生肖徽章2007版:猴
日期:2008-05-16 11:28:59生肖徽章2007版:马
日期:2008-10-08 17:01:01SQL大赛参与纪念
日期:2011-04-13 12:08:17授权会员
日期:2011-06-17 16:14:53ITPUB元老
日期:2011-06-21 11:47:01ITPUB官方微博粉丝徽章
日期:2011-07-01 09:45:27ITPUB十周年纪念徽章
日期:2011-09-27 16:30:472012新春纪念徽章
日期:2012-01-04 11:51:222012新春纪念徽章
日期:2020-11-30 22:13:24海蓝宝石
日期:2012-02-20 19:24:27
39#
发表于 2020-11-29 10:46 | 只看该作者
SQL> with t(lvl,A,B) as (select 0,10,10 from dual
  2                       union all
  3                      select lvl+1,
  4                             case when n=1 then A-1 else A end,
  5                             case when n=2 then B-1 else B end
  6                       from t,(select 1 n from dual union all select 2 from dual) s
  7                      where lvl < 15
  8                     ),
  9        s as (
10              select count(case when (A=5 and B=0) or (A=0 and B=5) then 1 end) cnt,
11                     count(*) total_cnt
12                from t
13                where lvl = 15 and A >=0 AND B>=0
14              ),
15  f(total_cnt,cnt) as ( select total_cnt,cnt from s
16                                 union all
17                                select greatest(total_cnt - cnt,cnt),least(total_cnt - cnt,cnt)
18                                  from f
19                                where total_cnt <> cnt)
20          select s.cnt/f.cnt||'/'||s.total_cnt/f.cnt as res
21            from s,f
22           where f.total_cnt = f.cnt
23  /

RES
--------------------------------------------------------------------------------
21 /1 0 1

使用道具 举报

回复
论坛徽章:
548
生肖徽章2007版:猴
日期:2008-05-16 11:28:59生肖徽章2007版:马
日期:2008-10-08 17:01:01SQL大赛参与纪念
日期:2011-04-13 12:08:17授权会员
日期:2011-06-17 16:14:53ITPUB元老
日期:2011-06-21 11:47:01ITPUB官方微博粉丝徽章
日期:2011-07-01 09:45:27ITPUB十周年纪念徽章
日期:2011-09-27 16:30:472012新春纪念徽章
日期:2012-01-04 11:51:222012新春纪念徽章
日期:2020-11-30 22:13:24海蓝宝石
日期:2012-02-20 19:24:27
40#
发表于 2020-11-29 10:56 | 只看该作者
38楼好像也不对,一盒吃完,另一盒可能剩10颗,9,8,。。。,0 都要考虑;  而且剩余5颗糖的时候,可能两个盒子都有。   都只考虑了后一半,前一半都没有考虑。所以不对。

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表