楼主: newkid

[精华] 趣味SQL:微软面试题:过河

[复制链接]
论坛徽章:
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
51#
 楼主| 发表于 2012-3-29 10:28 | 只看该作者
guostong 发表于 2012-3-29 10:22
此话怎讲?

DB2也有递归WITH.

你再来写一个容量不限的?比如每次最多可过:N个人。

使用道具 举报

回复
论坛徽章:
3
ITPUB十周年纪念徽章
日期:2011-11-01 16:24:51蜘蛛蛋
日期:2012-04-19 16:07:402013年新春福章
日期:2013-02-25 14:51:24
52#
发表于 2012-3-29 14:31 | 只看该作者
guostong 发表于 2012-3-29 10:22
此话怎讲?

我之前只见过db2的递归with,以为是db2的语句呢。刚才知道是11gR2的新特性。 我还停留在10g呢。

使用道具 举报

回复
论坛徽章:
4
紫蛋头
日期:2011-06-30 09:52:10ITPUB十周年纪念徽章
日期:2011-11-01 16:25:22咸鸭蛋
日期:2012-04-19 16:07:532013年新春福章
日期:2013-02-25 14:51:24
53#
发表于 2012-3-29 16:52 | 只看该作者
好吧。。。我用最烂的方法实现了,再多个人我就要死了。。。
CREATE TABLE bridge_crossing (
        name  NUMBER PRIMARY KEY
       ,time  NUMBER
       );
      
INSERT INTO bridge_crossing VALUES (1, 1);
INSERT INTO bridge_crossing VALUES (2, 2);
INSERT INTO bridge_crossing VALUES (3, 5);
INSERT INTO bridge_crossing VALUES (4, 10);
COMMIT;


with b as
(select c.onecol1,c.onecol2,d.name ret
         from bridge_crossing d,
        (select a.name onecol1 ,b.name onecol2  from bridge_crossing a,bridge_crossing b where a.name<>b.name) c --组合第一次去的人
         where c.onecol1=d.name or c.onecol2=d.name
), --组合第一次去加回来的人
  c as
(select b.onecol1,b.onecol2,b.ret,g.name from bridge_crossing g,b where g.name<>(case when b.onecol1<>b.ret then b.onecol1 else b.onecol2 end))

, d as
(
select x.*,x.name second1,m.name second2  from c x,c m where x.name<>m.name
)
--组合第二次去的可能性 onecol1和onecol2代表第一次去的组合,ret代表回来的人,second1和second2代表第二次去的人
,
e as (
select d.onecol1,d.onecol2,d.ret,d.second1,d.second2 from d where
  (case when d.onecol1<>d.ret then d.onecol1 else d.onecol2 end)<>d.second1
and (case when d.onecol1<>d.ret then d.onecol1 else d.onecol2 end)<>d.second2 --这里排除不可能的情况
group by d.onecol1,d.onecol2,d.ret,d.second1,d.second2
order by d.onecol1,d.onecol2,d.ret
)
/* select * from e*/
,h as (
  select e.*, f.name ret2 from e, bridge_crossing f where f.name in ((case when e.onecol1=e.ret then e.onecol2 else e.onecol1 end),e.second1,e.second2)
),
o as (
select h.*,j.name from h ,bridge_crossing j where
/*(j.name = (case when h.onecol1=h.ret  then h.onecol1 else h.onecol2 end) and j.name= (case when h.second1=ret2 then second1 else second2 end))
and*/ j.name<>( case when h.onecol1= h.ret  then h.onecol2 else h.onecol1 end )
and (j.name = (case when ret not in (second1,second2) then ret end) or (j.name not in (onecol1,onecol2,second1,second2))) --这里最后一个人是第一次去回来后第二次没去的人,或者一次都没去的人
and j.name<>( case when h.second1= h.ret2  then h.second2 else h.second1 end )
and j.name<>h.ret2
)
select o.*, case when onecol1>onecol2 then onecol1 else onecol2 end|| ret
||case when second1>second2 then second1 else second2 end ||ret2||case when ret2>name then ret2 else name end as time,
decode(case when onecol1>onecol2 then onecol1 else onecol2 end,1,1,2,2,3,5,4,10)+ decode(ret,1,1,2,2,3,5,4,10)
+decode(case when second1>second2 then second1 else second2 end,1,1,2,2,3,5,4,10)+decode(ret2,1,1,2,2,3,5,4,10)+
decode(case when ret2>name then ret2 else name end,1,1,2,2,3,5,4,10) as sumtime
  from o
order by sumtime

结果:
1        2        1        3        4        2        1        21422        17
1        2        1        4        3        2        1        21422        17
1        2        2        3        4        1        2        22412        17
1        2        2        4        3        1        2        22412        17
2        1        1        3        4        2        1        21422        17
2        1        1        4        3        2        1        21422        17
2        1        2        3        4        1        2        22412        17
2        1        2        4        3        1        2        22412        17
1        2        1        1        3        1        4        21314        19
1        2        1        1        4        1        3        21413        19
1        2        1        3        1        1        4        21314        19
1        2        1        4        1        1        3        21413        19
1        3        1        1        2        1        4        31214        19
1        3        1        1        4        1        2        31412        19
1        3        1        2        1        1        4        31214        19
1        3        1        4        1        1        2        31412        19
1        4        1        1        2        1        3        41213        19
1        4        1        1        3        1        2        41312        19
1        4        1        2        1        1        3        41213        19

