搜尋此網誌

[C/C++][49選6+1大樂透]

大樂透開獎時,開獎單位將隨機開出六個彼此不重複的號碼加一個特別號,以下則是依照此規則的c程式

/*
* File Name: Lottery.c
* Author: MH
* Since 2011/04/06
* Toolkit: Dev C++
*/

# include <stdlib.h>
# include <stdio.h>
# include <time.h>    // rand()

int main(){

    int i, j, temp;
    int num[7];    // 6 numbers and one special number

    srand(time(NULL));    // make random values with time

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

        temp = (rand()%49)+1;    // generate a radom value between 1 to 49

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

            if(temp == num[j]){    // check if there're numbers memoried before
                i--;    // if yes, go back to previous array
                break;    // then break
            }

        }

        if(temp != num[j])    // because of break commend, check if the number is not memoried
            num[i] = temp;    // if not, store it

    }

    printf("general number : ");

    for(i=0; i<6; i++)
        printf("%3d", num[i]);

    printf("\nspecial number : %3d\n\n", num[i]);

    system("Pause");
    return 0;
}

1 則留言: