티스토리 뷰
주어진 코드
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
int solution(int num_apple, int num_carrot, int k) {
int answer = 0;
if(num_apple < 3 * num_carrot)
answer = num_apple / 3;
else
answer = num_carrot;
num_apple -= 3 * answer;
num_carrot -= answer;
for(int i = 0; k - (num_apple + num_carrot + i) > 0; i++)
if(i % 4 == 0)
answer++;
return answer;
}
int main() {
int num_apple1 = 5;
int num_carrot1 = 1;
int k1 = 2;
int ret1 = solution(num_apple1, num_carrot1, k1);
printf("solution 함수의 반환 값은 %d 입니다.\n", ret1);
int num_apple2 = 10;
int num_carrot2 = 5;
int k2 = 4;
int ret2 = solution(num_apple2, num_carrot2, k2);
printf("solution 함수의 반환 값은 %d 입니다.\n", ret2);
}
완성 코드
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
int solution(int num_apple, int num_carrot, int k) {
int answer = 0;
if(num_apple < 3 * num_carrot)
answer = num_apple / 3;
else
answer = num_carrot;
num_apple -= 3 * answer;
num_carrot -= answer;
for(int i = 0; k - (num_apple + num_carrot + i) > 0; i++)
if(i % 4 == 0)
answer--;
return answer;
}
int main() {
int num_apple1 = 5;
int num_carrot1 = 1;
int k1 = 2;
int ret1 = solution(num_apple1, num_carrot1, k1);
printf("solution 함수의 반환 값은 %d 입니다.\n", ret1);
int num_apple2 = 10;
int num_carrot2 = 5;
int k2 = 4;
int ret2 = solution(num_apple2, num_carrot2, k2);
printf("solution 함수의 반환 값은 %d 입니다.\n", ret2);
}
결과
num_apple | num_carrot_len | k | return | |
예시 #1 | 5 | 1 | 2 | 1 |
예시 #2 | 10 | 5 | 4 | 2 |
'[코테] > [GroomEdu]' 카테고리의 다른 글
[COS PRO 2급] 3차 9번_주차장에 몇대가 들어올수 있는거야? (C/C++) (0) | 2021.05.17 |
---|---|
[COS PRO 2급] 3차 8번_TV 애청자 A씨 (C/C++) (0) | 2021.05.16 |
[COS PRO 2급] 3차 6번_타일 색칠 방법 구하기 (C/C++) (0) | 2021.05.15 |
[COS PRO 2급] 3차 5번_여행객의 총 교통비 구하기 (C/C++) (0) | 2021.05.15 |
[COS PRO 2급] 3차 3번_체조 선수의 점수 구해주기 (C/C++) (0) | 2021.05.14 |
댓글
공지사항
최근에 올라온 글
TAG
- COSPRO 2급
- 코딩테스트
- 1급
- 연습문제
- CosPro
- 구름 기출문제
- Cos Pro
- 자바
- cospro기출문제
- c언어
- lv2
- 알고리즘
- cospro기출
- 기출문제
- YBM기출
- 구름에듀
- 배열활용문제
- Java
- C++
- groom
- c언어 기출문제
- 코스프로
- YBM
- C
- 구름에듀 기출문제
- programmers
- 프로그래머스
- lv1
- 배열
- groomedu
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함