여행객의 총 교통비 구하기
문제 설명
여행객들의 총 교통비를 계산하려고 합니다. 교통편은 "Bus", "Ship", "Airplane" 총 3가지입니다.
나이가 20살 이상이면 어른 요금을 그렇지 않으면 어린이 요금을 받습니다.
각 교통편별 가격은 다음과 같습니다.
어른 | 어린이 | |
Bus | 40,000원 | 15,000월 |
Ship | 30,000월 | 13,000월 |
Airplane | 70,000원 | 45,000원 |
여행객들이 10명 이상인 경우 연령에 따라 할인을 받습니다.
어른 | 어린이 |
10% | 20% |
여행객들의 나이를 담고 있는 배열 member_age와 member_age의 길이 member_age_len, 교통편인 transportation이 매개변수로 주어질 때, 총 교통비를 return 하도록 solution 함수를 작성하세요.
예시
member_age | member_age_len | transportation | return |
[13, 33, 45, 11, 20] | 5 | "Bus" | 150000 |
[25, 11, 27, 56, 7, 19, 52, 31, 77, 8] | 10 | "Ship" | 203600 |
코드 해석 및 전체 코드
adult_expense ) 어른 교통비
child_expense ) 어린이 교통비
if문 1 ) transportation이 Bus일 경우 어른, 어린이 교통비를 대입한다.
else-if 1 in if 1 ) transportation이 Ship일 경우 어른, 어린이 교통비를 대입한다.
else-if 2 in if 1 ) transportation이 Airplane일 경우 어른, 어린이 교통비를 대입한다.
if문 2 ) 인원이 10명 이상인지 확인하는 조건문으로, 10명 이상일 경우 할인을 적용해야한다.
adult_expense -= adult_expense * 0.1 ) adult_expense에 adult_expense의 0.1(=10%)를 곱한 값을 뺀다.
child_expense -= child_expense * 0.2 ) child_expense에 child_expense 의 0.2(20%)를 곱한 값을 뺀다.
total_expeness ) 총 가격을 return할 변수
for문 ) 0 ~ member_age_len - 1까지 반복한다.
if in for ) 나이가 20살 이상일 경우 total_expeness에 adult_expense를 더한다.
예시 2
if 1
transportation = "Ship"으로 adult_expense = 30000, child_expense = 13000이다.
if 2
member_age_len이 10 이므로 if문 조건을 만족한다.
adult_expense | adult_expense * 0.1 | adult_expense -= adult_expense * 0.1 |
30000 | 3000 | 27000 |
child_expense | child_expense * 0.2 | child_expense -= adult_expense * 0.1 |
13000 | 2600 | 10400 |
adult_expense와 child_expense의 할인 가격은 각각 27,000원, 10,400원이다.
i | if(O) - else(X) | total_expenses |
25 | O | 0 + 27000 = 27000 |
11 | X | 27000 + 10400 = 37400 |
27 | O | 37400 + 27000 = 64400 |
56 | O | 64400 + 27000 = 91400 |
7 | X | 91400 + 10400 = 101800 |
19 | X | 101800 + 10400 = 112200 |
52 | O | 112200 + 27000 = 139200 |
31 | O | 139200 + 27000 = 166200 |
77 | O | 166200 + 27000 = 193200 |
8 | X | 193200 + 10400 = 203600 |
교통액 총액은 203,600원이다.
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
int solution(int member_age[], int member_age_len, char* transportation) {
int adult_expense = 0;
int child_expense = 0;
if (!strcmp(transportation, "Bus")) {
adult_expense = 40000;
child_expense = 15000;
}
else if (!strcmp(transportation, "Ship")) {
adult_expense = 30000;
child_expense = 13000;
}
else if (!strcmp(transportation, "Airplane")) {
adult_expense = 70000;
child_expense = 45000;
}
if (member_age_len >= 10) {
adult_expense -= adult_expense * 0.1;
child_expense -= child_expense * 0.2;
}
int total_expenses = 0;
for (int i = 0; i < member_age_len; i++) {
if (member_age[i] >= 20)
total_expenses += adult_expense;
else
total_expenses += child_expense;
}
return total_expenses;
}
int main() {
int member_age1[5] = { 13, 33, 45, 11, 20 };
int member_age1_len = 5;
char* transportations1 = "Bus";
int ret1 = solution(member_age1, member_age1_len, transportations1);
printf("solution 함수의 반환 값은 %d 입니다.\n", ret1);
int member_age2[10] = { 25, 11, 27, 56, 7, 19, 52, 31, 77, 8 };
int member_age2_len = 10;
char* transportations2 = "Ship";
int ret2 = solution(member_age2, member_age2_len, transportations2);
printf("solution 함수의 반환 값은 %d 입니다.\n", ret2);
}
구름 goormedu COS PRO 2급 기출문제[3차]
문제5) 여행객의 총 교통비 구하기 - C언어
'코딩 테스트 > [c언어] cos pro 2급 기출문제' 카테고리의 다른 글
[No.27] 남은 재료로 주스 만들기 / cos pro 2급 c언어 기출 문제 (0) | 2021.05.16 |
---|---|
[No.26] 타일 색칠 방법 구하기 / cos pro 2급 c언어 기출 문제 (2) | 2021.05.15 |
[No.24] 단어의 오타 수정하기 / cos pro 2급 c언어 기출 문제 (0) | 2021.05.14 |
[No.23] 체조 선수의 점수 구해주기 / cos pro 2급 c언어 기출 문제 (0) | 2021.05.14 |
[No.22] 장학생 수 구하기 / cos pro 2급 c언어 기출 문제 (0) | 2021.05.13 |
댓글