SQL数据库编程大赛(第二期)

    技术2022-05-20  56

    本期题目:问题1(80分):    在一个6*6的棋盘中,放置12个球,每行、每列、每个正负45度的斜线上最多放置2个球,请用一个SQL语句(不可以用PL/SQL或T-SQL匿名块、过程或函数,也不可以用Java等外部语言)求出不“重复”的摆法的个数(剔除上下对称、左右对称、中心对称,沿中心点旋转等各种变形)如下是4*4矩阵的几种变形原始排列ABCDEFGHIJKLMNOP左右翻转DCBAHGFELKJIPONM上下翻转MNOPIJKLEFGHABCD中心旋转180度PONMLKJIHGFEDCBA沿左上右下对角线翻转AEIMBFJNCGKODHLP沿左下右上对角线翻转PLHDOKGCNJFBMIEA中心顺时针旋转90度MIEANJFBOKGCPLHD中心逆时针旋转90度DHLPCGKOBFJNAEIM————————————————————————————————————————————————————————问题2(40分):    在一个N*N的棋盘中,每行放置M个球,每列、每个45度的斜线上最多放置M个球,其中5<=N<=6,1<=M<=(N-1),现要求出每个M,N组合中最多摆放球的不同的摆法的个数(包括重复和不重复的,分别输出)。请用最多两条SQL语句得到以下结果:(以M=2, N=5为例)两条SQL的输出格式:[Copy to clipboard] [ - ]CODE:SQL1:M      N         AllCnt2      5          92               --上期的结果总数是92,您答对了吗?SQL2:M      N         NoReptCnt2      5          xx一条SQL的输出格式:[Copy to clipboard] [ - ]CODE:M      N         AllCnt           NoReptCnt2      5          92                xxOracle变量定义如下(以M=2, N=5为例):var m number;exec :m:=2;var n number;exec :n:=5;MS SQL Server变量定义如下(以M=2, N=5为例):declare @n int,@m int;set @n=5;set @m=2;扩展题的执行方式最好如下:set timing onexec :m:=2;exec :n:=5;@/sql2-leitaisai.sql————————————————————————————————————————————————书写格式:仅包含一个查询语句(可以有子查询)和必要的注释(用/*   */括起),不得包含创建表、视图、索引和插入、更新、删除等语句。输出格式:问题1输出一个整数问题2输出见题目说明数据库平台:适用Oracle、MS SQL Server,版本(Oracle推荐10gr2(包含)以上版本、MS SQL Sever推荐2008版本)

    原文见:http://www.itpub.net/thread-1403356-1-1.html

    参赛者答案:http://www.itpub.net/thread-1411980-1-1.html

    我提交的答案:

    第一题:

    --ITPUB“盛拓传媒杯”SQL数据库编程大赛第二期,第1题 --Oracle9.2.0.1运行通过 with t as (select 0 c1 from dual union all select 1 from dual), --行排列组合 a as (select /*+ordered */ a1.c1 c1,a2.c1 c2,a3.c1 c3,a4.c1 c4,a5.c1 c5,a6.c1 c6 from t a1, t a2, t a3, t a4, t a5,t a6 where a1.c1+a2.c1+a3.c1+a4.c1+a5.c1+a6.c1=2) select count(*) AllCnt,--所有的组合 count(distinct greatest(s1,s2,s3,s4,s5,s6,s7,s8)) NoReptCnt --去重后的组合 from ( select /*+ordered */ r1.c1||r1.c2||r1.c3||r1.c4||r1.c5||r1.c6 ||r2.c1||r2.c2||r2.c3||r2.c4||r2.c5||r2.c6 ||r3.c1||r3.c2||r3.c3||r3.c4||r3.c5||r3.c6 ||r4.c1||r4.c2||r4.c3||r4.c4||r4.c5||r4.c6 ||r5.c1||r5.c2||r5.c3||r5.c4||r5.c5||r5.c6 ||r6.c1||r6.c2||r6.c3||r6.c4||r6.c5||r6.c6 s1, --以下为阵列的旋转变形 r6.c1||r6.c2||r6.c3||r6.c4||r6.c5||r6.c6 ||r5.c1||r5.c2||r5.c3||r5.c4||r5.c5||r5.c6 ||r4.c1||r4.c2||r4.c3||r4.c4||r4.c5||r4.c6 ||r3.c1||r3.c2||r3.c3||r3.c4||r3.c5||r3.c6 ||r2.c1||r2.c2||r2.c3||r2.c4||r2.c5||r2.c6 ||r1.c1||r1.c2||r1.c3||r1.c4||r1.c5||r1.c6 s2, r1.c6||r1.c5||r1.c4||r1.c3||r1.c2||r1.c1 ||r2.c6||r2.c5||r2.c4||r2.c3||r2.c2||r2.c1 ||r3.c6||r3.c5||r3.c4||r3.c3||r3.c2||r3.c1 ||r4.c6||r4.c5||r4.c4||r4.c3||r4.c2||r4.c1 ||r5.c6||r5.c5||r5.c4||r5.c3||r5.c2||r5.c1 ||r6.c6||r6.c5||r6.c4||r6.c3||r6.c2||r6.c1 s3, r6.c6||r6.c5||r6.c4||r6.c3||r6.c2||r6.c1 ||r5.c6||r5.c5||r5.c4||r5.c3||r5.c2||r5.c1 ||r4.c6||r4.c5||r4.c4||r4.c3||r4.c2||r4.c1 ||r3.c6||r3.c5||r3.c4||r3.c3||r3.c2||r3.c1 ||r2.c6||r2.c5||r2.c4||r2.c3||r2.c2||r2.c1 ||r1.c6||r1.c5||r1.c4||r1.c3||r1.c2||r1.c1 s4, r1.c1||r2.c1||r3.c1||r4.c1||r5.c1||r6.c1 ||r1.c2||r2.c2||r3.c2||r4.c2||r5.c2||r6.c2 ||r1.c3||r2.c3||r3.c3||r4.c3||r5.c3||r6.c3 ||r1.c4||r2.c4||r3.c4||r4.c4||r5.c4||r6.c4 ||r1.c5||r2.c5||r3.c5||r4.c5||r5.c5||r6.c5 ||r1.c6||r2.c6||r3.c6||r4.c6||r5.c6||r6.c6 s5, r1.c6||r2.c6||r3.c6||r4.c6||r5.c6||r6.c6 ||r1.c5||r2.c5||r3.c5||r4.c5||r5.c5||r6.c5 ||r1.c4||r2.c4||r3.c4||r4.c4||r5.c4||r6.c4 ||r1.c3||r2.c3||r3.c3||r4.c3||r5.c3||r6.c3 ||r1.c2||r2.c2||r3.c2||r4.c2||r5.c2||r6.c2 ||r1.c1||r2.c1||r3.c1||r4.c1||r5.c1||r6.c1 s6, r6.c1||r5.c1||r4.c1||r3.c1||r2.c1||r1.c1 ||r6.c2||r5.c2||r4.c2||r3.c2||r2.c2||r1.c2 ||r6.c3||r5.c3||r4.c3||r3.c3||r2.c3||r1.c3 ||r6.c4||r5.c4||r4.c4||r3.c4||r2.c4||r1.c4 ||r6.c5||r5.c5||r4.c5||r3.c5||r2.c5||r1.c5 ||r6.c6||r5.c6||r4.c6||r3.c6||r2.c6||r1.c6 s7, r6.c6||r5.c6||r4.c6||r3.c6||r2.c6||r1.c6 ||r6.c5||r5.c5||r4.c5||r3.c5||r2.c5||r1.c5 ||r6.c4||r5.c4||r4.c4||r3.c4||r2.c4||r1.c4 ||r6.c3||r5.c3||r4.c3||r3.c3||r2.c3||r1.c3 ||r6.c2||r5.c2||r4.c2||r3.c2||r2.c2||r1.c2 ||r6.c1||r5.c1||r4.c1||r3.c1||r2.c1||r1.c1 s8 from a r1, a r2, a r3, a r4, a r5,a r6 --笛卡尔连接生成所有行符合要求的排列组合 where --列判断 r1.c1+r2.c1+r3.c1+r4.c1+r5.c1+r6.c1=2 and r1.c2+r2.c2+r3.c2+r4.c2+r5.c2+r6.c2=2 and r1.c3+r2.c3+r3.c3+r4.c3+r5.c3+r6.c3=2 and r1.c4+r2.c4+r3.c4+r4.c4+r5.c4+r6.c4=2 and r1.c5+r2.c5+r3.c5+r4.c5+r5.c5+r6.c5=2 and r1.c6+r2.c6+r3.c6+r4.c6+r5.c6+r6.c6=2 --斜边1判断 and r1.c2+r2.c1<=2 and r1.c3+r2.c2+r3.c1<=2 and r1.c4+r2.c3+r3.c2+r4.c1<=2 and r1.c5+r2.c4+r3.c3+r4.c2+r5.c1<=2 and r1.c6+r2.c5+r3.c4+r4.c3+r5.c2+r6.c1<=2 and r2.c6+r3.c5+r4.c4+r5.c3+r6.c2<=2 and r3.c6+r4.c5+r5.c4+r6.c3<=2 and r4.c6+r5.c5+r6.c4<=2 and r5.c6+r6.c5<=2 --斜边2判断 and r5.c1+r6.c2<=2 and r4.c1+r5.c2+r6.c3<=2 and r3.c1+r4.c2+r5.c3+r6.c4<=2 and r2.c1+r3.c2+r4.c3+r5.c4+r6.c5<=2 and r1.c1+r2.c2+r3.c3+r4.c4+r5.c5+r6.c6<=2 and r1.c2+r2.c3+r3.c4+r4.c5+r5.c6<=2 and r1.c3+r2.c4+r3.c5+r4.c6<=2 and r1.c4+r2.c5+r3.c6<=2 and r1.c5+r2.c6<=2 --以下为优化 and r1.c1+r2.c1+r3.c1<=2 and r1.c2+r2.c2+r3.c2<=2 and r1.c3+r2.c3+r3.c3<=2 and r1.c4+r2.c4+r3.c4<=2 and r1.c5+r2.c5+r3.c5<=2 and r1.c6+r2.c6+r3.c6<=2 and r1.c1+r2.c1+r3.c1+r4.c1<=2 and r1.c2+r2.c2+r3.c2+r4.c2<=2 and r1.c3+r2.c3+r3.c3+r4.c3<=2 and r1.c4+r2.c4+r3.c4+r4.c4<=2 and r1.c5+r2.c5+r3.c5+r4.c5<=2 and r1.c6+r2.c6+r3.c6+r4.c6<=2 and r1.c1+r2.c1+r3.c1+r4.c1+r5.c1<=2 and r1.c2+r2.c2+r3.c2+r4.c2+r5.c2<=2 and r1.c3+r2.c3+r3.c3+r4.c3+r5.c3<=2 and r1.c4+r2.c4+r3.c4+r4.c4+r5.c4<=2 and r1.c5+r2.c5+r3.c5+r4.c5+r5.c5<=2 and r1.c6+r2.c6+r3.c6+r4.c6+r5.c6<=2 and r1.c4+r2.c3+r3.c2<=2 and r1.c5+r2.c4+r3.c3<=2 and r1.c6+r2.c5+r3.c4<=2 and r1.c1+r2.c2+r3.c3<=2 and r1.c2+r2.c3+r3.c4<=2 and r1.c3+r2.c4+r3.c5<=2); --结果为155

    第二题:

    --ITPUB“盛拓传媒杯”SQL数据库编程大赛第二期,第2题 --Oracle9.2.0.1运行通过 --5个棋子 select * from (with t as (select 0 c1 from dual union all select 1 from dual), --行排列组合 a as (select /*+ordered */ a1.c1 c1,a2.c1 c2,a3.c1 c3,a4.c1 c4,a5.c1 c5 from t a1, t a2, t a3, t a4, t a5 where a1.c1+a2.c1+a3.c1+a4.c1+a5.c1=:m and 5=:n) select :m,:n,count(*) AllCnt,--所有的组合 count(distinct greatest(s1,s2,s3,s4,s5,s6,s7,s8)) NoReptCnt --去重后的组合 from ( select /*+ordered */ r1.c1||r1.c2||r1.c3||r1.c4||r1.c5 ||r2.c1||r2.c2||r2.c3||r2.c4||r2.c5 ||r3.c1||r3.c2||r3.c3||r3.c4||r3.c5 ||r4.c1||r4.c2||r4.c3||r4.c4||r4.c5 ||r5.c1||r5.c2||r5.c3||r5.c4||r5.c5 s1, --以下为阵列的旋转变形 r5.c1||r5.c2||r5.c3||r5.c4||r5.c5 ||r4.c1||r4.c2||r4.c3||r4.c4||r4.c5 ||r3.c1||r3.c2||r3.c3||r3.c4||r3.c5 ||r2.c1||r2.c2||r2.c3||r2.c4||r2.c5 ||r1.c1||r1.c2||r1.c3||r1.c4||r1.c5 s2, r1.c5||r1.c4||r1.c3||r1.c2||r1.c1 ||r2.c5||r2.c4||r2.c3||r2.c2||r2.c1 ||r3.c5||r3.c4||r3.c3||r3.c2||r3.c1 ||r4.c5||r4.c4||r4.c3||r4.c2||r4.c1 ||r5.c5||r5.c4||r5.c3||r5.c2||r5.c1 s3, r5.c5||r5.c4||r5.c3||r5.c2||r5.c1 ||r4.c5||r4.c4||r4.c3||r4.c2||r4.c1 ||r3.c5||r3.c4||r3.c3||r3.c2||r3.c1 ||r2.c5||r2.c4||r2.c3||r2.c2||r2.c1 ||r1.c5||r1.c4||r1.c3||r1.c2||r1.c1 s4, r1.c1||r2.c1||r3.c1||r4.c1||r5.c1 ||r1.c2||r2.c2||r3.c2||r4.c2||r5.c2 ||r1.c3||r2.c3||r3.c3||r4.c3||r5.c3 ||r1.c4||r2.c4||r3.c4||r4.c4||r5.c4 ||r1.c5||r2.c5||r3.c5||r4.c5||r5.c5 s5, r1.c5||r2.c5||r3.c5||r4.c5||r5.c5 ||r1.c4||r2.c4||r3.c4||r4.c4||r5.c4 ||r1.c3||r2.c3||r3.c3||r4.c3||r5.c3 ||r1.c2||r2.c2||r3.c2||r4.c2||r5.c2 ||r1.c1||r2.c1||r3.c1||r4.c1||r5.c1 s6, r5.c1||r4.c1||r3.c1||r2.c1||r1.c1 ||r5.c2||r4.c2||r3.c2||r2.c2||r1.c2 ||r5.c3||r4.c3||r3.c3||r2.c3||r1.c3 ||r5.c4||r4.c4||r3.c4||r2.c4||r1.c4 ||r5.c5||r4.c5||r3.c5||r2.c5||r1.c5 s7, r5.c5||r4.c5||r3.c5||r2.c5||r1.c5 ||r5.c4||r4.c4||r3.c4||r2.c4||r1.c4 ||r5.c3||r4.c3||r3.c3||r2.c3||r1.c3 ||r5.c2||r4.c2||r3.c2||r2.c2||r1.c2 ||r5.c1||r4.c1||r3.c1||r2.c1||r1.c1 s8 from a r1, a r2, a r3, a r4, a r5 --笛卡尔连接生成所有行符合要求的排列组合 where --列判断 r1.c1+r2.c1+r3.c1+r4.c1+r5.c1<=:m and r1.c2+r2.c2+r3.c2+r4.c2+r5.c2<=:m and r1.c3+r2.c3+r3.c3+r4.c3+r5.c3<=:m and r1.c4+r2.c4+r3.c4+r4.c4+r5.c4<=:m and r1.c5+r2.c5+r3.c5+r4.c5+r5.c5<=:m --斜边1判断 and r1.c2+r2.c1<=:m and r1.c3+r2.c2+r3.c1<=:m and r1.c4+r2.c3+r3.c2+r4.c1<=:m and r1.c5+r2.c4+r3.c3+r4.c2+r5.c1<=:m and r2.c5+r3.c4+r4.c3+r5.c2<=:m and r3.c5+r4.c4+r5.c3<=:m and r4.c5+r5.c4<=:m --斜边2判断 and r4.c1+r5.c2<=:m and r3.c1+r4.c2+r5.c3<=:m and r2.c1+r3.c2+r4.c3+r5.c4<=:m and r1.c1+r2.c2+r3.c3+r4.c4+r5.c5<=:m and r1.c2+r2.c3+r3.c4+r4.c5<=:m and r1.c3+r2.c4+r3.c5<=:m and r1.c4+r2.c5<=:m --以下为优化 and r1.c1+r2.c1+r3.c1<=:m and r1.c2+r2.c2+r3.c2<=:m and r1.c3+r2.c3+r3.c3<=:m and r1.c4+r2.c4+r3.c4<=:m and r1.c5+r2.c5+r3.c5<=:m )) where 5=:n union all --6个棋子,算法同5个棋子 select * from ( with t as (select 0 c1 from dual union all select 1 from dual), --行排列组合 a as (select /*+ordered */ a1.c1 c1,a2.c1 c2,a3.c1 c3,a4.c1 c4,a5.c1 c5,a6.c1 c6 from t a1, t a2, t a3, t a4, t a5,t a6 where a1.c1+a2.c1+a3.c1+a4.c1+a5.c1+a6.c1=:m and 6=:n) select :m,:n,count(*) AllCnt,--所有的组合 count(distinct greatest(s1,s2,s3,s4,s5,s6,s7,s8)) NoReptCnt --去重后的组合 from ( select /*+ordered */ r1.c1||r1.c2||r1.c3||r1.c4||r1.c5||r1.c6 ||r2.c1||r2.c2||r2.c3||r2.c4||r2.c5||r2.c6 ||r3.c1||r3.c2||r3.c3||r3.c4||r3.c5||r3.c6 ||r4.c1||r4.c2||r4.c3||r4.c4||r4.c5||r4.c6 ||r5.c1||r5.c2||r5.c3||r5.c4||r5.c5||r5.c6 ||r6.c1||r6.c2||r6.c3||r6.c4||r6.c5||r6.c6 s1, --以下为阵列的旋转变形 r6.c1||r6.c2||r6.c3||r6.c4||r6.c5||r6.c6 ||r5.c1||r5.c2||r5.c3||r5.c4||r5.c5||r5.c6 ||r4.c1||r4.c2||r4.c3||r4.c4||r4.c5||r4.c6 ||r3.c1||r3.c2||r3.c3||r3.c4||r3.c5||r3.c6 ||r2.c1||r2.c2||r2.c3||r2.c4||r2.c5||r2.c6 ||r1.c1||r1.c2||r1.c3||r1.c4||r1.c5||r1.c6 s2, r1.c6||r1.c5||r1.c4||r1.c3||r1.c2||r1.c1 ||r2.c6||r2.c5||r2.c4||r2.c3||r2.c2||r2.c1 ||r3.c6||r3.c5||r3.c4||r3.c3||r3.c2||r3.c1 ||r4.c6||r4.c5||r4.c4||r4.c3||r4.c2||r4.c1 ||r5.c6||r5.c5||r5.c4||r5.c3||r5.c2||r5.c1 ||r6.c6||r6.c5||r6.c4||r6.c3||r6.c2||r6.c1 s3, r6.c6||r6.c5||r6.c4||r6.c3||r6.c2||r6.c1 ||r5.c6||r5.c5||r5.c4||r5.c3||r5.c2||r5.c1 ||r4.c6||r4.c5||r4.c4||r4.c3||r4.c2||r4.c1 ||r3.c6||r3.c5||r3.c4||r3.c3||r3.c2||r3.c1 ||r2.c6||r2.c5||r2.c4||r2.c3||r2.c2||r2.c1 ||r1.c6||r1.c5||r1.c4||r1.c3||r1.c2||r1.c1 s4, r1.c1||r2.c1||r3.c1||r4.c1||r5.c1||r6.c1 ||r1.c2||r2.c2||r3.c2||r4.c2||r5.c2||r6.c2 ||r1.c3||r2.c3||r3.c3||r4.c3||r5.c3||r6.c3 ||r1.c4||r2.c4||r3.c4||r4.c4||r5.c4||r6.c4 ||r1.c5||r2.c5||r3.c5||r4.c5||r5.c5||r6.c5 ||r1.c6||r2.c6||r3.c6||r4.c6||r5.c6||r6.c6 s5, r1.c6||r2.c6||r3.c6||r4.c6||r5.c6||r6.c6 ||r1.c5||r2.c5||r3.c5||r4.c5||r5.c5||r6.c5 ||r1.c4||r2.c4||r3.c4||r4.c4||r5.c4||r6.c4 ||r1.c3||r2.c3||r3.c3||r4.c3||r5.c3||r6.c3 ||r1.c2||r2.c2||r3.c2||r4.c2||r5.c2||r6.c2 ||r1.c1||r2.c1||r3.c1||r4.c1||r5.c1||r6.c1 s6, r6.c1||r5.c1||r4.c1||r3.c1||r2.c1||r1.c1 ||r6.c2||r5.c2||r4.c2||r3.c2||r2.c2||r1.c2 ||r6.c3||r5.c3||r4.c3||r3.c3||r2.c3||r1.c3 ||r6.c4||r5.c4||r4.c4||r3.c4||r2.c4||r1.c4 ||r6.c5||r5.c5||r4.c5||r3.c5||r2.c5||r1.c5 ||r6.c6||r5.c6||r4.c6||r3.c6||r2.c6||r1.c6 s7, r6.c6||r5.c6||r4.c6||r3.c6||r2.c6||r1.c6 ||r6.c5||r5.c5||r4.c5||r3.c5||r2.c5||r1.c5 ||r6.c4||r5.c4||r4.c4||r3.c4||r2.c4||r1.c4 ||r6.c3||r5.c3||r4.c3||r3.c3||r2.c3||r1.c3 ||r6.c2||r5.c2||r4.c2||r3.c2||r2.c2||r1.c2 ||r6.c1||r5.c1||r4.c1||r3.c1||r2.c1||r1.c1 s8 from a r1, a r2, a r3, a r4, a r5,a r6 --笛卡尔连接生成所有行符合要求的排列组合 where r1.c1+r2.c1+r3.c1+r4.c1+r5.c1+r6.c1=:m and r1.c2+r2.c2+r3.c2+r4.c2+r5.c2+r6.c2=:m and r1.c3+r2.c3+r3.c3+r4.c3+r5.c3+r6.c3=:m and r1.c4+r2.c4+r3.c4+r4.c4+r5.c4+r6.c4=:m and r1.c5+r2.c5+r3.c5+r4.c5+r5.c5+r6.c5=:m and r1.c6+r2.c6+r3.c6+r4.c6+r5.c6+r6.c6=:m --斜边1判断 and r1.c2+r2.c1<=:m and r1.c3+r2.c2+r3.c1<=:m and r1.c4+r2.c3+r3.c2+r4.c1<=:m and r1.c5+r2.c4+r3.c3+r4.c2+r5.c1<=:m and r1.c6+r2.c5+r3.c4+r4.c3+r5.c2+r6.c1<=:m and r2.c6+r3.c5+r4.c4+r5.c3+r6.c2<=:m and r3.c6+r4.c5+r5.c4+r6.c3<=:m and r4.c6+r5.c5+r6.c4<=:m and r5.c6+r6.c5<=:m --斜边2判断 and r5.c1+r6.c2<=:m and r4.c1+r5.c2+r6.c3<=:m and r3.c1+r4.c2+r5.c3+r6.c4<=:m and r2.c1+r3.c2+r4.c3+r5.c4+r6.c5<=:m and r1.c1+r2.c2+r3.c3+r4.c4+r5.c5+r6.c6<=:m and r1.c2+r2.c3+r3.c4+r4.c5+r5.c6<=:m and r1.c3+r2.c4+r3.c5+r4.c6<=:m and r1.c4+r2.c5+r3.c6<=:m and r1.c5+r2.c6<=:m --以下为优化 and r1.c1+r2.c1+r3.c1<=:m and r1.c2+r2.c2+r3.c2<=:m and r1.c3+r2.c3+r3.c3<=:m and r1.c4+r2.c4+r3.c4<=:m and r1.c5+r2.c5+r3.c5<=:m and r1.c6+r2.c6+r3.c6<=:m and r1.c1+r2.c1+r3.c1+r4.c1<=:m and r1.c2+r2.c2+r3.c2+r4.c2<=:m and r1.c3+r2.c3+r3.c3+r4.c3<=:m and r1.c4+r2.c4+r3.c4+r4.c4<=:m and r1.c5+r2.c5+r3.c5+r4.c5<=:m and r1.c6+r2.c6+r3.c6+r4.c6<=:m and r1.c1+r2.c1+r3.c1+r4.c1+r5.c1<=:m and r1.c2+r2.c2+r3.c2+r4.c2+r5.c2<=:m and r1.c3+r2.c3+r3.c3+r4.c3+r5.c3<=:m and r1.c4+r2.c4+r3.c4+r4.c4+r5.c4<=:m and r1.c5+r2.c5+r3.c5+r4.c5+r5.c5<=:m and r1.c6+r2.c6+r3.c6+r4.c6+r5.c6<=:m and r1.c4+r2.c3+r3.c2<=:m and r1.c5+r2.c4+r3.c3<=:m and r1.c6+r2.c5+r3.c4<=:m and r1.c1+r2.c2+r3.c3<=:m and r1.c2+r2.c3+r3.c4<=:m and r1.c3+r2.c4+r3.c5<=:m )) where 6=:n; --M=3,N=6时执行最长,一般机器大约需要10秒左右,ALLCNT=14412,NOREPTCNT=1811

    解题思路:

    两道题的思路相同,见里面的注释

    1、得到行符合要求的排列组合

    2、用笛卡尔连接生成所有的排列组合

    3、去除列与斜边不满足要求的记录

    4、将结果做不同位置的旋转,得到字符串

    5、取出所有旋转字符串并去除重复的,最后得到结果

    6、第二题用UNION ALL的方法组合实现行数为5*5和6*6的输入。

    评委点评:通过把代码写两遍的方法来实现扩展性显得有些笨拙。第一题的优化代码在M变大的情况下就成了累赘。采用5×5和6×6叠加的办法实现,可扩展性差,整体效率也很低。

    个人分析:

    1、在第一期的基础上,改进了得到行符合要求的排列组合,满足每行棋子数变化的需求。

    2、仅能满足5*5和6*6的要求,扩展性极差(水平有限,实在想不出好办法,或者是被第一期的思维给绑定了)。

    3、整体性能一般,但是要用同样的思路扩展到7*7的,估计SQL会执行不出来(临时表空间不够,时间会超长)。

    4、和第一期一样,相对其他人的代码简单,思路简单,直接了当(其他人的代码基本看不懂,太佩服评委了)。

    这道题是SQL大赛4期里面最难的,总体感觉很差,但是评委的最后评分很高,不知道原因是什么(可能其他人更差,也有可能评委搞错了),第1名(2-11)的答题不错(用了11gR2 with递归语法的新特性)。


    最新回复(0)