彩票问题 如何将35选7中6724520种组合结果在10秒中之内写如数据库,文本也行(200分)

  • 主题发起人 1982ybsybs
  • 开始时间
1

1982ybsybs

Unregistered / Unconfirmed
GUEST, unregistred user!
如何将35选7中6724520种组合结果在10秒中之内写如数据库,文本也行
 
为什么要10秒?
要知道速度与很多因素有关,比如机器、算法等。
另外,10秒是否包含计算组合的时间?
 
CPU CR 1.7G
128 DDR内存
有没有好的算法
 
呵呵,你好贪心,已有解决办法了。稍等。。。。。。
 
不要写入数据库,没什么必要,要写每次的话,每次只写筛选完的结果就行!
用的时候,在内存中定义一个数组,每次用之前初始化,比从数据库中调快得多!
 
[[:(]我的算法用了40秒(35选7写到文本里),老板说太慢,让速度控制在10秒以内
 
好家伙,差点把我的机器干瘫。
我是这样实现的:
1、利用sqlserver数据库。
2、建立一个表:ss,字段s,int型,存入1-35的数据。
3、建立视图:view1如下:
CREATE VIEW dbo.VIEW1
AS
SELECT dbo.ss.s, ss_1.s AS Expr1, ss_2.s AS Expr2, ss_3.s AS Expr3, ss_4.s AS Expr4,
ss_5.s AS Expr5, ss_6.s AS Expr6
FROM dbo.ss INNER JOIN
dbo.ss ss_1 ON dbo.ss.s < ss_1.s INNER JOIN
dbo.ss ss_2 ON ss_1.s < ss_2.s INNER JOIN
dbo.ss ss_3 ON ss_2.s < ss_3.s INNER JOIN
dbo.ss ss_4 ON ss_3.s < ss_4.s INNER JOIN
dbo.ss ss_5 ON ss_4.s < ss_5.s INNER JOIN
dbo.ss ss_6 ON ss_5.s < ss_6.s
视图中就是你要的组合,不用再存了。
 
没这么厉害吧
 
我不能用SQL Server数据库,Paradox无法创建视图,只能用Paradox 或者文本,有没有别的方法,李老大
 
把ss分为35个表也许会更快、更便于查询,自己试试吧。
 
这是我的代码:
#include <iostream>
#include <fstream>
#include <string>
#include <memory>
#include <io.h>
// ---------------------------------------------
filebuf buffer;
ostream output(&amp;buffer);
buffer.open(filename,ios::eek:ut|ios::trunc);

char *hao[35];
int i1,i2,i3,i4,i5,i6,i7;
hao[0]="01",hao[1]="02",hao[2]="03",hao[3]="04",hao[4]="05",hao[5]="06",
hao[6]="07",hao[7]="08",hao[8]="09",hao[9]="10",hao[10]="11",hao[11]="12",
hao[12]="13",hao[13]="14",hao[14]="15",hao[15]="16",hao[16]="17",hao[17]="18",
hao[18]="19",hao[19]="20",hao[20]="21",hao[21]="22",hao[22]="23",hao[23]="24",
hao[24]="25",hao[25]="26",hao[26]="27",hao[27]="28",hao[28]="29",hao[29]="30",
hao[30]="31",hao[31]="32",hao[32]="33",hao[33]="34",hao[34]="35";

for(i1=1;i1<=29;i1++)
for(i2=i1+1;i2<=30;i2++)
for(i3=i2+1;i3<=31;i3++)
for(i4=i3+1;i4<=32;i4++)
for(i5=i4+1;i5<=33;i5++)
for(i6=i5+1;i6<=34;i6++)
for(i7=i6+1;i7<=35;i7++)
{
output<<hao[i1-1]<<" "<<hao[i2-1]<<" "<<hao[i3-1]<<" "<<hao[i4-1]<<" "<<hao[i5-1]<<" "<<hao[i6-1]<<" "<<hao[i7-1]<<endl;

}
CB5用了200秒,CB6用了40秒, 如何再让它快点
 
我试过了,达不到要求。这么简单的程序无法优化。
还是sqlserver的方法较好。
另外还有两个建议,一正一邪:
1、用多线程,一个计算,一个存储。协调好缓冲区。
2、如果老板不看源程序,可以在程序启动时调用另一个程序真正执行操作,本程序什么也不做,等10秒左右,提示总用时,完成,退出。没办法,谁让他逼咱了呢。
 
我用Delphi,楼主的问题主要出在写文件上,楼主用的是比较方便然而低效的方法。
不如自己写文件来得快。另外生成的文件有160M,楼主要看看自己的硬盘如何了,
还有内存也是一个问题。CPU在这里可以忽略不记,在偶机器上计算过程约1秒,
cpu和你的一样,内存256ddr,硬盘不清楚,应该是7200,2M缓存的,总共耗时约8秒,
代码如下:
var
a,b,c,d,e,f,g: Integer;
i: integer;
tick: cardinal;
fi: textfile;
table: array [1..35] of string[2];
p: pchar;
begin
assignfile(fi,'c:/test.txt');
Rewrite(fi);
tick := gettickcount;
for i:= 1 to 35do
table := FormatFloat('00',i);
p := StrAlloc(21);
for i:= 0 to 20do
p := ' ';
i := 0;
for a := 1 to 29do
begin
Move(table[a][1],p[0],2);
for b := a+1 to 30do
begin
Move(table[1],p[3],2);
for c:= b+1 to 31do
begin
Move(table[c][1],p[6],2);
for d:= c+1 to 32do
begin
Move(table[d][1],p[9],2);
for e:= d+1 to 33do
begin
Move(table[e][1],p[12],2);
for f:= e+1 to 34do
begin
Move(table[f][1],p[15],2);
for g:= f+1 to 35do
begin
Move(table[g][1],p[18],2);
Writeln(fi,p);
inc(i);
end;
end;
end;
end;
end;
end;
end;
Closefile(fi);
caption := inttostr(i);
caption := caption+'|'+inttostr(gettickcount-tick);
end;
 
非要写文件,先写到缓存中,最后一块写效率高
 
char *buf;
__int64 curcount=0;
static int const rec_len = 21;
static int const max_count = 1024*1024;

static int const buf_size = rec_len * max_count;
buf=new char[buf_size];
// std::auto_ptr<char> buf(new char[buf_size]);
char *hao[35];
int i1,i2,i3,i4,i5,i6,i7;
hao[0]="01",hao[1]="02",hao[2]="03",hao[3]="04",hao[4]="05",hao[5]="06",
hao[6]="07",hao[7]="08",hao[8]="09",hao[9]="10",hao[10]="11",hao[11]="12",
hao[12]="13",hao[13]="14",hao[14]="15",hao[15]="16",hao[16]="17",hao[17]="18",
hao[18]="19",hao[19]="20",hao[20]="21",hao[21]="22",hao[22]="23",hao[23]="24",
hao[24]="25",hao[25]="26",hao[26]="27",hao[27]="28",hao[28]="29",hao[29]="30",
hao[30]="31",hao[31]="32",hao[32]="33",hao[33]="34",hao[34]="35";
filebuf buffer;
ostream output(&amp;buffer);
buffer.open("xxxx.txt",ios::eek:ut|ios::trunc);
for(i1=1;i1<=29;i1++)
for(i2=i1+1;i2<=30;i2++)
for(i3=i2+1;i3<=31;i3++)
for(i4=i3+1;i4<=32;i4++)
for(i5=i4+1;i5<=33;i5++)
for(i6=i5+1;i6<=34;i6++)
for(i7=i6+1;i7<=35;i7++)
{
++curcount;
buf[curbyte-1]='01';
//改成:buf[curbyte-1]=hao[i1-1];就编译不过去.
curbyte++;
buf[curbyte-1]='02';
curbyte++;
buf[curbyte-1]='03';
curbyte++;
buf[curbyte-1]='04';
curbyte++;
buf[curbyte-1]='05';
curbyte++;
buf[curbyte-1]='06';
curbyte++;
buf[curbyte-1]='07';
curbyte++;
buf[curbyte-1]='/n';
if(max_count==curcount)
{
output.write(buf,curcount*rec_len);
curcount=0;
delete buf;
buf=new char[buf_size];
// std::auto_ptr<char> buf(new char[buf_size]);
}
}
大概用了5'多 代码还有很多问题,谁能帮忙看一下,先谢了!!!
 
hao是字符串,buf[curbyte-1]是字符
 
可能机器的问题,用你的程序也不快。
 
DoubleWood的快,文件大小147M
 
不是机器的问题,是147兆频繁的读写。10秒是无法完成的。
我用 TC2.0 测试,计算:约1.2秒
生成文本文件约 3 分钟;
生成数据库待测试;(估计更慢)
 
同意楼上.147兆频繁的读写。10秒是无法完成的。就算一起写,好像也不能完成阿,除非你 的机器特高档
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
1K
DelphiTeacher的专栏
D
顶部