楼主: newkid

[每日一题] puzzleup 2018

[复制链接]
论坛徽章:
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
61#
发表于 2018-8-24 09:36 | 只看该作者
newkid 发表于 2018-8-23 21:31
给你们俩一人发个章吧,要什么直接点。

九尾狐狸

使用道具 举报

回复
论坛徽章:
2
紫蜘蛛
日期:2018-08-24 21:46:44玉兔
日期:2018-09-07 22:04:20
62#
发表于 2018-8-24 10:55 | 只看该作者
newkid 发表于 2018-8-23 21:31
给你们俩一人发个章吧,要什么直接点。

紫蜘蛛

使用道具 举报

回复
论坛徽章:
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
63#
 楼主| 发表于 2018-8-24 21:48 | 只看该作者
你们真识货,都是绝版的徽章。

再来一个SQL题,昨天在微信上看到的:
三个水桶, 容量分别为3升,5升,8升, 8升桶装满了水,其他两个为空。
如何通过最少次数将水平均分成两份,4+4放在5和8的桶里。

三个水桶的水量按照从小桶到大桶排列,初始状态是 008, 终止状态是 044, 用SQL找出这个变化路径。

使用道具 举报

回复
论坛徽章:
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
64#
 楼主| 发表于 2018-8-24 22:30 | 只看该作者
自己先玩一把:
PATH
-----------------------------------------------------------------------------------------------
008->053->323->026->206->251->341->044
008->305->035->332->152->107->017->314->044
008->305->350->053->323->026->206->251->341->044
008->305->035->053->323->026->206->251->341->044
008->053->323->305->035->332->152->107->017->314->044
008->053->350->305->035->332->152->107->017->314->044
008->053->323->350->305->035->332->152->107->017->314->044
008->305->035->332->152->053->323->026->206->251->341->044
008->305->035->332->350->053->323->026->206->251->341->044
008->053->323->026->206->305->035->332->152->107->017->314->044
008->305->035->332->152->350->053->323->026->206->251->341->044
008->305->035->332->152->107->017->053->323->026->206->251->341->044
008->053->323->026->206->251->341->305->035->332->152->107->017->314->044
008->053->323->026->206->251->350->305->035->332->152->107->017->314->044
008->053->323->026->206->251->341->350->305->035->332->152->107->017->314->044
008->305->035->332->152->107->017->314->350->053->323->026->206->251->341->044

16 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
65#
发表于 2018-8-25 11:01 | 只看该作者
newkid 发表于 2018-8-24 21:48
你们真识货,都是绝版的徽章。

再来一个SQL题,昨天在微信上看到的:

