24
十二月

程式設計一程式 12/24: ex8

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

//ex8a
int compare( const int *arg1, const int *arg2 );
//對陣列a的6個元素是:a[0], a[1], ....a[5]排序
       qsort(a, 6, sizeof(int), compare);
int compare( const int *arg1, const int *arg2 )
{
   return *arg1-*arg2;
}
=========
//ex8b
qsort(bingo, 6, sizeof(int), compare );
//舊的陣列比較,沒有排序
for (i = 0; i< 6; i++)
          {
              for (j = 0; j< 6; j++)
              {
                  if (bingo[i] == a[j])
                       matches++;
              }
          }
//新的陣列比較,有排序
for (i = 0, j=0; i< 6 && j < 6;   )
          {
              if (bingo[i] == a[j])
              {
                  i++; j++; matches++;
              }
              else if (bingo[i] > a[j])
                   j++
              else //if (bingo[i] < a[j])
                   i++;
          }
//ex8a.c 完整
#include <stdio.h>
#include <stdlib.h> 
#include <time.h> 
int compare( const int *arg1, const int *arg2 );
int main()
{
    int i, j, k, N, a[6];
    int ball[49], max, index;
    FILE* fout;
 
    scanf("%d", &N);
    srand((unsigned) time( NULL ) ); //設定亂數的啟始點
    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; //把最重的球重量歸零 
       }
       //對陣列a的6個元素是:a[0], a[1], ....a[5]排序
       qsort(a, 6, sizeof(int), compare);
       printf("%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 compare( const int *arg1, const int *arg2 )
{
   return *arg1-*arg2;
}
//ex8b.c
#include <stdio.h>
int compare( const int *arg1, const int *arg2 );
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]);
    qsort(bingo, 6, sizeof(int), compare );
 
    count[0] = count[1] = count[2] = count[3]= count[4]= count[5] = count[6] =0;
 
    fin = fopen("lotto-sort.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++;
              }
          }*/
          for (i = 0, j=0; i< 6 && j < 6;   )
          {
              if (bingo[i] == a[j])
              {
                  i++; j++; matches++;
              }
              else if (bingo[i] > a[j])
                   j++
              else //if (bingo[i] < a[j])
                   i++; 
          } 
          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;
}
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 星期三, 十二月 24th, 2014 at 09:20:11 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