日期:2014-05-18  浏览次数:20650 次

Play with SQL (Mission 2)
这是一些有趣的小问题。我们可以尝试用T-SQL来解答。
Just for fun!

大家回复时,别忘了贴出自己的代码,可以共同分享一下SQL的技巧和思维的火花。


以下两个题目中,每个字母代表一个0-9的数字,并且不同字母代表的数字是不同的。

1.
  a b c d e
  * f
_____________
= g g g g g g
问:a b c d e f g各是什么数字(不重复)
(该问题可用一个SQL查询给出答案)

2.
abcdef * 2 = cdefab
cdefab * 2 = efabcd
问:a b c d e f各是什么数字(不重复)
(该问题似乎用SQL解还不如用纸笔解方便,不太符合Play with SQL的本意。但abcdef是一个很神奇的数字,可以Google计算结果“abcdef”查看关于这个神奇数字的故事。)


------解决方案--------------------
反正就7个数,穷举法搞定,就不费脑子了。
SQL code
DECLARE @NUMS TABLE(NUM INT)
INSERT INTO @NUMS 
SELECT NUMBER FROM MASTER..SPT_VALUES WHERE TYPE='P' AND NUMBER BETWEEN 0 AND 9

SELECT N1.NUM,N2.NUM,N3.NUM,N4.NUM,N5.NUM,N6.NUM,N7.NUM
FROM @NUMS N1
INNER JOIN @NUMS N2 ON N1.NUM<>N2.NUM
INNER JOIN @NUMS N3 ON N1.NUM<>N3.NUM AND N2.NUM<>N3.NUM
INNER JOIN @NUMS N4 ON N1.NUM<>N4.NUM AND N2.NUM<>N4.NUM AND N3.NUM<>N4.NUM
INNER JOIN @NUMS N5 ON N1.NUM<>N5.NUM AND N2.NUM<>N5.NUM AND N3.NUM<>N5.NUM AND N4.NUM<>N5.NUM
INNER JOIN @NUMS N6 ON N1.NUM<>N6.NUM AND N2.NUM<>N6.NUM AND N3.NUM<>N6.NUM AND N4.NUM<>N6.NUM AND N5.NUM<>N6.NUM
INNER JOIN @NUMS N7 ON N1.NUM<>N7.NUM AND N2.NUM<>N7.NUM AND N3.NUM<>N7.NUM AND N4.NUM<>N7.NUM AND N5.NUM<>N7.NUM AND N6.NUM<>N7.NUM
WHERE N5.NUM*N6.NUM+N4.NUM*10*N6.NUM+N3.NUM*100*N6.NUM+N2.NUM*1000*N6.NUM+N1.NUM*10000*N6.NUM
=N7.NUM+N7.NUM*10+N7.NUM*100+N7.NUM*1000+N7.NUM*10000+N7.NUM*100000
/*
9    5    2    3    8    7    6    
SELECT 95238*7
--SELECT 95238*7
*/

------解决方案--------------------
第二题
SQL code
DECLARE @NUMS TABLE(NUM INT)
INSERT INTO @NUMS 
SELECT NUMBER FROM MASTER..SPT_VALUES WHERE TYPE='P' AND NUMBER BETWEEN 0 AND 9

SELECT N1.NUM,N2.NUM,N3.NUM,N4.NUM,N5.NUM,N6.NUM
FROM @NUMS N1
INNER JOIN @NUMS N2 ON N1.NUM<>N2.NUM
INNER JOIN @NUMS N3 ON N1.NUM<>N3.NUM AND N2.NUM<>N3.NUM
INNER JOIN @NUMS N4 ON N1.NUM<>N4.NUM AND N2.NUM<>N4.NUM AND N3.NUM<>N4.NUM
INNER JOIN @NUMS N5 ON N1.NUM<>N5.NUM AND N2.NUM<>N5.NUM AND N3.NUM<>N5.NUM AND N4.NUM<>N5.NUM
INNER JOIN @NUMS N6 ON N1.NUM<>N6.NUM AND N2.NUM<>N6.NUM AND N3.NUM<>N6.NUM AND N4.NUM<>N6.NUM AND N5.NUM<>N6.NUM

WHERE N1.NUM*100000*2+N2.NUM*10000*2+N3.NUM*1000*2+N4.NUM*100*2+N5.NUM*10*2+N6.NUM*1*2
=N3.NUM*100000+N4.NUM*10000+N5.NUM*1000+N6.NUM*100+N1.NUM*10+N2.NUM*1
AND N3.NUM*100000*2+N4.NUM*10000*2+N5.NUM*1000*2+N6.NUM*100*2+N1.NUM*10*2+N2.NUM*1*2
=N5.NUM*100000+N6.NUM*10000+N1.NUM*1000+N2.NUM*100+N3.NUM*10+N4.NUM*1
/*
1    4    2    8    5    7
*/

------解决方案--------------------
SQL code

2.

declare @tb table (num int)
insert into @tb
select number from master..spt_values 
where TYPE='p' and number between 0 and 9
select distinct a.num a,b.num b,c.num c,d.num d,e.num e,f.num f
from @tb a ,@tb b,@tb c,@tb d ,@tb e,@tb f
where (a.num*100000+b.num*10000+c.num*1000+d.num*100+e.num*10+f.num)*2=
       c.num*100000+d.num*10000+e.num*1000+f.num*100+a.num*10+b.num  and 
       (c.num*100000+d.num*10000+e.num*1000+f.num*100+a.num*10+b.num)*2=
       e.num*100000+f.num*10000+a.num*1000+b.num*100+c.num*10+d.num  
       and a.num<>b.num and a.num<>c.num and a.num<>d.num and a.num<>e.num
       and b.num<>c.num and b.num<>d.num and b.num<>e.num and c.num<>d.num 
       and c.num<>e.num and  d.num<>e.num 
a    b    c    d    e    f
1    4    2    8    5    7

------解决方案--------------------
两个都改一下
SQL code

1.
declare @tb table (num int)
insert into @tb
select number from master..spt_va