반응형
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);
}
구름 goormedu COS PRO 2급 기출문제 - C언어
반응형
'코딩 테스트 > [c언어] cos pro 2급 기출문제' 카테고리의 다른 글
[No.42] 공강시간 구하기 / cos pro 2급 c언어 기출 문제 (0) | 2021.05.23 |
---|---|
[No.41] 사다리 게임의 승자를 구해주세요!/ cos pro 2급 c언어 기출 문제 (0) | 2021.05.23 |
[No. 38] / cos pro 2급 c언어 기출 문제 (0) | 2021.05.21 |
[No. 37] 오른 점수와 떨어진 점수 구하기 / cos pro 2급 c언어 기출 문제 (0) | 2021.05.21 |
[No. 36] 여러분이 열심히 모은 point, 돌려 드립니다. / cos pro 2급 c언어 기출 문제 (0) | 2021.05.20 |
댓글