Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

C語言寫陣列函數

隨機產生6個 介於1到49(含1及49)的不相同的整數

不能重複

用C語言寫

2 Answers

Rating
  • 1 decade ago
    Favorite Answer

    //Power by Visual Studio 2005

    //Download Site: http://www.microsoft.com/taiwan/vstudio/express/

    #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): 僅供抄襲
  • 1 decade ago

    #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): 人間電腦
Still have questions? Get your answers by asking now.