티스토리 뷰
주어진 코드
#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
'[코테] > [GroomEdu]' 카테고리의 다른 글
[COS PRO 2급] 3차 7번_남은 재료로 주스 만들기 (C/C++) (0) | 2021.05.16 |
---|---|
[COS PRO 2급] 3차 6번_타일 색칠 방법 구하기 (C/C++) (0) | 2021.05.15 |
[COS PRO 2급] 3차 3번_체조 선수의 점수 구해주기 (C/C++) (0) | 2021.05.14 |
[COS PRO 2급] 3차 4번_단어의 오타 수정하기 (C/C++) (0) | 2021.05.14 |
[COS PRO 2급] 3차 2번_장학생 수 구하기 (C/C++) (0) | 2021.05.13 |
댓글
공지사항
최근에 올라온 글
TAG
- 배열활용문제
- lv1
- 구름에듀 기출문제
- c언어
- lv2
- 알고리즘
- programmers
- COSPRO 2급
- 1급
- groomedu
- YBM기출
- C++
- CosPro
- 코딩테스트
- 구름에듀
- Cos Pro
- cospro기출
- 연습문제
- 구름 기출문제
- c언어 기출문제
- C
- 프로그래머스
- cospro기출문제
- 배열
- groom
- YBM
- Java
- 코스프로
- 자바
- 기출문제
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
글 보관함