读书人

■■■■■■■■寻求旋转距阵的最佳方

发布时间: 2012-02-23 22:01:34 作者: rapoo

■■■■■■■■寻求旋转距阵的最佳方案!最佳方案者再赠送200分!■■■■■■■■
declare @tb table (F1 smallint,F2 smallint,F3 smallint,F4 smallint,F5 smallint,F6 smallint)
insert into @tb
select 1 F1,2 F2,3 F3,4 F4,5 F5,6 F6 union all
select 1 F1,2 F2,3 F3,4 F4,5 F5,7 F6 union all
select 1 F1,2 F2,3 F3,4 F4,5 F5,8 F6 union all
select 1 F1,2 F2,3 F3,4 F4,6 F5,7 F6 union all
select 1 F1,2 F2,3 F3,4 F4,6 F5,8 F6 union all
select 1 F1,2 F2,3 F3,4 F4,7 F5,8 F6 union all
select 1 F1,2 F2,3 F3,5 F4,6 F5,7 F6 union all
select 1 F1,2 F2,3 F3,5 F4,6 F5,8 F6 union all
select 1 F1,2 F2,3 F3,5 F4,7 F5,8 F6 union all
select 1 F1,2 F2,3 F3,6 F4,7 F5,8 F6 union all
select 1 F1,2 F2,4 F3,5 F4,6 F5,7 F6 union all
select 1 F1,2 F2,4 F3,5 F4,6 F5,8 F6 union all
select 1 F1,2 F2,4 F3,5 F4,7 F5,8 F6 union all
select 1 F1,2 F2,4 F3,6 F4,7 F5,8 F6 union all
select 1 F1,2 F2,5 F3,6 F4,7 F5,8 F6 union all
select 1 F1,3 F2,4 F3,5 F4,6 F5,7 F6 union all
select 1 F1,3 F2,4 F3,5 F4,6 F5,8 F6 union all
select 1 F1,3 F2,4 F3,5 F4,7 F5,8 F6 union all
select 1 F1,3 F2,4 F3,6 F4,7 F5,8 F6 union all
select 1 F1,3 F2,5 F3,6 F4,7 F5,8 F6 union all
select 1 F1,4 F2,5 F3,6 F4,7 F5,8 F6 union all
select 2 F1,3 F2,4 F3,5 F4,6 F5,7 F6 union all
select 2 F1,3 F2,4 F3,5 F4,6 F5,8 F6 union all
select 2 F1,3 F2,4 F3,5 F4,7 F5,8 F6 union all
select 2 F1,3 F2,4 F3,6 F4,7 F5,8 F6 union all
select 2 F1,3 F2,5 F3,6 F4,7 F5,8 F6 union all
select 2 F1,4 F2,5 F3,6 F4,7 F5,8 F6 union all
select 3 F1,4 F2,5 F3,6 F4,7 F5,8 F6

/*
表@tb是1,2,3,4,5,6,7,8这8个数的组6组合


F1 F2 F3 F4 F5 F6
------ ------ ------ ------ ------ ------


1 2 3 4 5 6
1 2 3 4 5 7
1 2 3 4 5 8
1 2 3 4 6 7
1 2 3 4 6 8
1 2 3 4 7 8
1 2 3 5 6 7
1 2 3 5 6 8
1 2 3 5 7 8
1 2 3 6 7 8
1 2 4 5 6 7
1 2 4 5 6 8
1 2 4 5 7 8
1 2 4 6 7 8
1 2 5 6 7 8
1 3 4 5 6 7
1 3 4 5 6 8


1 3 4 5 7 8
1 3 4 6 7 8
1 3 5 6 7 8
1 4 5 6 7 8
2 3 4 5 6 7
2 3 4 5 6 8
2 3 4 5 7 8
2 3 4 6 7 8
2 3 5 6 7 8
2 4 5 6 7 8
3 4 5 6 7 8
*/


/*
现要随机找出组合中最少且有组合5出现的组合(即采票所说的中6保5/旋转距阵)
下面是其中一种方案结果。


F1 F2 F3 F4 F5 F6
------ ------ ------ ------ ------ ------
1 2 3 4 5 6

1 2 3 4 7 8

1 2 5 6 7 8

1 3 4 6 7 8


*/

[解决办法]
现要随机找出组合中最少且有组合5出现的组合(即采票所说的中6保5/旋转距阵)
下面是其中一种方案结果。


F1 F2 F3 F4 F5 F6
------ ------ ------ ------ ------ ------
1 2 3 4 5 6

1 2 3 4 7 8 ???????

1 2 5 6 7 8

1 3 4 6 7 8 ???????
[解决办法]
下面三列是旋转表,不知道是否对你有用?

将下表数据:
A b c d e
-------------------- ----------- ----------- ----------- -----------
x 1 2 3 4
y 5 6 7 8
z 9 10 11 12

转化成如下结果:
a x y z
-------------------- ---------- ---------- ----------
b 1 5 9
c 2 6 10
d 3 7 11
e 4 8 12

--生成测试数据
create table test1(A varchar(20),b int,c int,d int,e int)
insert into test1 select 'x ',1,2 ,3 ,4
insert into test1 select 'y ',5,6 ,7 ,8
insert into test1 select 'z ',9,10,11,12


--生成中间数据表
declare @s varchar(8000)
set @s= 'create table test2(a varchar(20) '
select @s=@s+ ', '+A+ ' varchar(10) ' from test1
set @s=@s+ ') '
exec(@s)

--借助中间表实现行列转换
declare @name varchar(20)

declare t_cursor cursor for
select name from syscolumns
where id=object_id( 'test1 ') and colid> 1 order by colid

open t_cursor

fetch next from t_cursor into @name

while @@fetch_status=0
begin
exec( 'select '+@name+ ' as t into test3 from test1 ')
set @s= 'insert into test2 select ' ' '+@name+ ' ' ' '
select @s=@s+ ', ' ' '+rtrim(t)+ ' ' ' ' from test3
exec(@s)
exec( 'drop table test3 ')
fetch next from t_cursor into @name
end
close t_cursor
deallocate t_cursor


--查看行列互换处理结果
select * from test1
select * from test2

--删除表
drop table test1
drop table test2
[解决办法]
,LZ一的例子

/*

现要随机找出组合中最少且有组合5出现的组合(即采票所说的中6保5/旋转距阵)
下面是其中一种方案结果。


F1 F2 F3 F4 F5 F6
------ ------ ------ ------ ------ ------
1 2 3 4 5 6

1 2 3 4 7 8 --> 1,3,4,7,8

1 2 5 6 7 8

1 3 4 6 7 8 --> 1,3,4,7,8
*/

也有重啊...
[解决办法]
up

读书人网 >SQL Server

热点推荐