電腦隨機產生樂透號碼
- 從01~49中任意產生6個號碼, 不能重複
- 必須用function,傳陣列給function
- (1)主程式中,輸入6個號碼(你買的號碼)
- (2)主程式中印出中獎號碼 (10%)
- (3)主程式中印出你猜中的號碼(10%)
#include <stdio.h> #include <stdlib.h> #include <time.h> #define NUMBER_SIZE 6 void produce_number(int system_number[],int number_size); int number_check(int user_number[],int system_nmuber[],int bingo[],int number_size); void main (void) { int system_number[NUMBER_SIZE],user_number[NUMBER_SIZE],bingo[NUMBER_SIZE],bingo_conter,i=0; //使用者輸入號碼 while(i < NUMBER_SIZE) { int test_result=1; printf("please enter No. %d number >",i+1); scanf("%d",&user_number[i]); for(int j=0;j<i;j++) if(user_number[i]==user_number[j] || user_number[i] > 49) test_result=0; if(test_result) i++; } //產生號碼(開獎) produce_number(system_number,NUMBER_SIZE); //丟入驗證 bingo_conter=number_check(user_number,system_number,bingo,NUMBER_SIZE); //顯示開獎結果 printf("The number you choose :\n"); for(int i=0;i<NUMBER_SIZE;i++) printf("%d\t",user_number[i]); printf("\n"); printf("The Bingo number is \n"); for(int i=0;i<NUMBER_SIZE;i++) printf("%d\t",system_number[i]); printf("\n"); //告知中獎號碼數與中獎號碼 if(bingo_conter) { printf("%d number(s) bingo,and the number(s) is\n",bingo_conter); for(int i=0;i<bingo_conter;i++) printf("%d\t",bingo[i]); } else { printf("沒中獎,請繼續努力"); } printf("\n"); system("pause"); } void produce_number(int system_number[],int number_size) { srand(time(NULL)); int i=1; system_number[0]=1+rand()%49; while(i < number_size) { int temp=1+rand()%49,test_result=1; for(int j=0;j<i;j++) if(temp==system_number[j]) test_result=0; if(test_result) { system_number[i]=temp; i++; } } /* for(int i=0;i<number_size;i++) system_number[i]=1+rand()%49; */ } int number_check(int user_number[],int system_nmuber[],int bingo[],int number_size) { int bingo_counter=0; for(int i=0;i<number_size;i++) { for(int j=0;j<number_size;j++) { if(user_number[i]==system_nmuber[j]) { bingo[bingo_counter]=user_number[i]; bingo_counter++; } } } return bingo_counter; }
沒有留言:
張貼留言