魔方算法-SQL版
2 (
3 @num int
4 )
5 as
6 begin
7
8 set nocount on
9
10 declare @i int
11 set @i = SQRT(@num)
12
13 if CEILING(@i) <> @i
14 return
15
16 declare @table varchar(8000)
17 declare @insert varchar(8000)
18 declare @update varchar(8000)
19 declare @print varchar(8000)
20
21 select @insert = '',@update = '',@print = ''
22
23 select @table = '
24 create table #t
25 ('
26
27 declare @k int
28 select @k = 1
29 while (@k <= @i)
30 begin
31 select @table = @table + '
32 [' + cast(@k as varchar) + '] int,'
33 select @insert = @insert + 'insert into #t([1]) select ' + cast(@k as varchar) + ' '
34 select @k = @k + 1
35 end
36
37 select @table = reverse(stuff(reverse(@table),1,1,''))
38
39 select @table = @table + '
40 )
41 '
42
43 select @update = '
44 declare @r int
45 declare @c int
46 select @r = SQRT(' + cast(@num as varchar) + '),@c = 1
47
48 declare @i int
49 select @i = SQRT(' + cast(@num as varchar) + ') + 1
50 declare @s1 char(1)
51 select @s1 = ''+''
52 declare @s2 char(1)
53 select @s2 = ''c''
54 declare @s3 int --用于计算符号
55 select @s3 = 0
56 declare @len int
57 select @len = SQRT(' + cast(@num as varchar) + ')
58 declare @len2 int
59 select @len2 = 0
60
61
62 while (@len > 0)
63 begin
64 if @s3 % 2 = 0
65 select @len = @len - 1
66 select @len2 = @len
67
68 while(@len2 > 0)
69 begin
70 if @s1 = ''+''
71 begin
72 if @s2 = ''c''
73 select @c = @c + 1
74 else
75 select @r = @r + 1
76 end
77 else
78 begin
79 if @s2 = ''c''
80 select @c = @c - 1
81 else
82 select @r = @r - 1
83 end
84
85 declare @sql varchar(8000)
86 select @sql = ''update #t set ['' + cast(@c as varchar) + ''] = '' + cast(@i as varchar) +
87 '' where [1] = '' + cast(@r as varchar)
88 exec (@sql)
89
90 select @len2 = @len2 - 1
91 select @i = @i + 1
92
93 end
94
95 if @s3/2 % 2 = 1
96 select @s1 = ''+''
97 else
98 select @s1 = ''-''
99
100 if @s2 = ''c''
101 select @s2 = ''r''
102 else
103 select @s2 = ''c''
104
105 select @s3 = @s3 + 1
106 end
107
108 --select * from #t
109 '
110
111 select @print = '
112 declare @string varchar(8000)
113 select @string = ''''
114 declare @conn varchar(1000)
115 select @conn =''''
116 declare @ii int
117 select @ii = sqrt('+ cast(@num as varchar) + ')
118 declare @m int
119 select @m = 1
120 declare @n int
121 select @n = 1
122 while (@ii >= @n)
123 begin
124 select @string = ''declare @conn varchar(1000)
125 select @conn =''
126 select @m = 1
127
128 while (@ii >= @m)
129 begin
130 select @string = @string + '' right( SPACE(len(cast('+cast(@num as varchar)+' as varchar))) + cast(['' + cast(@m as varchar) + ''] as varchar), len(cast('+cast(@num as varchar)+' as varchar)) + 1) +''
131 select @m = @m + 1
132 end
133 select @string = reverse(stuff(reverse(@string),1,1,''''))
134 select @string = @string + '' from #t where [1] = '' + cast(@n as varchar) + ''
135 print @conn''
136 exec(@string)
137 select @n = @n + 1
138 end
139 '
140 exec (@table + @insert + @update + @print)
141
142 end
143
exec raoquan 400
/*
1 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58
2 77 144 143 142 141 140 139 138 137 136 135 134 133 132 131 130 129 128 57
3 78 145 204 203 202 201 200 199 198 197 196 195 194 193 192 191 190 127 56
4 79 146 205 256 255 254 253 252 251 250 249 248 247 246 245 244 189 126 55
5 80 147 206 257 300 299 298 297 296 295 294 293 292 291 290 243 188 125 54
6 81 148 207 258 301 336 335 334 333 332 331 330 329 328 289 242 187 124 53
7 82 149 208 259 302 337 364 363 362 361 360 359 358 327 288 241 186 123 52
8 83 150 209 260 303 338 365 384 383 382 381 380 357 326 287 240 185 122 51
9 84 151 210 261 304 339 366 385 396 395 394 379 356 325 286 239 184 121 50
10 85 152 211 262 305 340 367 386 397 400 393 378 355 324 285 238 183 120 49
11 86 153 212 263 306 341 368 387 398 399 392 377 354 323 284 237 182 119 48
12 87 154 213 264 307 342 369 388 389 390 391 376 353 322 283 236 181 118 47
13 88 155 214 265 308 343 370 371 372 373 374 375 352 321 282 235 180 117 46
14 89 156 215 266 309 344 345 346 347 348 349 350 351 320 281 234 179 116 45
15 90 157 216 267 310 311 312 313 314 315 316 317 318 319 280 233 178 115 44
16 91 158 217 268 269 270 271 272 273 274 275 276 277 278 279 232 177 114 43
17 92 159 218 219 220 221 222 223 224 225 226 227 228 229 230 231 176 113 42
18 93 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 112 41
19 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 40
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
*/

浙公网安备 33010602011771号