日期:2014-05-18  浏览次数:20618 次

算法过硬的人进来,如何最快速地把彩票双色球33选7的所有可能组合写入数据库
只算6个红号
数学公式算出来 C33(6)=(33*32*31*30*29*28)/(6*5*4*3*2) = 1107568 
也就是说插入数据库后最终有一百一十多万条记录, 我数学不好就只采用了笨办法转成二进制,从1 到 33位进制地对比如111111000000000000000000000000
而 111111000000000000000000000000000 = 8455716864 也就循环84亿多次,把每个数值转成二进制字符串,判断如果包含1的个数为6 就如库,可是效率太慢了,我的双核处理器 跑了个晚上才跑到10亿多,这样算下来 差不多要连续开机4天才能把数据入库,谁有什么高招快速地算出数据入库?


 还有一个问题为什么这么慢呢 现在的处理器都是GHZ 1HZ 相当于 1000*1000*1000 每秒运算10亿次,我AMD 3600++ 相当于3.6GHZ 算起来每秒运算36亿次,咋个我跑了一晚晚才跑完10亿个循环,当然还包括入库,界面元素更新等操作

------解决方案--------------------
SQL code
select top 33 identity(int,1,1) id into #t from sys.objects

select a.id id1,b.id id2,c.id id3,d.id id4,e.id id5,f.id id6
    from #t a cross join #t b cross join #t c cross join #t d cross join #t e cross join #t f 
    where a.id<>b.id and a.id<>c.id and a.id<>d.id and a.id<>e.id and a.id<>f.id
    and b.id<>c.id and b.id<>d.id and b.id<>e.id and b.id<>f.id
    and c.id<>d.id and c.id<>e.id and c.id<>f.id
    and d.id<>e.id and d.id<>f.id
    and e.id<>f.id
/*结果
id1,id2,id3,id4,id5,id6
-----------------------
4    6    5    2    3    1
4    7    5    2    3    1
4    8    5    2    3    1
4    9    5    2    3    1
4    10    5    2    3    1
4    11    5    2    3    1
4    12    5    2    3    1
4    13    5    2    3    1
4    14    5    2    3    1
4    15    5    2    3    1
4    16    5    2    3    1
4    17    5    2    3    1
4    18    5    2    3    1
4    19    5    2    3    1
4    20    5    2    3    1
4    21    5    2    3    1
4    22    5    2    3    1
4    23    5    2    3    1
4    24    5    2    3    1
4    25    5    2    3    1
...
...
...
*/