본문 바로가기
코딩 테스트/[c언어] cos pro 2급 기출문제

[No.32] 시험 합격자가 몇 명이지? / cos pro 2급 c언어 기출 문제

by M개발자 2021. 5. 18.
반응형

시험 합격자가 몇 명이지? 

문제 설명

 

체력시험 합격 인원을 알아보려고 합니다.

윗몸일으키기 팔굽혀펴기 달리기
80점 이상 80점 이상 70점 이상

 

- 통과한 종목이 하나 이하거나, 통과 점수의 반을 넘기지 못한 종목이 있다면 불합격입니다.

- 그 외에는 합격입니다.

 

각 종목 기록을 담고 있는 배열 scores, scores의 행 길이 scores_len이 매개변수로 주어질 때, 시험에 합격한 인원을 return하도록 solution 함수를 작성하려고 합니다.

위 구조를 참고하여 코드가 올바르게 동작할 수 있도록 빈칸에 주어진 func_a, func_b, func_c 함수와 매개변수를 알맞게 채워주세요.

 


예시

 

  scores scores_len return
예시 1 [[30, 40, 100], [97, 88, 95]] 2 1
예시 2 {90, 88, 70}, {85, 90, 90}, {100, 100, 70}, {30, 90, 80}, {40, 10, 20}, {83, 88, 80}} 6 4

코드 해석 및 전체 코드

int solution(int scores[][3], int scores_len) {
    int answer = 0;
    for(int i = 0; i<scores_len; i++) {
        int passed = func_c(scores[i]);
        int non_passed = func_b(scores[i]);
        answer += func_a(passed, non_passed);
    }
    return answer;
}

solution 함수의 흐름대로 함수 설명

 

예시는 예시 1의 배열 [0]으로 설명

func_c통과한 종목이 몇 개인지 세는 함수이다. 

한 사람의 점수들을 넘겨야 하므로 매개변수를 scores[i]로 넘겨야한다. 

 

answer ) 통과한 종목을 return할 변수

 

if 1 ) scores[0]은 윗몸일으키기의 점수로 80점 이상이면 통과여서 answer를 1 증가한다.

if 2 ) scores[1]은 팔굽혀펴기의 점수로 80점 이상이면 통과여서 answer를 1 증가한다.

if 3 ) scores[2]은 달리기의 점수로 70점 이상이면 통과여서 answer를 1 증가한다.

  if 1 if 2 if 3
scores[0,1,2] 30 40 100
answer     answer++

if 3에서만 조건을 만족하므로 통과한 종목은 1개이다. 

 

func_b통과 점수의 반을 넘기지 못한 종목이 몇 개인지 세는 함수이다. 

 

answer ) 통과 점수의 반을 넘기지 못한 종목을 return할 변수

 

if 1 ) scores[0]은 윗몸일으키기의 점수로 40점 미만이면 answer를 1 증가한다.

if 2 ) scores[1]은 팔굽혀펴기의 점수로 40점 미만이면 answer를 1 증가한다.

if 3 ) scores[2]은 달리기의 점수로 35점 이하이면 answer를 1 증가한다.

  if 1 if 2 if 3
scores[0,1,2] 30 40 100
answer answer++    

if 1에서만 조건을 만족하므로 통과 점수의 반을 넘지 못한 종목은 1개이다. 

 

func_a합격한 인원을 세는 함수이다. 

합격 조건은 통과한 종목이 하나보다 많고 통과 점수의 반을 넘기지 못한 종목이 없는 것이다. 

매개변수로는 func_c인 passed와 func_b인 non_passed를 받는다.

 

passed가 1보다 크고, non_passed가 0이면 return 한다. 

passed non_passed
1 1

passed는 만족하지만 non_passed는 만족하지 않아 return값은 0이다. 

만족할 경우 1을 return 한다. 

 

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

int func_a(int passed, int non_passed) {
    return (passed > 1 && non_passed == 0);
}

int func_b(int scores[3]) {
    int answer = 0;
    if (scores[0] < 40) {
        answer++;
    }
    if (scores[1] < 44) {
        answer++;
    }
    if (scores[2] < 35) {
        answer++;
    }
    return answer;
}

int func_c(int scores[3]) {
    int answer = 0;
    if (scores[0] >= 80) {
        answer++;
    }
    if (scores[1] >= 88) {
        answer++;
    }
    if (scores[2] >= 70) {
        answer++;
    }
    return answer;
}
int solution(int scores[][3], int scores_len) {
    int answer = 0;
    for (int i = 0; i < scores_len; i++) {
        int passed = func_c(scores[i]);
        int non_passed = func_b(scores[i]);
        answer += func_a(passed, non_passed);
    }
    return answer;
}
int main() {
    int scores1[2][3] = {
        {30, 40, 100},
        {97, 88, 95}
    };
    int ret1 = solution(scores1, 2);

    printf("solution 함수의 반환 값은 %d 입니다.\n", ret1);

    int scores2[6][3] = {
        {90, 88, 70},
        {85, 90, 90},
        {100, 100, 70},
        {30, 90, 80},
        {40, 10, 20},
        {83, 88, 80}
    };
    int ret2 = solution(scores2, 6);

    printf("solution 함수의 반환 값은 %d 입니다.\n", ret2);
}

cos pro 2급 기출문제

github

 

구름 goormedu COS PRO 2급 기출문제 - C언어

[4차] 문제2) 시험 합격자가 몇명이지?

반응형

댓글