楼主: ~贝贝~

[精华] “盛拓传媒杯”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
51#
发表于 2011-3-16 08:43 | 只看该作者
原帖由 lastwinner 于 2011-3-16 00:12 发表


C33,这个你漏掉了



喔发现了,谢谢,死的不冤了。。。

使用道具 举报

回复
论坛徽章:
131
2006年度最佳技术回答
日期:2007-01-24 12:58:48福特
日期:2013-10-24 13:57:422014年新春福章
日期:2014-02-18 16:41:11马上有车
日期:2014-02-18 16:41:11马上有车
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上加薪
日期:2014-02-19 11:55:142013年新春福章
日期:2013-02-25 14:51:24
52#
发表于 2011-3-16 09:11 | 只看该作者
2楼中newkid 11g with解法中最后的一部分其实是有瑕疵的
select rownum,all_rows from (select all_rows  from(select all_rows, rank() over(order by col_cnt desc) rnk from balls  )
where  rnk=1 order by all_rows desc);

缺了一个level(lvl)的限定条件,虽然结果是对的,但是逻辑上少了,应该是

select rownum,all_rows from (select all_rows  from(select all_rows, rank() over(order by col_cnt desc) rnk from balls where lvl = 5  )
where  rnk=1 order by all_rows desc);

3楼的lastwinner的就把那个条件加上了~

[ 本帖最后由 rollingpig 于 2011-3-16 09:13 编辑 ]

使用道具 举报

回复
论坛徽章:
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
53#
发表于 2011-3-16 09:18 | 只看该作者
原帖由 rollingpig 于 2011-3-16 09:11 发表
2楼中newkid 11g with解法中最后的一部分其实是有瑕疵的
select rownum,all_rows from (select all_rows  from(select all_rows, rank() over(order by col_cnt desc) rnk from balls  )
where  rnk=1 order by all_rows desc);

缺了一个level(lvl)的限定条件,虽然结果是对的,但是逻辑上少了,应该是

select rownum,all_rows from (select all_rows  from(select all_rows, rank() over(order by col_cnt desc) rnk from balls where lvl = 5  )
where  rnk=1 order by all_rows desc);

3楼的lastwinner的就把那个条件加上了~

这个是我画蛇添足改坏的.
原文

  1. WITH r AS (    ---------- 一行里放M个球,这M个球所处的列位置的所有组合
  2. SELECT s as one_row
  3.   FROM (SELECT REPLACE(SYS_CONNECT_BY_PATH(x,','),',') s
  4.           FROM (SELECT 1 x FROM DUAL UNION ALL SELECT 0 x FROM DUAL)
  5.          WHERE LEVEL=:N
  6.         CONNECT BY LEVEL<=:N
  7.        )
  8. WHERE LENGTH(s)-NVL(LENGTH(REPLACE(s,'1')),0)=:M
  9. )
  10. ,balls (row_cnt    -- 当前行数
  11.        ,all_rows   -- 截至当前行,所有行的摆法拼接的结果
  12.        ,col_cnt    -- 把每一列看作十进制的一位,此数字表示在列方向的各位和
  13.        ,diag_cnt1  -- 此数字表示在右上至左下的斜线上各位的和
  14.        ,diag_cnt2  -- 此数字表示在左上至右下的斜线上各位的和
  15.        )
  16. AS (
  17.    SELECT 1,one_row,TO_NUMBER(one_row),TO_NUMBER(one_row),TO_NUMBER(one_row) ---初始化第一行数据
  18.      FROM r
  19.    UNION ALL
  20.    SELECT balls.row_cnt+1  ---- 行数递增
  21.          ,balls.all_rows||r.one_row  ---当前行拼入总结果
  22.          ,balls.col_cnt+r.one_row    --- 在列方向各位进行累加
  23.          ,balls.diag_cnt1*10+r.one_row  ---- 在斜线方向各位进行累加
  24.          ,balls.diag_cnt2+r.one_row*POWER(10,balls.row_cnt)
  25.      FROM balls,r
  26.     WHERE balls.row_cnt<:N ---- 递归出口
  27.           AND INSTR(balls.col_cnt+r.one_row,:M+1)=0 ---- 把带M+1的结果拦截掉
  28.           AND INSTR(balls.diag_cnt1*10+r.one_row,:M+1)=0
  29.           AND INSTR(balls.diag_cnt2+r.one_row*POWER(10,balls.row_cnt),:M+1)=0
  30. )
  31. SELECT ROWNUM,all_rows
  32.   FROM (SELECT all_rows
  33.           FROM balls
  34.          WHERE row_cnt=:N
  35.         ORDER BY 1 DESC
  36.        );
