[코테]/[GroomEdu]
[COS PRO 2급] 3차 5번_여행객의 총 교통비 구하기 (C/C++)
Sky_
2021. 5. 15. 16:00
주어진 코드
#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 = //빈칸
child_expense = //빈칸
}
int total_expenses = 0;
for (int i = 0; i < member_age_len; i++) {
if ( >= 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);
}
완성 코드
#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.9;
child_expense = child_expense * 0.8;
}
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);
}
결과
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 |
코드 분석
단계 | 과정 |
main | 각 인원의 나이가 담긴 배열 member_age과 배열의 길이 member_age_len을 선언한다 교통수단 Bus,Ship,Airplane 중 하나를 transportations에 저장한다. solution함수를 호출한다. |
solution | 성인일때의 요금 adult_expense와 아이일때의 요금 child_expense를 선언한다. |
solution - if | if문을 사용하여 일치하는 교통수단일때에 각 성인/아이 요금을 할당한다. |
solution - if | if문을 사용하여 총 인원수가 10명이 넘었을 때, 성인/아이 할인율을 적용한다. |
solution - for | for문을 총 인원수만큼 반복하여 차례대로 각 여행객이 성인인지, 아이인지 판별하고 해당하는 요금을 총 금액 변수 total_expenses에 더한다. |
solution | 최종 금액 total_expenses를 return한다. |
main | 결과를 출력한 후 프로그램을 종료한다. |
goorm
구름은 클라우드 기술을 이용하여 누구나 코딩을 배우고, 실력을 평가하고, 소프트웨어를 개발할 수 있는 클라우드 소프트웨어 생태계입니다.
www.goorm.io