当前位置: 代码迷 >> Sql Server >> Play with SQL (Mission 2)解决办法
  详细解决方案

Play with SQL (Mission 2)解决办法

热度:421   发布时间:2016-04-27 17:05:03.0
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 9SELECT N1.NUM,N2.NUM,N3.NUM,N4.NUM,N5.NUM,N6.NUM,N7.NUMFROM @NUMS N1INNER JOIN @NUMS N2 ON N1.NUM<>N2.NUMINNER JOIN @NUMS N3 ON N1.NUM<>N3.NUM AND N2.NUM<>N3.NUMINNER JOIN @NUMS N4 ON N1.NUM<>N4.NUM AND N2.NUM<>N4.NUM AND N3.NUM<>N4.NUMINNER JOIN @NUMS N5 ON N1.NUM<>N5.NUM AND N2.NUM<>N5.NUM AND N3.NUM<>N5.NUM AND N4.NUM<>N5.NUMINNER 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.NUMINNER 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.NUMWHERE 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 9SELECT N1.NUM,N2.NUM,N3.NUM,N4.NUM,N5.NUM,N6.NUMFROM @NUMS N1INNER JOIN @NUMS N2 ON N1.NUM<>N2.NUMINNER JOIN @NUMS N3 ON N1.NUM<>N3.NUM AND N2.NUM<>N3.NUMINNER JOIN @NUMS N4 ON N1.NUM<>N4.NUM AND N2.NUM<>N4.NUM AND N3.NUM<>N4.NUMINNER JOIN @NUMS N5 ON N1.NUM<>N5.NUM AND N2.NUM<>N5.NUM AND N3.NUM<>N5.NUM AND N4.NUM<>N5.NUMINNER 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.NUMWHERE 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*1AND 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 @tbselect number from master..spt_values where TYPE='p' and number between 0 and 9select distinct a.num a,b.num b,c.num c,d.num d,e.num e,f.num ffrom @tb a ,@tb b,@tb c,@tb d ,@tb e,@tb fwhere (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    f1    4    2    8    5    7
------解决方案--------------------
两个都改一下
SQL code
1.declare @tb table (num int)insert into @tbselect number from master..spt_values where TYPE='p' and number between 0 and 9select distinct a.num a,b.num b,c.num c,d.num d,e.num e,f.num f,g.num gfrom @tb a ,@tb b,@tb c,@tb d ,@tb e,@tb f,@tb gwhere (a.num*10000+b.num*1000+c.num*100+d.num*10+e.num)*f.num      =( g.num*100000+g.num*10000+g.num*1000+g.num*100+g.num*10+g.num)      and a.num<>b.num and a.num<>c.num and a.num<>d.num and a.num<>e.num      and a.num<>f.num and b.num<>c.num and b.num<>d.num and b.num<>e.num      and b.num<>f.num and c.num<>d.num and c.num<>e.num and c.num<>f.num      and d.num<>e.num and d.num<>f.num and e.num<>f.num and f.num<>0 and g.num<>0      and a.num<>g.num and b.num<>g.num and c.num<>g.num and d.num<>g.num      and e.num<>g.num and f.num<>g.numa    b    c    d    e    f    g9    5    2    3    8    7    6select 95238*7 --=6666662.declare @tb table (num int)insert into @tbselect number from master..spt_values where TYPE='p' and number between 0 and 9select distinct a.num a,b.num b,c.num c,d.num d,e.num e,f.num ffrom @tb a ,@tb b,@tb c,@tb d ,@tb e,@tb fwhere (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 a.num<>f.num and b.num<>c.num and b.num<>d.num and b.num<>e.num        and b.num<>f.num and c.num<>d.num and c.num<>e.num and c.num<>f.num        and d.num<>e.num and d.num<>f.num and e.num<>f.numa    b    c    d    e    f1    4    2    8    5    7
  相关解决方案