# C語言寫陣列函數

Rating

//Power by Visual Studio 2005

#include<stdio.h>

#include<stdlib.h>

#include<time.h>

#define MAX 6

int isrepeat(int Length, int *Number, int Key){

int l=Length,*n=Number,k=Key,flag=0,i;

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

if(n[i]==k){

flag=1;

break;

}

}

return flag;

}

int main(int argc, char* argv[]){

//=====START=====//

int i,n[MAX];

srand((unsigned)time(NULL));

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

do{

n[i]=rand()%48+1;

}while(isrepeat(i,n,n[i]));

printf(" %d%s",n[i],(i==(MAX-1)?"\n":""));

}

//=====END=====//

system("PAUSE");

return 0;

}

Source(s): 僅供抄襲

#include<stdio.h>

#include<time.h>

#include<stdlib.h>

#define MAXNUM 6//修改此值增加亂數個數

int main()

{

int a[MAXNUM], i = 0, j;

srand(time(NULL));//亂數種子

do {

a[i] = rand() % 49 + 1;//產生1~49的亂數

for(j = 0; j < i; j++)

if(a[i] == a[j]) {

printf("發現重複 a[%d]=a[%d]=%d\n", i, j, a[i]);

i--;

break;

}

i++;

} while(i < MAXNUM);

for(i = 0; i < MAXNUM; i++)

printf("a[%d]=%d\n", i, a[i]);

system("pause");

return 0;

}

====範例輸出====

發現重複 a[3]=a[2]=6

a[0]=29

a[1]=23

a[2]=6

a[3]=15

a[4]=48

a[5]=35

請按任意鍵繼續 . . .

Source(s): 人間電腦