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

[No.40] XX시험 합격자 수 구하기 / cos pro 2급 c언어 기출 문제

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

XX시험 합격자 수 구하기


문제 설명

XX 시험을 친 수험생들의 점수가 주어질 때, 합격자 수를 구하려 합니다. 시험에 합격하기 위해서는 커트라인 이상의 점수를 받아야 합니다. 

 

수험생들의 시험 점수가 들어있는 배열 scores, scores의 길이 scores_len, 커트라인 점수 cutline이 매개변수로 주어질 때, 합격자 수를 return 하도록 solution함수를 완성하세요. 

 


예시

 

scores scores_len cutline return   
[80, 90, 55, 60, 59] 5 60 3

코드 해석 및 전체 코드

 

answer ) 합격자 수를 return 할 변수 

for ) 0 ~ scores_len - 1까지 반복

if ) scores[i]가 cutline과 같거나 클 경우 answer 1 증가 

 

i scores[i] >= cutline answer
0 80 >= 60 answer++
1 90 >= 60 answer++
2 55 < 60  
3 60 >= 60 answer++
4 59 < 60  

합격자 수는 총 3명이다. 

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
int solution(int scores[], int scores_len, int cutline) {
    int answer = 0;
    for (int i = 0; i < scores_len; i++) {
        if (scores[i] >= cutline) answer++;
    }
    return answer;
}
int main() {
    int scores[5] = { 80, 90, 55, 60, 59 };
    int scores_len = 5;
    int cutline = 60;
    int ret = solution(scores, scores_len, cutline);

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

cos pro 2급 기출문제

github

 

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

 

 

반응형

댓글