[JW0004-3],[JW0004-4] 퀴즈-1(9-10장) 문제, 답

Post Reply
sungbum
Posts: 12
Joined: Sun Jan 04, 2015 10:06 pm

[JW0004-3],[JW0004-4] 퀴즈-1(9-10장) 문제, 답

Post by sungbum »

문제1: 미니 정렬 프로그램
1-1.PNG
1-1.PNG (113.82 KiB) Viewed 5329 times
답안

Code: Select all

  1 #include<stdio.h>
  2
  3 void swap(double *pa, double *pb);
  4 void sort(double *pmax, double *pmid, double *pmin);
  5
  6 int main(void)
  7 {
  8     double max, mid, min;
  9
 10     printf("input :");
 11     scanf("%lf%lf%lf", &max, &mid, &min);
 12
 13     sort(&max, &mid, &min);
 14
 15     printf("sorted result: %.1lf, %.1lf, %.1lf\n", max, mid, min);
 16
 17     return (0);
 18 }
 19
 20 void swap(double *pa, double *pb)
 21 {
 22     double temp;
 23     temp = *pa;
 24     *pa = *pb;
 25     *pb = temp;
 26 }
 27
 28 void sort(double *pmax, double *pmid, double *pmin)
 29 {
 30     if(*pmax < *pmid)   {
 31         swap(pmax, pmid);
 32     }
 33
 34     if(*pmax < *pmin)   {
 35         swap(pmax, pmin);
 36     }
 37
 38     if(*pmid < *pmin)   {
 39         swap(pmid, pmin);
 40     }
 41 }

문제2: 로또 번호 확인 프로그램
1-2.PNG
1-2.PNG (111.46 KiB) Viewed 5326 times
답안

Code: Select all

  1 #include<stdio.h>
  2
  3 void check_num(int *lotto, int *input);
  4
  5 int main(void)
  6 {
  7     int lotto_num[6] = {1,2,3,10,20,30};
  8     int input[6];
  9
 10     printf("숫자 6개를 입력하세요 : ");
 11     scanf("%d%d%d%d%d%d",&input[0],&input[1],&input[2],&input[3],&input[4],&input[5]);
 12     check_num(lotto_num, input);
 13     return (0);
 14 }
 15
 16 void check_num(int *lotto, int *input)
 17 {
 18     int cnt = 0, i, j;
 19
 20     for(i=0; i<6; i++)
 21         for(j=0;j<i+1;j++)
 22         {
 23             if(lotto[j] == input[i])
 24             {
 25                 cnt++;
 26             }
 27         }
 28     printf("일치하는 번호의 개수: %d 개 \n", cnt);
 29 }

Post Reply