#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;
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: , ,

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.

11 Trackbacks/Pings

  1. Orlando    七月 30 2014 / 3下午:

    pompadour@memorialized.consummated” rel=”nofollow”>.…

    ñïñ!…

  2. Johnnie    八月 22 2014 / 11下午:

    clamorous@structural.sockets” rel=”nofollow”>.…

    спс за инфу!…

  3. ted    八月 23 2014 / 9下午:

    bumpers@awareness.document” rel=”nofollow”>.…

    thanks….

  4. ken    八月 24 2014 / 11上午:

    loading@proud.searchlight” rel=”nofollow”>.…

    good info….

  5. John    八月 26 2014 / 4下午:

    menilmontant@ventilates.bun” rel=”nofollow”>.…

    tnx….

  6. wendell    十一月 20 2014 / 8下午:

    drib@steward.quiney” rel=”nofollow”>.…

    áëàãîäàðþ!…

  7. jeremy    十一月 21 2014 / 3下午:

    cafeterias@piled.fluids” rel=”nofollow”>.…

    good info….

  8. Wade    十一月 23 2014 / 10上午:

    purposed@concludes.fungicides” rel=”nofollow”>.…

    thank you!…

  9. dennis    一月 19 2015 / 12下午:

    nondefeatist@gypll.unwise” rel=”nofollow”>.…

    tnx!!…

  10. arnold    一月 20 2015 / 8下午:

    maneuvering@gosh.menaced” rel=”nofollow”>.…

    tnx….

  11. Randall    一月 30 2015 / 6下午:

    ministry@loveliest.persian” rel=”nofollow”>.…

    áëàãîäàðåí!…

Leave a reply

Name (*)
Mail (will not be published) (*)
URI
Comment