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

本期题目:
问题1(80分)
在一个6*6的棋盘中,放置12个球,每行、每列、每个正负45度的斜线上最多放置2个球,请用一个SQL语句(不可以用PL/SQL或T-SQL匿名块、过程或函数,也不可以用Java等外部语言)求出不“重复”的摆法的个数(剔除上下对称、左右对称、中心对称,沿中心点旋转等各种变形)
如下是4*4矩阵的几种变形
原始排列
ABCD
EFGH
IJKL
MNOP

左右翻转
DCBA
HGFE
LKJI
PONM

上下翻转
MNOP
IJKL
EFGH
ABCD

中心旋转180度
PONM
LKJI
HGFE
DCBA

沿左上右下对角线翻转
AEIM
BFJN
CGKO
DHLP

沿左下右上对角线翻转
PLHD
OKGC
NJFB
MIEA

中心顺时针旋转90度
MIEA
NJFB
OKGC
PLHD

中心逆时针旋转90度
DHLP
CGKO
BFJN
AEIM

————————————————————————————————————————————————————————

问题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 AllCnt
2 5 92 --上期的结果总数是92,您答对了吗?

SQL2:
M N NoReptCnt
2 5 xx


一条SQL的输出格式:

[Copy to clipboard] [ - ]
CODE:
M N AllCnt NoReptCnt
2 5 92 xx


Oracle变量定义如下(以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 on
exec :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递归语法的新特性)。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值