2010-09-15 84 views
2

这里是问题TSQL集理论

你要用数字标记骰子边。每个骰子有6面。你有两个骰子。您必须标签,这样就可以显示(没有总结或产品)的数字0到31的完整输出:

00 
01 
02 
03 
04 
05 
06 
07 
08 
09 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 

我试图

(select 
     0 as dice1 union 
     select 1 union 
     select 2 union 
     select 3 union 
     select 4 union 
     select 5) 

    join 

(select 
     0 as dice2 union 
     select 1 union 
     select 2 union 
     select 3 union 
     select 4 union 
     select 5) 

我不知道如何进一步处理它。帮助表示赞赏。

+4

不知道。你肯定会需要6,7,8,9在一个或其他的骰子。但是0,1和2需要放在两个骰子上,只剩下7个数字的6个插槽!也许你必须把6倒过来才能得到9。无论如何,与SQL Server 2005无关! – 2010-09-15 15:11:04

+1

经常遇到的问题是由您有时在银行看到的由木块组成的“连续压光机”。这个问题严格限制过度,正如@Martin所暗示的那样,用6/9的模糊来解决问题。但是,无论如何,这不是一个编程问题,而是关键问题。 – dmckee 2010-09-15 17:22:23

回答

1

正如马丁在他的评论中提到的,关键要解决这个问题是使用倒挂6一9参见:solution: calendar cubes

作为一个纲领性的T-SQL的解决方案,或许是:

declare @Dice1 table (
    side int 
) 

insert into @Dice1 
    (side) 
    select 0 union all 
    select 1 union all 
    select 2 union all 
    select 3 union all 
    select 4 union all 
    select 5 

declare @Dice2 table (
    side int 
) 

insert into @Dice2 
    (side) 
    select 0 union all 
    select 1 union all 
    select 2 union all 
    select 6 union all 
    select 7 union all 
    select 8 union all 
    select 9 /* Upside down 6 */ 


select CAST(d1.side as CHAR(1)) + CAST(d2.side as CHAR(1)) as MyDate 
    from @Dice1 d1 
     cross join @Dice2 d2 
    where d1.side * 10 + d2.side <= 31 
union 
select CAST(d2.side as CHAR(1)) + CAST(d1.side as CHAR(1)) as MyDate 
    from @Dice1 d1 
     cross join @Dice2 d2 
    where d2.side * 10 + d1.side <= 31 
order by MyDate 
+0

非常优雅的解决方案。它是'0 1 2 3 4 5'和'0 1 2 7 8 6'。 – 2010-09-15 20:43:20

+0

为什么'MyDate'? :) – 2010-09-15 20:49:12

+0

@Denis:多年和几年喝微软库尔援助:我的文档,我的下载,...,MyDate。 :-) – 2010-09-15 20:55:27