楼主: newkid

[每日一题] puzzleup 2021

[复制链接]
论坛徽章:
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
321#
发表于 2021-11-5 21:50 | 只看该作者
〇〇 发表于 2021-11-3 21:57
cte可以模拟,但不知道怎么计算平均值

CTE 检测有循环,不好使。。。

使用道具 举报

回复
论坛徽章:
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
322#
发表于 2021-11-5 21:51 | 只看该作者
# 10
模拟

SQL> with function f_dart
  2  return int
  3  is
  4  
  5    type t_seg is table of int index by pls_integer;
  6    l_seg t_seg;
  7  
  8    l_total int := power(2,20)-1;
  9    l_bit_sum int;
10    l_sign    int;
11    l_tmp     int;
12  begin
13  
14    l_seg(1) := trunc(dbms_random.value(1,21));
15    l_bit_sum := power(2,l_seg(1)-1);
16  
17    while (l_bit_sum < l_total) loop
18  
19      l_sign := trunc(dbms_random.value(1,3));
20      l_tmp := case when l_sign =1 then
21                       case when l_seg(l_seg.count) = 1 then 20
22                            else l_seg(l_seg.count) - 1
23                       end
24                     else
25                       case when l_seg(l_seg.count) = 20 then 1
26                            else l_seg(l_seg.count) + 1
27                       end
28                end;
29  
30      l_seg(l_seg.count+1) := l_tmp;
31  
32      if bitand(l_bit_sum,power(2,l_tmp -1)) =0 then
33        l_bit_sum := l_bit_sum + power(2,l_tmp -1);
34      end if;
35    end loop;
36  
37    return l_seg.count;
38  end;
39  select avg(f_dart) from dual connect by level <= 100000
40  /

AVG(F_DART)
-----------
  190.81293

SQL> /

AVG(F_DART)
-----------
  191.16794

SQL> /

AVG(F_DART)
-----------
   191.2911

SQL> /

AVG(F_DART)
-----------
  190.58309

应该是 191

使用道具 举报

回复
论坛徽章:
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
323#
发表于 2021-11-5 22:38 来自手机 | 只看该作者
我写cte死循环就放弃了

使用道具 举报

回复
论坛徽章:
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
324#
发表于 2021-11-6 08:35 来自手机 | 只看该作者
第10题感觉就是(n-1)*n/2

使用道具 举报

回复
论坛徽章:
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
325#
发表于 2021-11-6 08:36 来自手机 | 只看该作者
照搬猫猫的思路

import random

def game(n):
  total=pow(2,n)-1
  sum=1
  i=0
  last=1
  while sum<total:
    i+=1
    sig=round(random.random()*100000)%2
    if sig==0:
      last-=1
    else:
      last+=1
    if last>n:
      last=1
    elif last<1:
      last=n
    #print(last,end=',')
    sum |=pow(2,last-1)
  #print("i=",i)
  return i


def p(m,n):
  s=0
  for j in range(m):
    s+=game(n)
  return s/m

#p(10000,20)

for i in range(1,21)rint(i,round(p(1000*i,i)))

'''
1 0
2 1
3 3
4 6
5 10
6 15
7 21
8 28
9 36
10 45
11 55
12 66
13 78
14 91
15 104
16 120
17 135
18 153
19 171
20 191
'''

使用道具 举报

回复
论坛徽章:
520
奥运会纪念徽章:垒球
日期: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
326#
 楼主| 发表于 2021-11-6 09:06 来自手机 | 只看该作者
2和3答案就不对了。

使用道具 举报

回复
论坛徽章:
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
327#
发表于 2021-11-6 10:31 | 只看该作者
我改参数后,1-20的模拟值

SQL> with function f_dart (p_n in int)
  2   return int
  3   is
  4  
  5     type t_seg is table of int index by pls_integer;
  6     l_seg t_seg;
  7  
  8     l_total int := power(2,p_n)-1;
  9     l_bit_sum int;
10    l_sign    int;
11    l_tmp     int;
12  begin
13  
14    l_seg(1) := trunc(dbms_random.value(1,p_n + 1));
15    l_bit_sum := power(2,l_seg(1)-1);
16  
17    while (l_bit_sum < l_total) loop
18  
19      l_sign := trunc(dbms_random.value(1,3));
20      l_tmp := case when l_sign =1 then
21                       case when l_seg(l_seg.count) = 1 then p_n
22                            else l_seg(l_seg.count) - 1
23                       end
24                     else
25                       case when l_seg(l_seg.count) = p_n then 1
26                            else l_seg(l_seg.count) + 1
27                       end
28                end;
29  
30      l_seg(l_seg.count+1) := l_tmp;
31  
32      if bitand(l_bit_sum,power(2,l_tmp -1)) =0 then
33        l_bit_sum := l_bit_sum + power(2,l_tmp -1);
34      end if;
35    end loop;
36  
37    return l_seg.count;
38  end;
39  t(n) as (select level from dual connect by level <= 20)
40  select n,(select round(avg(f_dart(n))) from dual connect by level <= 100000) avg_cnt
41    from t
42  /

         N    AVG_CNT
---------- ----------
         1          1
         2          2
         3          4
         4          7
         5         11
         6         16
         7         22
         8         29
         9         37
        10         46
        11         56
        12         67
        13         79
        14         92
        15        106
        16        120
        17        137
        18        154
        19        173
        20        191

20 rows selected

使用道具 举报

回复
论坛徽章:
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
328#
发表于 2021-11-6 10:38 | 只看该作者
〇〇 发表于 2021-11-6 08:35
第10题感觉就是(n-1)*n/2

纠正一下,通项公式应该是: a(n) = 1 + n*(n-1)/2

使用道具 举报

回复
论坛徽章:
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
329#
发表于 2021-11-6 10:41 | 只看该作者
newkid 发表于 2021-11-6 09:06
2和3答案就不对了。

加个 1 就应该是对的

使用道具 举报

回复
论坛徽章:
0
330#
发表于 2021-11-6 22:48 来自手机 | 只看该作者
Let me try first few steps and see what happens.  For group of 4, if 2/2 is even and each side is 1a+1b then 1a+1b<>2a (from any other groups) and all other groups only contains a and almost done.  Otherwise all  are a in group of 4.  If 2/2 are uneven, one side contains 1 or 2 and the other side contains 2a.  Now take 2 from any other group and compare with side 1 and side 2.  Since the new picked 2 can be either 2a or 1a+1b, side 1 or side 2 must be equal to the new picked 2.  The following need paper or better write a small program to track how many steps have been taken.  I read a article in sins.com about quality control using AI.  So basically it is the same topic but the method should be practical and scalable.

使用道具 举报

回复

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

本版积分规则 发表回复

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