数学中的数学

binjuny 2003-06-05 06:28:26
问题是这样的:生成一个文件,每行 7 个数,每个数都小于100,大于 9 的原样显示,小于或等于9的在数的前面加个"0" 即如:02 03 12 15 每两个数间有一个空格隔开,且新生成的不能和原来生成的任意一组一样,


我的机子每次几乎要死机,,,都得几十分钟运行,,,大家帮我优化一下,另给程序也行,,
看下面我的小程序
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <algorithm>

using namespace std;
typedef struct Elem
{
int elem[7];
Elem* Next;
}Elem_A;

void main( void )
{
Elem_A *p1,*p2,*p3,*p4;

p1 = new struct Elem;
p1->Next = NULL;
p2 = p1;
p3 = p1;
p4 = NULL;

int i0,i1,i2,i3,i4,i5,i6;
FILE* file;
file = fopen("G:\\a.txt", "w+");
srand( (unsigned)time( NULL ) );
int a[7] = {0};
for( i0 = 0; i0 < 6; i0++ )
{
for( i1 = 0; i1 < 6; i1++ )
{
for( i2 = 0; i2 < 6; i2++ )
{
for( i3 = 0; i3 < 6; i3++ )
{
for( i4 = 0; i4 < 6; i4++ )
{
for( i5 = 0; i5 < 6; i5++ )
{
for( i6 = 0; i6 < 6; i6++ )
{
a[0] = rand()%100; a[1] = rand()%100; a[2] = rand()%100; a[3] = rand()%100; a[4] = rand()%100; a[5] = rand()%100; a[6] = rand()%100; for(int i7 = 0; i7 < 7; i7++) { for(int i8 = i7+1; i8 < 7; i8++) { if(a[i7] == a[i8]) { goto LOOP; } } }
sort(a,a+7); p4 = p3; while (p4->Next !=NULL) { int m_iCount = 0; for(int k = 0; k < 7; k++) { for(int k1 = 0; k1 < 7; k1++) { if(p4->elem[k] == a[k1]) { m_iCount++; } }
}
if (m_iCount > 5)
{ goto LOOP;
}
p4 = p4->Next;
}

for(int i9 = 0; i9 < 7; i9++)
{
int X = 0;
if (a[i9]>9)
{
fprintf(file, "%-3d", a[i9] );
}
else
{
fprintf(file, " %d%-2d", X,a[i9] );
}
p1->elem[i9] = a[i9];
}
p1 = new struct Elem;
p1->Next = NULL;
p2->Next = p1;
p2 = p2->Next;

fprintf(file, "\n");
}
LOOP:;
}
}
}
}
}
}
p4 = p3;
while (p4->Next !=NULL)
{
p3 = p3->Next;
delete p4;
p4 = p3;

}
}

...全文
105 33 打赏 收藏 转发到动态 举报
写回复
用AI写文章
33 条回复
切换为时间正序
请发表友善的回复…
发表回复
hookuy 2003-07-13
  • 打赏
  • 举报
回复
c 的代码不是很熟悉 。。
flab_lwq 2003-06-09
  • 打赏
  • 举报
回复
还有是在VC6+WIN2000的环境下通过,一直弄不明白,在TC里能编译通过,但是无法动态分配内存。
flab_lwq 2003-06-09
  • 打赏
  • 举报
回复
使用了binary search tree,速度比较快,不过在测试中,发现随机生成的数很难出现重复,后来粗略算了算,发现出现重复的数的可能非常小,大概在10e-9左右。这个程序原理的随机生成一组数,然后insert到binary树里。最后遍历数。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <process.h>
#include <string.h>

#define MAXSIZE 7
#define SUCCESS 1
#define FAIL 0

//global variable
FILE *fp;
int a[MAXSIZE];

//datastr binary search tree node declaration
struct treeNode {
struct treeNode *leftPtr;
char data[100];
struct treeNode *rightPtr;
};
typedef struct treeNode TREENODE;
typedef TREENODE *TREENODEPTR;

//function declare
int insertNode(TREENODEPTR *, char *);
void inOrder(TREENODEPTR, void(*visit)(char *));
void createLine(char *);
void writeFile(char *);
void freeAll(TREENODEPTR);

