반응형
250x250
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- LeetCode 83번
- 사칙연산
- 프로그래머스
- 이분 탐색
- 자료 구조
- 시뮬레이션
- 유클리드 호제법
- KMP알고리즘
- 조합론
- 큐
- Queue
- 스택
- 해시를 사용한 집합과 맵
- 연결리스트 정렬
- 별 찍기
- 수학
- 정수론
- 문자열
- 재귀
- 큰 수 연산
- 실패함수
- LeetCode Remove Duplicates from Sorted List in c
- 연결리스트 중복제거
- 브루트포스 알고리즘
- LeetCode 83 c언어
- 임의 정밀도 / 큰 수 연산
- 다이나믹 프로그래밍
- 문자열제곱
- 정렬
- 구현
Archives
- Today
- Total
hahn
[백준 - JAVA] 거스름돈 본문
728x90
반응형
14916번: 거스름돈
첫째 줄에 거스름돈 액수 n(1 ≤ n ≤ 100,000)이 주어진다.
www.acmicpc.net
http://boj.kr/eeb59436bade4a7c806927d5f1f5ba95
공유 소스 보기
www.acmicpc.net
더보기
import java.util.Scanner;
class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int input, result = 0;
input = sc.nextInt();
while(input != 0) {
if(input < 2) {
result = -1;
break;
}
if((input - 5) % 2 == 0 || input / 5 >= 2) {
input -= 5;
result++;
}else {
input -= 2;
result++;
}
}
System.out.println(result);
}
}
일단 1원이 되면 거슬러줄 수 없으니 -1 반환
9원 이하 일 때,
5원을 거슬러주고 2로 나누어 떨어져 지지 않으면
1원이 남게되므로 위 조건 추가
if((input - 5) % 2 == 0 || input / 5 >= 2)
실수로 >=를 >로 써서 틀렸었다..
728x90
반응형