后面还有很多组合。。。不知道对不对。。

使用道具 举报

回复
论坛徽章:
69
生肖徽章2007版:羊
日期:2008-11-14 14:42:19复活蛋
日期:2011-08-06 08:59:05ITPUB十周年纪念徽章
日期:2011-11-01 16:19:412012新春纪念徽章
日期:2012-01-04 11:49:542012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:20版主4段
日期:2012-05-15 15:24:11
54#
发表于 2012-3-29 17:06 | 只看该作者
这个问题我觉得只要速度最快的一个人作为手电筒传递人员, 就应该是最快的呀.

使用道具 举报

回复
论坛徽章:
69
生肖徽章2007版:羊
日期:2008-11-14 14:42:19复活蛋
日期:2011-08-06 08:59:05ITPUB十周年纪念徽章
日期:2011-11-01 16:19:412012新春纪念徽章
日期:2012-01-04 11:49:542012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:20版主4段
日期:2012-05-15 15:24:11
55#
发表于 2012-3-29 17:13 | 只看该作者
nyfor 发表于 2012-3-29 17:06
这个问题我觉得只要速度最快的一个人作为手电筒传递人员, 就应该是最快的呀.

晕,我太想当然了

使用道具 举报

回复
论坛徽章:
3
ITPUB十周年纪念徽章
日期:2011-11-01 16:24:51蜘蛛蛋
日期:2012-04-19 16:07:402013年新春福章
日期:2013-02-25 14:51:24
56#
发表于 2012-3-29 19:33 | 只看该作者
从网上搜了一个结论:
1.过河时间最短的和次数最短的人先过。2.在已过的人中最短时间的人返回。3.过河时间最长和次最长的人过河。4.在已过的人中次最短时间的人返回。5.在剩下的人中过河时间最短和次最短的人过河。。。重复以上的过程即可。

基本知识: 1.M个人过河,船上能载N个人,由于需要一人划船,故共需过河M-1N-1次(分子、分母分别减“1”是因为需要1个人划船,如果需要n个人划船就要同时减去n); 2.“过一次河”指的是单程,“往返一次”指的是双程;3.载人过河的时候,最后一次不再需要返回。

使用道具 举报

回复
论坛徽章:
6
2011新春纪念徽章
日期:2011-01-04 10:35:482011新春纪念徽章
日期:2011-02-18 11:43:32ITPUB十周年纪念徽章
日期:2011-11-01 16:21:152012新春纪念徽章
日期:2012-01-04 11:51:222013年新春福章
日期:2013-02-25 14:51:24兰博基尼
日期:2013-11-01 13:10:24
57#
发表于 2012-3-29 20:45 | 只看该作者
太复杂了。。。只有围观的份

使用道具 举报

回复
论坛徽章:
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
58#
 楼主| 发表于 2012-3-29 21:08 | 只看该作者
changhe325 发表于 2012-3-29 19:33
从网上搜了一个结论:
1.过河时间最短的和次数最短的人先过。2.在已过的人中最短时间的人返回。3.过河时间 ...

可恶的人肉!把我们动脑筋的乐趣都剥夺了。
即使这样,用SQL实现这个算法也是不错的练习题!

使用道具 举报

回复
论坛徽章:
38
授权会员
日期:2005-10-30 17:05:332012新春纪念徽章
日期:2012-02-13 15:12:09现任管理团队成员
日期:2011-11-07 09:46:59ITPUB十周年纪念徽章
日期:2011-11-01 16:19:41ITPUB9周年纪念徽章
日期:2010-10-08 09:31:21版主3段
日期:2012-05-15 15:24:112009新春纪念徽章
日期:2009-01-04 14:52:282010新春纪念徽章
日期:2010-03-01 11:06:202009日食纪念
日期:2009-07-22 09:30:00祖国60周年纪念徽章
日期:2009-10-09 08:28:00
59#
发表于 2012-3-29 21:44 | 只看该作者
newkid 发表于 2012-3-28 21:28
DB2也有递归WITH.

你再来写一个容量不限的?比如每次最多可过:N个人。

试试,感觉需要套一个递归或connect by

使用道具 举报

回复
论坛徽章:
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
60#
 楼主| 发表于 2012-3-30 04:15 | 只看该作者
N个人的做法,按照二进制位的思路也很容易写出来。

56楼的算法,碰到容量为3的就不符合了:
ACD  ---- A ----- AB   总共13 分钟。

照算法第一步应该是ABC先过:
ABC ---- A ---- AD 总共 16 分钟。
应该如何来调整这个算法?

使用道具 举报

回复

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

本版积分规则 发表回复

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