자바 백준 5585번 거스름돈
500엔부터 1엔까지 가장 적게 잔돈을 줄 수 있는 방법은 500엔부터 비교하며 계산하는 것이다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class Main { | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
int pay_money = sc.nextInt(); | |
int give_money = 1000-pay_money; | |
int number=0; | |
int flag=0; | |
Integer[] a = {500,100,50,10,5,1}; | |
while(true) { | |
if(give_money==0) { | |
break; | |
} | |
int sub = give_money/a[flag]; | |
if(sub!=0) { | |
number+=sub; | |
give_money-= sub*a[flag]; | |
} | |
flag++; | |
} | |
System.out.println(number); | |
} | |
} |
'백준 알고리즘 > JAVA' 카테고리의 다른 글
백준 4673번 셀프 넘버 (0) | 2019.01.21 |
---|---|
자바 백준 1764번 듣보잡 (1) | 2018.09.02 |
자바 백준 7568번 덩치 (1) | 2018.08.25 |
자바 백준 1065번 한수 (0) | 2018.08.23 |
자바 백준 2231번 분해합 (0) | 2018.08.23 |