[Java | 백준 5585번] 거스름돈
· 테크· Java
Java
백준 5585번 www.acmicpc.net/problem/5585
풀이
while, if 문을 이용한 풀이보다 간단한 계산이 될 수 있도록 한 줄에 나열해보았다. 연산자 / 와 % 를 사용해서 사용된 동전의 수를 구했다.
여기서 그리디 알고리즘과 동일하게 적은 동전수가 필요하므로 가장 큰 수 인 500 으로 나누고 그 몫을 동전 수에 더한다. 나머지 값은 다시 100 원의 동전의 개수를 구하는데 사용한다. 이런식으로 나열된 것이 아래 cnt 이다.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
import java.util.Scanner;
public class codinginteview_5585 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int returnMoney = sc.nextInt();
sc.nextLine();
int cnt = 0;
int payMoney = 1000-returnMoney;
cnt = payMoney/500+(payMoney%500/100)+(payMoney%500%100/50)+(payMoney%500%100%50/10)
+(payMoney%500%100%50%10/5)+(payMoney%500%100%50%10%5/1);
System.out.println(cnt);
sc.close();
}
}
|
cs |
Comments
No comments yet. Be the first!
319 posts in 테크
- 368Supabase 프로젝트 복사하기 (Restore to a New Project)NEW
- 341Migrating from Permanent Access Tokens to Token Exchange — Why Order Matters
- 326Startup & Product Glossary: Terms Every Solo Founder Should Know
- 325Context Management — How I Do It Now
- 324Claude Code Routines vs Cowork Schedule — What's the Difference?
1–5 / 319