SQL>   with r(a,b,c) as (select 1,-1,0 from dual
  2                       union all
  3                      select 1,0,-1 from dual
  4                       union all
  5                      select -1,0,1 from dual
  6                       union all
  7                      select -1,1,0 from dual
  8                       union all
  9                      select 0,1,-1 from dual
10                       union all
11                      select 0,-1,1 from dual),
12  t(n,x,y,z,path,node) as (select 1,
13                             0,
14                             0,
15                             8,
16                             '008',
17                             '008'
18                        from dual
19                       union all
20                      select t.n+1,
21                             t.x + r.a*s.v x,
22                             t.y + r.b*s.v y,
23                             t.z + r.c*s.v z,
24                             t.path||'->'||to_char(t.x + r.a*s.v)||to_char(t.y + r.b*s.v)||to_char(t.z + r.c*s.v),
25                             to_char(t.x + r.a*s.v)||to_char(t.y + r.b*s.v)||to_char(t.z + r.c*s.v)
26                        from t,
27                             (select level v from dual connect by level<=5) s,
28                             r
29                       where s.v in (3-t.x,5-t.y,8-t.z,t.x,t.y,t.z)
30                         and t.x + r.a*s.v + t.y + r.b*s.v + t.z + r.c*s.v = 8
31                         and t.x + r.a*s.v between 0 and 3
32                         and t.y + r.b*s.v between 0 and 5
33                         and t.z + r.c*s.v between 0 and 8
34                         and ( case when r.a = 1 and r.b=-1 then t.x + r.a*s.v - 3
35                                    when r.a = 1 and r.c=-1 then t.x + r.a*s.v - 3
36                                    when r.b = 1 and r.a=-1 then t.y + r.b*s.v - 5
37                                    when r.b = 1 and r.c=-1 then t.y + r.b*s.v - 5
38                                    when r.c = 1 and r.a=-1 then t.z + r.c*s.v - 8
39                                    when r.c = 1 and r.b=-1 then t.z + r.c*s.v - 8
40                               end = 0
41                             or
42                               case when r.a = 1 and r.b=-1 then t.y + r.b*s.v
43                                    when r.a = 1 and r.c=-1 then t.z + r.c*s.v
44                                    when r.b = 1 and r.a=-1 then t.x + r.a*s.v
45                                    when r.b = 1 and r.c=-1 then t.z + r.c*s.v
46                                    when r.c = 1 and r.a=-1 then t.x + r.a*s.v
47                                    when r.c = 1 and r.b=-1 then t.y + r.b*s.v
48                               end = 0)
49                         )
50  cycle node set flag to 'Y' default 'N'
51  select path from t where flag = 'N' and node = '044'
52  /
PATH
--------------------------------------------------------------------------------
008->053->323->026->206->251->341->044
008->305->035->332->152->107->017->314->044
008->305->035->053->323->026->206->251->341->044
008->305->350->053->323->026->206->251->341->044
008->053->323->305->035->332->152->107->017->314->044
008->053->350->305->035->332->152->107->017->314->044
008->305->035->332->152->053->323->026->206->251->341->044
008->305->035->332->350->053->323->026->206->251->341->044
008->053->323->350->305->035->332->152->107->017->314->044
008->305->035->332->152->350->053->323->026->206->251->341->044
008->053->323->026->206->305->035->332->152->107->017->314->044
008->305->035->332->152->107->017->053->323->026->206->251->341->044
008->053->323->026->206->251->341->305->035->332->152->107->017->314->044
008->053->323->026->206->251->350->305->035->332->152->107->017->314->044
008->305->035->332->152->107->017->314->350->053->323->026->206->251->341->044
008->053->323->026->206->251->341->350->305->035->332->152->107->017->314->044
16 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
66#
发表于 2018-8-25 11:08 | 只看该作者
newkid 发表于 2018-8-24 22:30
自己先玩一把:
PATH
------------------------------------------------------------------------------ ...

你应该再奖励我一个章,这个有点费脑筋

使用道具 举报

回复
论坛徽章:
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
67#
 楼主| 发表于 2018-8-26 02:05 | 只看该作者
solomon_007 发表于 2018-8-25 11:08
你应该再奖励我一个章,这个有点费脑筋

好吧,既然陪我玩,就奖励你一个。

12C写法:
with
move AS (
   SELECT p  ---- 源水桶编号
         ,m  ---- 目标水桶编号
         ,'358' c ---- 水桶容量
    FROM (SELECT LEVEL p FROM DUAL CONNECT BY LEVEL<=3),(SELECT LEVEL m FROM DUAL CONNECT BY LEVEL<=3)
    WHERE p<>m
    )
,t(s,path) as (
select '008', CAST('008' AS VARCHAR2(1000)) FROM DUAL
UNION ALL
SELECT new_s
      ,path||'->'||new_s
  FROM t
      ,move
      ,LATERAL(SELECT  SUBSTR(s,p,1)-LEAST(SUBSTR(s,p,1),SUBSTR(c,m,1)-SUBSTR(s,m,1)) ps
                      ,SUBSTR(s,m,1)+LEAST(SUBSTR(s,p,1),SUBSTR(c,m,1)-SUBSTR(s,m,1)) ms
                FROM DUAL
               )
      ,LATERAL(SELECT  DECODE(1,p,ps,m,ms,SUBSTR(s,1,1))
                      ||DECODE(2,p,ps,m,ms,SUBSTR(s,2,1))
                      ||DECODE(3,p,ps,m,ms,SUBSTR(s,3,1)) new_s
                FROM DUAL
               )
WHERE s<>'044'
       AND SUBSTR(t.s,p,1)<>0 -----源水桶有水
       AND SUBSTR(t.s,m,1)<SUBSTR(c,m,1) -----目标水桶有空
       AND INSTR(path,new_s
                )=0
)
select path from t where s='044'
;