int main()
{
unsigned long count;
unsigned int i;
char line[100];
TREENODEPTR rootPtr = NULL;
srand(time(NULL));
if( (fp = fopen("output.txt", "w")) == NULL) {
printf("i can't create the file, sorry:)");
return 1;
}
//mark line string the end char '\0'
line[21] = '\0';
printf("how many lines do u need? ");
scanf("%d", &count);
//construct a binary search tree
for(i=0; i<count; ) {
createLine(line);
if(insertNode(&rootPtr, line) == SUCCESS)
i++;
}
printf("generate completed, waiting for output\n");
//output result to file
inOrder(rootPtr, writeFile);
//free every dynamic allocate memory
freeAll(rootPtr);
fclose(fp);
printf("mission accomplished, check output file\n");
system("pause");
return 0;
}

void createLine(char *line)
{
int i, j;
//assure no duplicate
for(i = 0; i < MAXSIZE; ) {
a[i] = rand()%100;
for(j = 0; j < i; j++)
if(a[i] == a[j]) {
i--;
break;//find duplicate, end compare
}
i++;
}
for(i = 0; i < MAXSIZE; i++) {
line[3*i] = a[i]/10 + '0';
line[3*i+1] = a[i]%10 + '0';
line[3*i+2] = ' ';
}
}

void writeFile(char *line)
{
fprintf(fp, "%s\n", line);
}

//implement of binary search tree
int insertNode(TREENODEPTR *treePtr, char *value)
{
int result = SUCCESS;
if(*treePtr == NULL) {
*treePtr = (TREENODEPTR)malloc(sizeof(TREENODE));
if(*treePtr != NULL) {
strcpy((*treePtr)->data, value);
(*treePtr)->leftPtr = NULL;
(*treePtr)->rightPtr = NULL;
}
else {
printf("No memory available. \n");
exit(1);
}
}
else if(strcmp(value, (*treePtr)->data) < 0)
insertNode(&((*treePtr)->leftPtr), value);
else if(strcmp(value, (*treePtr)->data) > 0)
insertNode(&((*treePtr)->rightPtr), value);
else result = FAIL;

return result;
}

//inorder traverse binary search tree
void inOrder(TREENODEPTR treePtr, void(*visit)(char *))
{
if(treePtr != NULL) {
inOrder(treePtr->leftPtr, visit);
(*visit)(treePtr->data);
inOrder(treePtr->rightPtr, visit);
}
}

//postorder free allocated memory
void freeAll(TREENODEPTR treePtr)
{
if(treePtr != NULL) {
freeAll(treePtr->leftPtr);
freeAll(treePtr->rightPtr);
free(treePtr);
}
}
binjuny 2003-06-09
  • 打赏
  • 举报
回复
Up
flab_lwq 2003-06-06
  • 打赏
  • 举报
回复
10万行?而且还不能重复,恐怖
fengxvhui 2003-06-06
  • 打赏
  • 举报
回复
如果不要求随机就可以大为简化
binjuny 2003-06-06
  • 打赏
  • 举报
回复
对不起,一时疏忽,
不是我不急,因为种种原因,我晚上不能上网,,,所以没看到,,,,,very Sorry!

我忘了写要生成多少行了,,,至少10万行,即每个 for 循环不小于 6,
主要是时间用的太久,,,
binjuny 2003-06-06
  • 打赏
  • 举报
回复
对不起,一时疏忽,
不是我不急,因为种种原因,我晚上不能上网,,,所以没看到,,,,,very Sorry!

我忘了写要生成多少行了,,,至少10万行,即每个 for 循环不小于 6,
主要是时间用的太久,,,
binjuny 2003-06-06
  • 打赏
  • 举报
回复
对不起,一时疏忽,
不是我不急,因为种种原因,我晚上不能上网,,,所以没看到,,,,,very Sorry!

我忘了写要生成多少行了,,,至少10万行,即每个 for 循环不小于 6,
主要是时间用的太久,,,
binjuny 2003-06-06
  • 打赏
  • 举报
回复
人呢,,,,,,大虾们
binjuny 2003-06-06
  • 打赏
  • 举报
回复
100里面的7个数字的全排列是 80678106432000行,,
我说的不重复是指排序后的任意两组数,,不相同,,
UP
flab_lwq 2003-06-06
  • 打赏
  • 举报
回复
为什么不试试求100里面的7个数字的全排列呢?
以前试过求26个字母的全排列,速度不慢。
非要用随机函数生成数字?
binjuny 2003-06-06
  • 打赏
  • 举报
回复
xghbd(xghbd) 这位同行,,你的程序要运行多久,你试过吗,,,,
我整理后的程序
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <algorithm>

using namespace std;
typedef struct Elem
{
int elem[7];
Elem* Next;
}Elem_A;