复制代码

那个非递归的with也是我参照第2期答案改的,用K排序就妥了,如果存在1行全0的最终解
select rownum,all_rows from (select all_rows  from(select all_rows, rank() over(order by K desc)rnk from balls  )
where  rnk=1 order by all_rows desc);

[ 本帖最后由 〇〇 于 2011-3-16 09:22 编辑 ]

使用道具 举报

回复
论坛徽章:
131
2006年度最佳技术回答
日期:2007-01-24 12:58:48福特
日期:2013-10-24 13:57:422014年新春福章
日期:2014-02-18 16:41:11马上有车
日期:2014-02-18 16:41:11马上有车
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上加薪
日期:2014-02-19 11:55:142013年新春福章
日期:2013-02-25 14:51:24
54#
发表于 2011-3-16 09:31 | 只看该作者
3 楼的lastwinner的code同样有提升空间。
with t1 as
(select rownum-1 c from dual connect by rownum < 3),
t2 as
(select a.c a, b.c b, c.c, d.c d, e.c e
    from t1 a, t1 b, t1 c, t1 d, t1 e
   where a.c + b.c + c.c + d.c + e.c < 3),
t3 as
(select a.a || a.b || a.c || a.d || a.e || b.a || b.b ||
         b.c || b.d || b.e || c.a || c.b || c.c || c.d ||
         c.e || d.a || d.b || d.c || d.d || d.e || e.a ||
         e.b || e.c || e.d || e.e x,
         (a.a + a.b + a.c + a.d + a.e + b.a + b.b +
         b.c + b.d + b.e + c.a + c.b + c.c + c.d +
         c.e + d.a + d.b + d.c + d.d + d.e + e.a +
         e.b + e.c + e.d + e.e) y,
         max(a.a + a.b + a.c + a.d + a.e + b.a + b.b + b.c + b.d + b.e + c.a + c.b + c.c + c.d + c.e + d.a + d.b + d.c + d.d + d.e + e.a + e.b + e.c + e.d + e.e)over() z
    from t2 a, t2 b, t2 c, t2 d, t2 e
   where a.a + b.a + c.a + d.a + e.a < 3
     and a.b + b.b + c.b + d.b + e.b < 3
     and a.c + b.c + c.c + d.c + e.c < 3
     and a.d + b.d + c.d + d.d + e.d < 3
     and a.e + b.e + c.e + d.e + e.e < 3
     and a.c + b.b + c.a < 3
     and a.d + b.c + c.b + d.a < 3
     and a.e + b.d + c.c + d.b + e.a < 3
     and b.e + c.d + d.c + e.b < 3
     and c.e + d.d + e.c < 3
     and a.c + b.d + c.e < 3
     and a.b + b.c + c.d + d.e < 3
     and a.a + b.b + c.c + d.d + e.e < 3
     and b.a + c.b + d.c + e.d < 3
     and c.a + d.b + e.c < 3)
select rank() over(order by x desc)rn ,x from t3 where y = z

最简单的就是在t2中预先计算 a.c+b.c +c.c+d.c + e.c 和 a.c||b.c||c.c||d.c||e.c,避免在笛卡尔积时做这些运算,同时获得代码的精简性和性能,也降低copy paste出错概率

with t1 as   
(select rownum-1 c from dual connect by rownum < 3),
t2 as
(select a.c a, b.c b, c.c, d.c d, e.c e ,a.c+b.c +c.c+d.c + e.c n,a.c||b.c||c.c||d.c||e.c s
    from t1 a, t1 b, t1 c, t1 d, t1 e
   where a.c + b.c + c.c + d.c + e.c < 3) ,
t3 as
(select   a.s || b.s || e.s ||d.s || e.s  x,
        (a.n + b.n + c.n + d.n +  e.n ) y,
         max(a.n + b.n + c.n + d.n +  e.n)over() z

    from t2 a, t2 b, t2 c, t2 d, t2 e
   where a.a + b.a + c.a + d.a + e.a < 3
     and a.b + b.b + c.b + d.b + e.b < 3
     and a.c + b.c + c.c + d.c + e.c < 3
     and a.d + b.d + c.d + d.d + e.d < 3
     and a.e + b.e + c.e + d.e + e.e < 3
     and a.c + b.b + c.a < 3
     and a.d + b.c + c.b + d.a < 3
     and a.e + b.d + c.c + d.b + e.a < 3
     and b.e + c.d + d.c + e.b < 3
     and c.e + d.d + e.c < 3
     and a.c + b.d + c.e < 3
     and a.b + b.c + c.d + d.e < 3
     and a.a + b.b + c.c + d.d + e.e < 3
     and b.a + c.b + d.c + e.d < 3
     and c.a + d.b + e.c < 3)
