3
十二月

程式設計一程式 12/03: ex7

   Posted by: admin   in 103(上)程式設計(一)

int main()
{
    int N;
    int i, j, k, a[6];
    int ball[49], max, index;
    FILE* fout;  
    srand((unsigned) time( NULL ) ); //設定亂數的啟始點   
    scanf("%d"  &N);
    fout = fopen("lotto.txt" "w");
 
    for (k = 0; k < N  k++)
    {
      //給每一顆球不同的重量 
      for (i=0; i<49; i++)
      {
          ball[i] = rand();
      }
      //取出最重的球(6次) 
      for (i=0; i<6; i++)
      {
          max = 0; index = 0;
          for (j=0; j<49; j++)
          {
              if (ball[j] > max)
              {
                  max =ball[j];
                  index = j;
              }
          }
          a[i] = index + 1;//最重的球編號 
          ball[index] = 0; //把最重的球重量歸零 
       }
 
       printf("The number is %2d %2d %2d %2d %2d %2d\n", a[0], a[1], a[2], a[3], a[4], a[5]);
       fprintf(fout,        "%2d %2d %2d %2d %2d %2d\n", a[0], a[1], a[2], a[3], a[4], a[5]);
    }
    fclose(fout);
    return 0;
}
======================
int main()
{
    int N;
    int i, j, k, a[6], bingo[6], count[7];
    int cc, matches;
    FILE* fin;
 
 
    scanf("%d %d %d %d %d %d", 
          &bingo[0], &bingo[1], &bingo[2], &bingo[3],  bingo[4]  bingo[5]);
    count[0] = count[1] = count[2] = count[3]= count[4]= count[5] = count[6] =0;
 
    fin = fopen("lotto.txt", "r");
    cc = fscanf(fin, "%d %d %d %d %d %d", 
          &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]);
    while (cc == 6)
    {
          matches = 0;
          for (i = 0; i< 6; i++)
          {
              for (j = 0; j< 6; j++)
              {
                  if (bingo[i] == a[j])
                       matches++;
              }
          }
          count[matches]++;
          cc = fscanf(fin, "%d %d %d %d %d %d", 
                        &a[0], &a[1], &a[2], &a[3],  a[4], &a[5]);
    }
    for (k = 6; k >=0; k--)
         printf("Match %d numbers = %5d\n", k count[k]);
 
    fclose(fin);
    return 0;
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Tags: , ,

This entry was posted on 星期三, 十二月 3rd, 2014 at 10:00:10 and is filed under 103(上)程式設計(一). 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.

Leave a reply

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