void main( void )
{
Elem_A *p1,*p2,*p3,*p4;

p1 = new struct Elem;
p1->Next = NULL;
p2 = p1;
p3 = p1;
p4 = NULL;

int i0,i1,i2,i3,i4,i5,i6;
FILE* file;
file = fopen("G:\\a.txt", "w+");
srand( (unsigned)time( NULL ) );
int a[7] = {0};
for( i0 = 0; i0 < 6; i0++ )
{
for( i1 = 0; i1 < 6; i1++ )
{
for( i2 = 0; i2 < 6; i2++ )
{
for( i3 = 0; i3 < 6; i3++ )
{
for( i4 = 0; i4 < 6; i4++ )
{
for( i5 = 0; i5 < 6; i5++ )
{
for( i6 = 0; i6 < 6; i6++ )
{
a[0] = rand()%100;
a[1] = rand()%100;
a[2] = rand()%100;
a[3] = rand()%100;
a[4] = rand()%100;
a[5] = rand()%100;
a[6] = rand()%100;
for(int i7 = 0; i7 < 7; i7++)
{
for(int i8 = i7+1; i8 < 7; i8++)
{
if(a[i7] == a[i8])
{
goto LOOP;
}
}
}
sort(a,a+7);
p4 = p3;

while (p4->Next !=NULL)
{
int m_iCount = 0;
for(int k = 0; k < 7; k++) { for(int k1 = 0; k1 < 7; k1++) { if(p4->elem[k] == a[k1]) { m_iCount++; } }
}
if (m_iCount >= 7)
{
goto LOOP;
}

p4 = p4->Next;
}

for(int i9 = 0; i9 < 7; i9++)
{
int X = 0;
if (a[i9]>9)
{
fprintf(file, "%-3d", a[i9] );
}
else
{
fprintf(file, " %d%-2d", X,a[i9] );
}
p1->elem[i9] = a[i9];
}
fprintf(file, "\n");

p1 = new struct Elem;
p1->Next = NULL;
p2->Next = p1;
p2 = p2->Next;

}
LOOP:;
}
}
}
}
}
}
p4 = p3;
while (p4->Next !=NULL)
{
p3 = p3->Next;
delete p4;
p4 = NULL;
p4 = p3;
}
if(p4->Next == NULL)
{
delete p4;
p4 = NULL;
}
}


dongjianguo 2003-06-05
  • 打赏
  • 举报
回复
对不起,一时疏忽,指针数组定义错误,应该是char * str[100/7+1]
dongjianguo 2003-06-05
  • 打赏
  • 举报
回复
我用c++写了一个,可能你要求用c写,我写出来主要是给你个思想,因为用c 实验起来太难,我一时没时间(偶就快过四级了),所以用c++写
#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>

int Isequal(char *str[],char *nowstr,int n)
{
int flag=0,i;
for(i=0;i<n;i++)
if((strcmp(str[i],nowstr))==0){flag=1 ;break;}
return flag;
}

int main(void)
{
fstream file;
char fn[50],*str[7];
int i,j,k,flag=0,num[100],tmp;

cout<<"请输入文件绝对路径:"<<endl;
cin>>fn;
file.open(fn,ios::in|ios::out);

srand((unsigned)time(NULL));

for(i=0,j=0;i<100;i+=7)
{
do
{
num[i]=rand()%100;
num[i+1]=rand()%100;
num[i+2]=rand()%100;
num[i+3]=rand()%100;
num[i+4]=rand()%100;
num[i+5]=rand()%100;
num[i+6]=rand()%100;
str[j]=(char*)malloc(8*sizeof(char));
tmp=i;
for(k=0;k<7;k++)str[j][k]=num[tmp++];
//将数字信息转换为字符串信息,如果两行相同,那么所生成的字符串必定相同,因为字符也即整数,在-128-127之内是一一对应的;
str[j][k]='\0';
flag=Isequal(str,str[j],j);
//该函数比较新生成行与原有行是否相同,不同置0,则结束while 循环,否则直至生成新的一行
}while(flag);
j++;
}

for(i=0;i<100;i++)
{

if(num[i]<10)file<<"0"<<num[i]<<" ";
else file<<num[i]<<" ";
if((i+1)%7==0)file<<"\n";
}//输出至文件,结果如下

free(str);
file.close();
return 0;
}

结果:
07 62 16 16 66 25 91
39 81 93 57 52 73 99
75 31 06 01 93 86 13
40 54 37 00 26 50 82
86 94 20 79 17 49 84
78 92 69 96 56 73 15
33 61 55 25 88 36 15
78 47 07 31 37 21 14
89 68 02 89 34 88 08
17 72 37 86 43 80 22
57 83 92 95 77 66 40
23 24 43 90 27 45 48
53 91 30 54 91 04 77
87 19 75 95 70 38 41
08 28
chinajiji 2003-06-05
  • 打赏
  • 举报