select rank() over(order by x desc)rn ,x from t3 where y = z

old code:  381 word,     1410  char, run time 5.6 sec
new code:  269  word ,  1051 char, tun time 3.7 sec

使用道具 举报

回复
论坛徽章:
52
SQL数据库编程大师
日期:2011-04-13 12:09:01奥运会纪念徽章:水球
日期:2012-10-08 09:35:51蛋疼蛋
日期:2012-11-19 10:28:53鲜花蛋
日期:2012-12-05 14:37:57紫蛋头
日期:2012-12-11 17:27:27紫蛋头
日期:2012-12-24 23:00:26兰博基尼
日期:2013-11-21 09:19:58马上有车
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14
55#
发表于 2011-3-16 09:32 | 只看该作者
"第一届“盛拓传媒杯”SQL数据库编程大赛宗旨并非考核脑筋急转弯或高深的算法考核各位puber的算法能力,而且主要以PL/SQL的技巧和技能,以及对SQL新功能的掌握能力,性能优化能力,并附带考核对数据库体系结构的综合理解。题目均以oracle自带的HR或Scott示例数据库为平台。"


为什么这么多人都考虑到可以直接写 =2 ,而不用 <=2, 可用看出大家都是基于第一期的要求 "性能优化能力", 把<=2改为 =2, 性能可以提高10倍,为什么不能加? 这明显是应当加分,而不是减分!

还有很多人因为没加注释而减分,我认为,好的程序是不需要注释的,如果一个算法(高深的算法),要加一大堆注释,人家才能理解,就不是一个好的解法.

有些程序,不需要注释,人家就看懂了
有些程序加了一大堆注释,人家还看不懂, ,两个性能一样,你选哪个?


衡量一个结果最重要的一个标准是什么? 我认为是结果的正确性,如果输出的结果没满足要求,或者甚至是错的,再高效有什么用? "算法高效,可惜最后粗心,导致结果全错了,最终结果由于少写一个数字,很遗憾显示的全错了",这样的结果怎么能比大多数答题正确的结果分还高,怎么解释?


呵呵,一口怨气,不吐不快.

使用道具 举报

回复
论坛徽章:
131
2006年度最佳技术回答
日期:2007-01-24 12:58:48福特
日期:2013-10-24 13:57:422014年新春福章
日期:2014-02-18 16:41:11马上有车
日期:2014-02-18 16:41:11马上有车
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上加薪
日期:2014-02-19 11:55:142013年新春福章
日期:2013-02-25 14:51:24
56#
发表于 2011-3-16 09:36 | 只看该作者
另外,最后的地方用分析函数看似很炫,但是,单纯从代码量以及性能来看,其实,还是不如直接用
y=(select max(y) from t3)


old code:  381 word,     1410  char, run time 5.6 sec
new code1 :  269  word ,  1051 char, tun time 3.7 sec
new code2:  258 word ,  1011 char, tun time 3.4 sec

with
t1 as(select rownum-1 c from dual connect by rownum < 3),
t2 as
(select a.c a, b.c b, c.c, d.c d, e.c e ,a.c+b.c +c.c+d.c + e.c n,a.c||b.c||c.c||d.c||e.c s
    from t1 a, t1 b, t1 c, t1 d, t1 e
   where a.c + b.c + c.c + d.c + e.c < 3) ,
t3 as
(select   a.s || b.s || e.s ||d.s || e.s  x,
        (a.n + b.n + c.n + d.n +  e.n ) y
    from t2 a, t2 b, t2 c, t2 d, t2 e
   where a.a + b.a + c.a + d.a + e.a < 3
     and a.b + b.b + c.b + d.b + e.b < 3
     and a.c + b.c + c.c + d.c + e.c < 3
     and a.d + b.d + c.d + d.d + e.d < 3
     and a.e + b.e + c.e + d.e + e.e < 3
     and a.c + b.b + c.a < 3
     and a.d + b.c + c.b + d.a < 3
     and a.e + b.d + c.c + d.b + e.a < 3
     and b.e + c.d + d.c + e.b < 3
     and c.e + d.d + e.c < 3
     and a.c + b.d + c.e < 3
     and a.b + b.c + c.d + d.e < 3
     and a.a + b.b + c.c + d.d + e.e < 3
     and b.a + c.b + d.c + e.d < 3
     and c.a + d.b + e.c < 3)
