我改了一下程式
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define Yes 1
#define No 0
int a[49];
int InArray(int t,int index)
{
int i,result=No;
for (i=0;i<=index;i++)
if (a[i]==t)
{ result=Yes;
break;
}
return result;
int main()
{ int b,i,j;
srand((unsigned)time(NULL));
a[0]=rand()%90+10;
//b[0]=a[0];
for(i=1;i<=49;i++)
Here: b = rand()%90+10;
if (InArray(b,i-1)==Yes)
goto Here;
else
/* code */
a[i]=b;
for(i=0;i<=49;i++)
{ printf("%-3d",a[i]);
if((i+1)%10==0)
printf("\n");
執行結果:第一次
37 96 28 48 33 60 66 39 87 51
84 50 97 57 25 32 26 34 42 43
47 46 70 55 53 67 15 73 21 49
83 79 71 82 17 80 64 44 12 31
74 20 23 63 81 30 16 27 90 93
第二次
77 61 37 40 72 60 27 14 36 85
25 12 23 65 24 16 97 39 98 89
33 75 67 68 29 73 53 19 70 82
22 94 58 91 46 76 31 78 50 86
92 56 84 35 69 54 63 51 62 44
我改了一下程式
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define Yes 1
#define No 0
int a[49];
int InArray(int t,int index)
{
int i,result=No;
for (i=0;i<=index;i++)
if (a[i]==t)
{ result=Yes;
break;
}
return result;
}
int main()
{ int b,i,j;
srand((unsigned)time(NULL));
a[0]=rand()%90+10;
//b[0]=a[0];
for(i=1;i<=49;i++)
{
Here: b = rand()%90+10;
if (InArray(b,i-1)==Yes)
goto Here;
else
{
/* code */
a[i]=b;
}
}
for(i=0;i<=49;i++)
{ printf("%-3d",a[i]);
if((i+1)%10==0)
printf("\n");
}
}
執行結果:第一次
37 96 28 48 33 60 66 39 87 51
84 50 97 57 25 32 26 34 42 43
47 46 70 55 53 67 15 73 21 49
83 79 71 82 17 80 64 44 12 31
74 20 23 63 81 30 16 27 90 93
第二次
77 61 37 40 72 60 27 14 36 85
25 12 23 65 24 16 97 39 98 89
33 75 67 68 29 73 53 19 70 82
22 94 58 91 46 76 31 78 50 86
92 56 84 35 69 54 63 51 62 44