回复
/* 生成一个文件,每行 7 个数,每个数都小于100,大于 9 的原样显示,小于或等于9的在
数的前面加个"0" 即如:02 03 12 15 每两个数间有一个空格隔开,且新生成的不能和原来
生成的任意一组一样.
*/

#pragma warning(disable:4786)

#include <iostream>
#include <ctime>
#include <algorithm>
#include <vector>
#include <set>
#include <fstream>
#include <iterator>
#include <iomanip>
#include <cstdlib>
#include <cmath>
using namespace std;

class nRand {
public:
nRand(int limit) : m_Limit(limit) {}
int operator()() const { return rand() % m_Limit; }
private:
int m_Limit;
};

class genAline {
public:
genAline(set< vector<int> > &set,
const nRand &rnd,
int count = 7) : m_set(set), m_rand(rnd), m_count(count) {}
vector<int> operator()() {
vector<int> aline;
aline.reserve(m_count);
do {
aline.clear();
for(int i = 0; i < m_count; i++)
aline.push_back(m_rand());
}
while( m_set.find(aline) != m_set.end() );
vector<int>::const_iterator iter = aline.begin();
return aline;
}
private:
set< vector<int> > &m_set;
const nRand &m_rand;
int m_count;
};

int main() {
srand(time(0));
const int NUM_PER_LINE = 7;
const int MAX = 100;
set< vector<int> > result;
int LINES = 0;
cout << "How many lines do you want to output: ";
cin >> LINES;
generate_n(inserter< set< vector<int> > >(result, result.begin()),
LINES, genAline(result, nRand(MAX), NUM_PER_LINE));

ofstream outputFile("result.txt");
if(outputFile == NULL) {
cout << "Can NOT creat file: result.txt!" << endl;
exit(-1);
}
outputFile << " output lines: " << result.size() << endl;
outputFile.fill('0');

// output all numbers into file: result.txt
cout << "caculated finished, "
<< "Now output all numbers into file: result.txt" << endl;
set< vector<int> >::const_iterator iter = result.begin();
for(; iter != result.end(); iter++) {
vector<int>::const_iterator vIter = iter->begin();
for(; vIter != iter->end(); vIter++)
outputFile << setw(2) << *vIter << ' ';
outputFile << endl;
}

cout << "all numbers have been written to file: result.txt!" << endl;
system("pause");
return 0;
}
bm1408 2003-06-05
  • 打赏
  • 举报
回复
your question is wrong!
i don't understand what you said!
fengfeng2003 2003-06-05
  • 打赏
  • 举报
回复
#include "vector"
#include "iostream"
#include "algorithm"
#include "stdlib.h"
#include "time.h"
#include "fstream"

using namespace std;

//得到[LowerLimit,UpperLimit]间的整数随机数
class Random
{
public:
Random(int L,int U):LowerLimit(L),UpperLimit(U)
{
srand((unsigned)time(NULL));
}

int operator()()
{
return rand()%(UpperLimit-LowerLimit+1)+LowerLimit;
}
private:
int UpperLimit;
int LowerLimit;
};


void ToFile(vector<int>& v)
{
fstream f;
f.open("result.txt");
f.seekp(0,ios::end);
vector<int>::iterator it;
for(it=v.begin();it!=v.end();it++)
{
if((*it)<10)
{
f<<0<<(*it)<<'\t';
}
else
{
f<<(*it)<<'\t';
}
}
f<<endl;
f.close();
}


void main()
{
Random r(1,100);

vector<int> v(7);
//产生十组数
for(int i=0;i<10;i++)
{
generate(v.begin(),v.end(),r);
ToFile(v);
}
}
robertcarlos 2003-06-05
  • 打赏
  • 举报
回复
#include<match.h>
main()
{int i=1,n,a[];
ll: while (leap)
{n=rand()%100; //取随机数,可能有点不对,因为我第一次用
for (i=1;i<100;i++)
while(n==a[i]) goto ll; //用了goto,但是方便啊,一样就重来
i++;
a[i]=n;
if (i==100) leap=0;
}
for (i=1;i<100;i++)
{if (a[i]<9) printf ("0%3d",a[i]); //小于10的数输出
if (i%7==0) printf("\n");} //7个回车
}


小弟不才,只能到这里~~
fengxvhui 2003-06-05
  • 打赏
  • 举报
回复
看样子楼主自己都不着急,呵呵。
加载更多回复(13)

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