怀疑创建使用TSQL一个谢尔宾斯基地毯(基于集)(Doubt in creating a Sierp

2019-10-29 18:44发布

TSQL挑战56结束了....所以我safetly问怀疑上

我试图解决这个问题,下

WITH CTE AS 
( 
    SELECT   
            id ,
                        level ,
                        1 AS row ,
                        REPLICATE('X', POWER(3, level)) AS carpet
               FROM     TC56
               UNION ALL
               SELECT   id ,
                        level ,
                        row + 1 ,
                        carpet
               FROM     CTE
               WHERE    Row < POWER(3, level)
             )
    SELECT  
        id
        ,row
        ,carpet
    FROM    CTE
    ORDER BY id,row

但产量不与指定的标准杆。我的输出是下

id row carpet
1 1 X
2 1 XXX
2 2 XXX
2 3 XXX
3 1 XXXXXXXXX
3 2 XXXXXXXXX
3 3 XXXXXXXXX
3 4 XXXXXXXXX
3 5 XXXXXXXXX
3 6 XXXXXXXXX
3 7 XXXXXXXXX
3 8 XXXXXXXXX
3 9 XXXXXXXXX

问题是,我不能够填补这个位置的需要来填充空白空间

需要帮助

Answer 1:

下面是可以扩展到能够产生更大的订单谢尔宾斯基地毯的解决方案。 目前,它可以正确地产生谢尔宾斯基地毯到3阶。

由“正确”我的意思是,它可以产生所有必要的行更大的顺序的模式,但目前附加列需要添加(那些sort N的),以提供正确的行布置。 我已经添加在需要修改的意见。

WITH TC56 (id, level) AS (
  SELECT 1, 0 UNION ALL
  SELECT 2, 1 UNION ALL
  SELECT 3, 2
),
x AS (
  SELECT CAST('X' AS varchar(max)) AS x
),
rec_cte AS (
  SELECT
    x,
    level = 0,
    sort1 = 0,
    sort2 = 0,
    sort3 = 0
    /* add new columns here */
  FROM x
  UNION ALL
  SELECT
    x = CAST(x.x + CASE TC56.level WHEN 1 THEN SPACE(LEN(x.x)) ELSE x.x END + x.x
             AS varchar(max)),
    level = x.level + 1,
    sort1 = TC56.level,
    sort2 = (TC56.level * 3 + x.sort1) % 3,
    sort3 = ((TC56.level * 3 + x.sort1) * 3 + x.sort2) % 3
    /* computations of the new columns' values follow the above pattern; e.g.
    sort4 = (((TC56.level * 3 + x.sort1) * 3 + x.sort2) * 3 + x.sort3) % 3 */
  FROM rec_cte x
    CROSS JOIN TC56
  WHERE x.level < 3  /* the necessary level to generate;
                   the last sortN's N number defines the maximum value
                   for which the pattern produced is guaranteed to be correct;
                   in this script the last sort column is sort3, so
                   the maximum 'supported' order is 3 */
)
SELECT * FROM rec_cte
ORDER BY level, sort1, sort2, sort3 /* add the new columns here */

这里的结果:

x                              level       sort1       sort2       sort3
------------------------------ ----------- ----------- ----------- -----------
X                              0           0           0           0
XXX                            1           0           0           0
X X                            1           1           0           0
XXX                            1           2           0           0
XXXXXXXXX                      2           0           0           0
X XX XX X                      2           0           1           0
XXXXXXXXX                      2           0           2           0
XXX   XXX                      2           1           0           0
X X   X X                      2           1           1           0
XXX   XXX                      2           1           2           0
XXXXXXXXX                      2           2           0           0
X XX XX X                      2           2           1           0
XXXXXXXXX                      2           2           2           0
XXXXXXXXXXXXXXXXXXXXXXXXXXX    3           0           0           0
X XX XX XX XX XX XX XX XX X    3           0           0           1
XXXXXXXXXXXXXXXXXXXXXXXXXXX    3           0           0           2
XXX   XXXXXX   XXXXXX   XXX    3           0           1           0
X X   X XX X   X XX X   X X    3           0           1           1
XXX   XXXXXX   XXXXXX   XXX    3           0           1           2
XXXXXXXXXXXXXXXXXXXXXXXXXXX    3           0           2           0
X XX XX XX XX XX XX XX XX X    3           0           2           1
XXXXXXXXXXXXXXXXXXXXXXXXXXX    3           0           2           2
XXXXXXXXX         XXXXXXXXX    3           1           0           0
X XX XX X         X XX XX X    3           1           0           1
XXXXXXXXX         XXXXXXXXX    3           1           0           2
XXX   XXX         XXX   XXX    3           1           1           0
X X   X X         X X   X X    3           1           1           1
XXX   XXX         XXX   XXX    3           1           1           2
XXXXXXXXX         XXXXXXXXX    3           1           2           0
X XX XX X         X XX XX X    3           1           2           1
XXXXXXXXX         XXXXXXXXX    3           1           2           2
XXXXXXXXXXXXXXXXXXXXXXXXXXX    3           2           0           0
X XX XX XX XX XX XX XX XX X    3           2           0           1
XXXXXXXXXXXXXXXXXXXXXXXXXXX    3           2           0           2
XXX   XXXXXX   XXXXXX   XXX    3           2           1           0
X X   X XX X   X XX X   X X    3           2           1           1
XXX   XXXXXX   XXXXXX   XXX    3           2           1           2
XXXXXXXXXXXXXXXXXXXXXXXXXXX    3           2           2           0
X XX XX XX XX XX XX XX XX X    3           2           2           1
XXXXXXXXXXXXXXXXXXXXXXXXXXX    3           2           2           2


文章来源: Doubt in creating a Sierpinski carpet using TSQL (set based)