with
move AS (
   SELECT p  ---- 源水桶编号
         ,m  ---- 目标水桶编号
         ,'358' c ---- 水桶容量
    FROM (SELECT LEVEL p FROM DUAL CONNECT BY LEVEL<=3),(SELECT LEVEL m FROM DUAL CONNECT BY LEVEL<=3)
    WHERE p<>m
    )
,t(s,path) as (
select '008', CAST('008' AS VARCHAR2(1000)) FROM DUAL
UNION ALL
SELECT new_s
      ,path||'->'||new_s
  FROM t
       JOIN move ON s<>'044'
                 AND SUBSTR(t.s,p,1)<>0 -----源水桶有水
                 AND SUBSTR(t.s,m,1)<SUBSTR(c,m,1) -----目标水桶有空
       CROSS APPLY (SELECT  SUBSTR(s,p,1)-LEAST(SUBSTR(s,p,1),SUBSTR(c,m,1)-SUBSTR(s,m,1)) ps
                      ,SUBSTR(s,m,1)+LEAST(SUBSTR(s,p,1),SUBSTR(c,m,1)-SUBSTR(s,m,1)) ms
                     FROM DUAL
                    )
       CROSS APPLY (SELECT  DECODE(1,p,ps,m,ms,SUBSTR(s,1,1))
                      ||DECODE(2,p,ps,m,ms,SUBSTR(s,2,1))
                      ||DECODE(3,p,ps,m,ms,SUBSTR(s,3,1)) new_s
                     FROM DUAL
                    )
WHERE INSTR(path,new_s
                )=0
)
select path from t where s='044'
;

11G写法很啰嗦:

with
move AS (
   SELECT p  ---- 源水桶编号
         ,m  ---- 目标水桶编号
         ,'358' c ---- 水桶容量
    FROM (SELECT LEVEL p FROM DUAL CONNECT BY LEVEL<=3),(SELECT LEVEL m FROM DUAL CONNECT BY LEVEL<=3)
    WHERE p<>m
    )
,t(s,path) as (
select '008', CAST('008' AS VARCHAR2(1000)) FROM DUAL
UNION ALL
SELECT DECODE(1,p,SUBSTR(s,p,1)-LEAST(SUBSTR(s,p,1),SUBSTR(c,m,1)-SUBSTR(s,m,1)),m,SUBSTR(s,m,1)+LEAST(SUBSTR(s,p,1),SUBSTR(c,m,1)-SUBSTR(s,m,1)),SUBSTR(s,1,1))
       ||DECODE(2,p,SUBSTR(s,p,1)-LEAST(SUBSTR(s,p,1),SUBSTR(c,m,1)-SUBSTR(s,m,1)),m,SUBSTR(s,m,1)+LEAST(SUBSTR(s,p,1),SUBSTR(c,m,1)-SUBSTR(s,m,1)),SUBSTR(s,2,1))
       ||DECODE(3,p,SUBSTR(s,p,1)-LEAST(SUBSTR(s,p,1),SUBSTR(c,m,1)-SUBSTR(s,m,1)),m,SUBSTR(s,m,1)+LEAST(SUBSTR(s,p,1),SUBSTR(c,m,1)-SUBSTR(s,m,1)),SUBSTR(s,3,1))
      ,path||'->'||DECODE(1,p,SUBSTR(s,p,1)-LEAST(SUBSTR(s,p,1),SUBSTR(c,m,1)-SUBSTR(s,m,1)),m,SUBSTR(s,m,1)+LEAST(SUBSTR(s,p,1),SUBSTR(c,m,1)-SUBSTR(s,m,1)),SUBSTR(s,1,1))
       ||DECODE(2,p,SUBSTR(s,p,1)-LEAST(SUBSTR(s,p,1),SUBSTR(c,m,1)-SUBSTR(s,m,1)),m,SUBSTR(s,m,1)+LEAST(SUBSTR(s,p,1),SUBSTR(c,m,1)-SUBSTR(s,m,1)),SUBSTR(s,2,1))
       ||DECODE(3,p,SUBSTR(s,p,1)-LEAST(SUBSTR(s,p,1),SUBSTR(c,m,1)-SUBSTR(s,m,1)),m,SUBSTR(s,m,1)+LEAST(SUBSTR(s,p,1),SUBSTR(c,m,1)-SUBSTR(s,m,1)),SUBSTR(s,3,1))
  FROM t
      ,move
WHERE s<>'044'
       AND SUBSTR(t.s,p,1)<>0 -----源水桶有水
       AND SUBSTR(t.s,m,1)<SUBSTR(c,m,1) -----目标水桶有空
       AND INSTR(path,DECODE(1,p,SUBSTR(s,p,1)-LEAST(SUBSTR(s,p,1),SUBSTR(c,m,1)-SUBSTR(s,m,1)),m,SUBSTR(s,m,1)+LEAST(SUBSTR(s,p,1),SUBSTR(c,m,1)-SUBSTR(s,m,1)),SUBSTR(s,1,1))
       ||DECODE(2,p,SUBSTR(s,p,1)-LEAST(SUBSTR(s,p,1),SUBSTR(c,m,1)-SUBSTR(s,m,1)),m,SUBSTR(s,m,1)+LEAST(SUBSTR(s,p,1),SUBSTR(c,m,1)-SUBSTR(s,m,1)),SUBSTR(s,2,1))
       ||DECODE(3,p,SUBSTR(s,p,1)-LEAST(SUBSTR(s,p,1),SUBSTR(c,m,1)-SUBSTR(s,m,1)),m,SUBSTR(s,m,1)+LEAST(SUBSTR(s,p,1),SUBSTR(c,m,1)-SUBSTR(s,m,1)),SUBSTR(s,3,1))
                )=0
)
select * from t where s='044'
;

