
풀이1,2 (1은 혼자 풀었고, 2는 그리디 알고리즘을 통해 풀었음)
This file contains hidden or 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
//[JAVA] baekjoon : 5585" 거스름돈 | |
package javaProfessioner; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
public class Main5585 { | |
public static void main(String[] args) throws IOException { | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
String str = br.readLine(); | |
int n = Integer.parseInt(str); | |
int [] enhwa = {500,100,50,10,5,1}; // 엔화 동전의 종류를 배열로 넣어두었음. | |
int money = 1000-n; // 1000엔 지폐를 내고 나머지 받을 돈을 변수에 저장 | |
int coin=0; // 동전의 개수를 셀 변수 | |
int i = 0; // 동전 종류에 접근하기 위한 변수 | |
while(money != 0) // 나머지 받을 돈이 0이 될 때까지 반복 | |
{ | |
coin += money / enhwa[i]; // 동전의 종류 (배열)에 접근하면서 나누어 몫을 저장 | |
money = money % enhwa[i]; // 500엔부터 나누고 남은 나머지를 money변수에 저장 하여 0이 될 때까지 반복함 | |
i++; | |
} | |
System.out.println(coin); // 동전의 최소 개수 출력 | |
} | |
} |
This file contains hidden or 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
//[JAVA] baekjoon : 5585" 거스름돈_2( 그리디 알고리즘 ) | |
package javaProfessioner; | |
import java.util.Scanner; | |
public class Main5585_2 { | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
int [] en = {500,100,50,10,5,1}; // 엔화 배열 선언 | |
int money = 1000 - sc.nextInt(); // 잔돈 | |
int coin = 0; | |
int idx = 0 ; | |
while(money != 0) // 잔돈이 0이 될 때까지 반복 | |
{ | |
int change = money / en[idx]; // 잔돈을 엔화의 종류별로 나누어 변수에 저장 | |
money -= change * en[idx++]; // 동전의 종류로 나누고 난 나머지를 잔돈에 저장 | |
coin += change; // 나눈 몫을 동전의 개수 변수인 coin에 저장 | |
} | |
System.out.println(coin); | |
} | |
} |
'알고리즘 > JAVA' 카테고리의 다른 글
[JAVA] 백준 : 1157" 단어 공부 (0) | 2019.08.16 |
---|---|
[JAVA] 백준 : 10798" 세로읽기 (0) | 2019.08.15 |
[JAVA] 백준 : 10773" 제로 (0) | 2019.07.31 |
[JAVA] 백준 : 2439" 별찍기-2 (0) | 2019.07.30 |
[JAVA] 백준 : 10818" 최소, 최대 (0) | 2019.07.30 |