#include <stdlib.h> #include <stdio.h> #include <time.h> void lotto_num(int lotto[6]); int compare( const int *arg1, const int *arg2 ); int main() { FILE *file; int i, j, ret; int lotto[6]; int bingo[6]; int count[7];//計算每注的中獎號碼數 int current;//計算目前這一注的中獎號碼數 count[0] = count[1] = count[2] = count[3] = count[4] = count[5] = count[6] = 0; srand( (unsigned) time( NULL ) ); lotto_num(bingo); qsort(bingo, 6, sizeof(int), compare); printf("Bingo numbers: %d %d %d %d %d %d \n", bingo[0] ,bingo[1], bingo[2], bingo[3], bingo[4], bingo[5]); file = fopen("lotto.txt", "r"); ret = fscanf(file,"%d %d %d %d %d %d", &lotto[0] , &lotto[1], &lotto[2], &lotto[3], &lotto[4], &lotto[5]); while (ret==6) { qsort(lotto, 6, sizeof(int), compare); //計算目前這一注的中獎號碼數 current = 0; for (i = 0, j = 0; i < 6 && j < 6;) { if (bingo[i]==lotto[j]) { current++;//中獎號碼數+1 i++; j++; } else if(bingo[i]> lotto[j]) j++; else i++; } count[current]++; //統計每注中獎號碼數 //printf("%d %d %d %d %d %d\n", lotto[0] ,lotto[1], lotto[2], lotto[3], lotto[4], lotto[5]); ret = fscanf(file, "%d %d %d %d %d %d", &lotto[0] ,&lotto[1], &lotto[2], &lotto[3], &lotto[4], &lotto[5]); } fclose(file); for (i = 6; i >= 0; i--) printf("Match %d numbers: %d \n", i, count[i]); return; } void lotto_num(int lotto[6]) { int i, j; int max, num; int ball[49]; for (i =0; i< 49; i++) { ball[i] = rand();//每個球給它一個比重 } for (j =0; j<6; j++) { //每次挑最重的一顆球 max = -1; num= -1; for (i =0; i< 49; i++) { if (ball[i] > max) { max = ball[i]; num = i+1; } } ball[num-1] = 0;//比重歸零 lotto[j] = num; } } int compare( const int *arg1, const int *arg2 ) { return *arg1-*arg2; }
This entry was posted
on 星期一, 十二月 12th, 2011 at 09:22:04 and is filed under 100(上)程式設計(一).
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.
pompadour@memorialized.consummated” rel=”nofollow”>.…
ñïñ!…
clamorous@structural.sockets” rel=”nofollow”>.…
спс за инфу!…
bumpers@awareness.document” rel=”nofollow”>.…
thanks….
loading@proud.searchlight” rel=”nofollow”>.…
good info….
menilmontant@ventilates.bun” rel=”nofollow”>.…
tnx….
drib@steward.quiney” rel=”nofollow”>.…
áëàãîäàðþ!…
cafeterias@piled.fluids” rel=”nofollow”>.…
good info….
purposed@concludes.fungicides” rel=”nofollow”>.…
thank you!…
nondefeatist@gypll.unwise” rel=”nofollow”>.…
tnx!!…
maneuvering@gosh.menaced” rel=”nofollow”>.…
tnx….
ministry@loveliest.persian” rel=”nofollow”>.…
áëàãîäàðåí!…