11g 模拟12C LATERAL的写法:


with
move AS (
   SELECT p  ---- 源水桶编号
         ,m  ---- 目标水桶编号
         ,'358' c ---- 水桶容量
    FROM (SELECT LEVEL p FROM DUAL CONNECT BY LEVEL<=3),(SELECT LEVEL m FROM DUAL CONNECT BY LEVEL<=3)
    WHERE p<>m
    )
,t(s,path) as (
select '008', CAST('008' AS VARCHAR2(1000)) FROM DUAL
UNION ALL
SELECT new_s.column_value
      ,path||'->'||new_s.column_value
  FROM t
      ,move
      ,TABLE(CAST(MULTISET(SELECT SUBSTR(s,p,1)-LEAST(SUBSTR(s,p,1),SUBSTR(c,m,1)-SUBSTR(s,m,1)) FROM DUAL) AS SYS.ODCIVARCHAR2LIST)) ps
      ,TABLE(CAST(MULTISET(SELECT SUBSTR(s,m,1)+LEAST(SUBSTR(s,p,1),SUBSTR(c,m,1)-SUBSTR(s,m,1)) FROM DUAL) AS SYS.ODCIVARCHAR2LIST)) ms
      ,TABLE(CAST(MULTISET(SELECT  DECODE(1,p,ps.column_value,m,ms.column_value,SUBSTR(s,1,1))
                                  ||DECODE(2,p,ps.column_value,m,ms.column_value,SUBSTR(s,2,1))
                                  ||DECODE(3,p,ps.column_value,m,ms.column_value,SUBSTR(s,3,1))  FROM DUAL) AS SYS.ODCIVARCHAR2LIST)) new_s
WHERE s<>'044'
       AND SUBSTR(t.s,p,1)<>0 -----源水桶有水
       AND SUBSTR(t.s,m,1)<SUBSTR(c,m,1) -----目标水桶有空
       AND INSTR(path,new_s.column_value
                )=0
)
select path from t where s='044'
;

使用道具 举报

回复
论坛徽章:
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
68#
发表于 2018-8-26 06:23 来自手机 | 只看该作者
真会玩

使用道具 举报

回复
论坛徽章:
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
69#
发表于 2018-8-26 12:35 | 只看该作者
newkid 发表于 2018-8-26 02:05
好吧,既然陪我玩,就奖励你一个。

12C写法:

给个“举人”

你一下玩出了这么多写法!!

使用道具 举报

回复
论坛徽章:
8
玉兔
日期:2015-11-16 10:18:00铁扇公主
日期:2015-10-27 21:47:42九尾狐狸
日期:2015-12-11 22:31:15
70#
发表于 2018-8-27 08:57 | 只看该作者
倒个100升看看,桶大小分别是 43, 59, 100, 分成 50,50

使用道具 举报

回复

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

本版积分规则 发表回复

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