Archive for the ‘100(上)程式設計(一)’ Category

26
十二月

程式設計一程式 12/26: 小考2

   Posted by: admin Tags: , ,

//-----------------------------------------quiz-2a.c
#include <stdio.h>
int main(int argc, char *argv[])
{
    int num1, num2, q, r;
	//讀入二個整數,並計算乘商及餘數
	  printf("Input two numbers:");
    scanf("%d %d", &num1, & num2);
    q = num1 / num2;
    r = num1 % num2;
    printf("%d / %d = %d\n", num1, num2, q); 
    printf("%d / %d...%d\n", num1, num2, r); 
    return 0;
}
//-----------------------------------------quiz-2b.c
#include <stdio.h>
int compare( const void* arg1, const void* arg2 );
int main()
{
   int i, ret, score1, score2;
   double avg[100];
   int num=0;
   FILE * stream;
   char filename[20]="score.txt";
   stream  = fopen( filename, "r" );
 
   ret = fscanf(stream, "%d %d", &score1, &score2);
   while (ret !=  EOF)
   {
         avg[num++] = (double)(score1 + score2)/2.0;
         ret = fscanf(stream, "%d", &score1, &score2);
   }
   fclose(stream);
 
   qsort(avg, num, sizeof(double), compare);//排序
   for (i=0; i< num; i++)
        printf("%7.2f\n", avg[i]);
 
   return 0;
}
int compare( const void* arg1, const void* arg2 )
{
    int n1 = *((double*) arg1);
    int n2 = *((double*) arg2);
    return n2-n1;
}

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

#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