select rank() over(order by x desc)rn ,x from t3 where y =(select max(y)from t3)

使用道具 举报

回复
论坛徽章:
0
57#
发表于 2011-3-16 09:36 | 只看该作者
呵呵, 我也可以我算出来92种答案直接select 出来, 我也证明了,只有92种答案,在纸上,哈哈,和用=2是半斤八两

使用道具 举报

回复
论坛徽章:
0
58#
发表于 2011-3-16 09:38 | 只看该作者
要不然你找个第93种出来。。。嘿嘿

使用道具 举报

回复
论坛徽章:
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
59#
发表于 2011-3-16 09:40 | 只看该作者
原帖由 rollingpig 于 2011-3-16 09:36 发表
另外,最后的地方用分析函数看似很炫,但是,单纯从代码量以及性能来看,其实,还是不如直接用
y=(select max(y) from t3)


old code:  381 word,     1410  char, run time 5.6 sec
new code1 :  269  word ,  1051 char, tun time 3.7 sec
new code2:  258 word ,  1011 char, tun time 3.4 sec


我这里11.2.0.1

new code1 : 00: 00: 02.04

new code2 : 00: 00: 02.20

使用道具 举报

回复
论坛徽章:
52
SQL数据库编程大师
日期:2011-04-13 12:09:01奥运会纪念徽章:水球
日期:2012-10-08 09:35:51蛋疼蛋
日期:2012-11-19 10:28:53鲜花蛋
日期:2012-12-05 14:37:57紫蛋头
日期:2012-12-11 17:27:27紫蛋头
日期:2012-12-24 23:00:26兰博基尼
日期:2013-11-21 09:19:58马上有车
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14
60#
发表于 2011-3-16 09:42 | 只看该作者
另外,贴一下我的答案,序号RT,我就认为很不错   因为

性能考虑 where l1.c+l2.c+l3.c+l4.c+l5.c =2)  , 我最后改为 =2 from <3.

算法跟lastwinner贴出的基本一致,不然也可以成为good model 了.





with
a as (SELECT 1 c FROM DUAL UNION SELECT 0 FROM DUAL)        
,b as (select l1.c c1,l2.c c2,l3.c c3,l4.c c4,l5.c c5,  l1.c+l2.c+l3.c+l4.c+l5.c cnt, l1.c||l2.c||l3.c||l4.c||l5.c  str
         from a l1,a l2,a l3,a l4,a l5
         where l1.c+l2.c+l3.c+l4.c+l5.c =2)  
,c as(
        select  str
        from
        (   
        select
        rank() over(order by l1.cnt+l2.cnt+l3.cnt+l4.cnt+l5.cnt  desc )seq  ,          
         l1.str||l2.str||l3.str||l4.str||l5.str   str            
        from b l1, b l2, b l3, b l4, b l5
        where   l1.c1+l2.c1+l3.c1+l4.c1+l5.c1 <3
            and l1.c2+l2.c2+l3.c2+l4.c2+l5.c2<3
            and l1.c3+l2.c3+l3.c3+l4.c3+l5.c3 <3
            and l1.c4+l2.c4+l3.c4+l4.c4+l5.c4 <3
            and l1.c5+l2.c5+l3.c5+l4.c5+l5.c5 <3                         
            and l3.c1+l4.c2+l5.c3<3
            and l2.c1+l3.c2+l4.c3+l5.c4<3
            and l1.c1+l2.c2+l3.c3+l4.c4+l5.c5<3
            and l1.c2+l2.c3+l3.c4+l4.c5<3
            and l1.c3+l2.c4+l3.c5<3             
            and l1.c3+l2.c2+l3.c1<3
            and l1.c4+l2.c3+l3.c2+l4.c1<3
            and l1.c5+l2.c4+l3.c3+l4.c2+l5.c1<3
            and l2.c5+l3.c4+l4.c3+l5.c2<3
            and l3.c5+l4.c4+l5.c3<3       
        ) t where seq =1
        order by str desc       
  )
select rownum, str from c

使用道具 举报

回复

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

本版积分规则 发表回复

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