主题:[讨论]紧急求助各位高手数据归类的问题!!!
以下是主程序及运行示例,各位帮忙看一下有什么问题,因为要量较少时程序没有什么问题,可是ns在30000附近时运行不出结果,但是又不会报错,请各位高手帮忙解决一下!诚恳
致谢!!!
此处归类的意思就是将有关联的数据归在一起,如运行的结果那样
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
typedef struct
{
int num[2]; // 等归类的和对数据
int tag; // 类号
char flg[2]; // 使用标记
}tosort;
// 归类主函数
void SORT(tosort *s, int ns, int i, int nKind)
{
int j;
s[i].tag = nKind;
s[i].flg[0] = s[i].flg[1] = 1;
for (j=0; j<ns; j++)
{
if (s[j].flg[0] == 1) continue;
if (s[j].num[0]==s[i].num[0] ||
s[j].num[0]==s[i].num[1] ||
s[j].num[1]==s[i].num[0] ||
s[j].num[1]==s[i].num[1])
{
SORT(s, ns, j, nKind);
}
}
}
// 用于排序
void px(int *s, int xs)
{
int i, j;
int tmp;
for (i=0; i<xs; i++)
{
for (j=i+1; j<xs; j++)
{
if (s[i] > s[j])
{
tmp = s[i];
s[i] = s[j];
s[j] = tmp;
}
}
}
}
void main()
{
int ns = 10;
int i, j, u, v;
int nKind = 1;
tosort s[100];
int kind[100][100] = { 0 };
int enkind[100] = { 0 };
memset(s, 0, sizeof(s));
srand(unsigned int(time(NULL)));
i = 0;
do
{
s[i].num[0] = rand() % 20;
s[i].num[1] = rand() % 20;
if (s[i].num[0] != s[i].num[1])
{
i++;
}
}while (i <ns);
for (i=0; i<ns; i++)
{
printf("%2d : %2d %2d\n", i, s[i].num[0], s[i].num[1]);
}
printf("\n");
for (i=0; i<ns; i++)
{
if (s[i].flg[0] == 0)
{
SORT(s, ns, i, nKind++);
}
}
printf("nKind = %d\n\n", nKind);
for (i=1; i<nKind; i++)
{
for (j=0; j<ns; j++)
{
if (s[j].tag == i)
{
for (u=0; u<2; u++)
{
for (v=0; v<enkind[i]; v++)
{
if (s[j].num[u] == kind[i][v])
{
break;
}
}
if (v == enkind[i])
{
kind[i][enkind[i]++] = s[j].num[u];
}
}
}
}
}
for (i=1; i<nKind; i++)
{
px(kind[i], enkind[i]);
printf("%3d : ", i);
for (j=0; j<enkind[i]; j++)
{
printf("%2d ", kind[i][j]);
}
printf("\n");
}
}
运行结果:
0 : 1 3
1 : 1 4
2 : 18 6
3 : 7 19
4 : 3 1
5 : 14 17
6 : 6 1
7 : 10 6
8 : 11 4
9 : 18 1
nKind = 4
1 : 1 3 4 6 10 11 18
2 : 7 19
3 : 14 17
致谢!!!
此处归类的意思就是将有关联的数据归在一起,如运行的结果那样
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
typedef struct
{
int num[2]; // 等归类的和对数据
int tag; // 类号
char flg[2]; // 使用标记
}tosort;
// 归类主函数
void SORT(tosort *s, int ns, int i, int nKind)
{
int j;
s[i].tag = nKind;
s[i].flg[0] = s[i].flg[1] = 1;
for (j=0; j<ns; j++)
{
if (s[j].flg[0] == 1) continue;
if (s[j].num[0]==s[i].num[0] ||
s[j].num[0]==s[i].num[1] ||
s[j].num[1]==s[i].num[0] ||
s[j].num[1]==s[i].num[1])
{
SORT(s, ns, j, nKind);
}
}
}
// 用于排序
void px(int *s, int xs)
{
int i, j;
int tmp;
for (i=0; i<xs; i++)
{
for (j=i+1; j<xs; j++)
{
if (s[i] > s[j])
{
tmp = s[i];
s[i] = s[j];
s[j] = tmp;
}
}
}
}
void main()
{
int ns = 10;
int i, j, u, v;
int nKind = 1;
tosort s[100];
int kind[100][100] = { 0 };
int enkind[100] = { 0 };
memset(s, 0, sizeof(s));
srand(unsigned int(time(NULL)));
i = 0;
do
{
s[i].num[0] = rand() % 20;
s[i].num[1] = rand() % 20;
if (s[i].num[0] != s[i].num[1])
{
i++;
}
}while (i <ns);
for (i=0; i<ns; i++)
{
printf("%2d : %2d %2d\n", i, s[i].num[0], s[i].num[1]);
}
printf("\n");
for (i=0; i<ns; i++)
{
if (s[i].flg[0] == 0)
{
SORT(s, ns, i, nKind++);
}
}
printf("nKind = %d\n\n", nKind);
for (i=1; i<nKind; i++)
{
for (j=0; j<ns; j++)
{
if (s[j].tag == i)
{
for (u=0; u<2; u++)
{
for (v=0; v<enkind[i]; v++)
{
if (s[j].num[u] == kind[i][v])
{
break;
}
}
if (v == enkind[i])
{
kind[i][enkind[i]++] = s[j].num[u];
}
}
}
}
}
for (i=1; i<nKind; i++)
{
px(kind[i], enkind[i]);
printf("%3d : ", i);
for (j=0; j<enkind[i]; j++)
{
printf("%2d ", kind[i][j]);
}
printf("\n");
}
}
运行结果:
0 : 1 3
1 : 1 4
2 : 18 6
3 : 7 19
4 : 3 1
5 : 14 17
6 : 6 1
7 : 10 6
8 : 11 4
9 : 18 1
nKind = 4
1 : 1 3 4 6 10 11 18
2 : 7 19
3 : 14 17