본문 바로가기
알고리즘 풀이

[백준] 2720번: 세탁소 사장 동혁 - java 풀이

by 코디드 2023. 6. 28.

내 풀이

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		int t = Integer.parseInt(br.readLine());
		
		int q = 25;
		int d = 10;
		int n = 5;
		int p =1;
		
		StringBuilder sb = new StringBuilder();
		
		for(int i=0; i<t; i++) {
			int c = Integer.parseInt(br.readLine());
			
			int qCnt = c/q;
			int dCnt = c%q/d;
			int nCnt = c%q%d/n;
			int pCnt = c%q%d%n/p;
			
			sb.append(qCnt).append(" ");
			sb.append(dCnt).append(" ");
			sb.append(nCnt).append(" ");
			sb.append(pCnt).append("\n");
			
		}
		System.out.println(sb);
	}
}

 

이 풀이도 맞는 방법이다.

 

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		int t = Integer.parseInt(br.readLine());
		
		int q = 25;
		int d = 10;
		int n = 5;
		int p =1;
		
		StringBuilder sb = new StringBuilder();
		
		for(int i=0; i<t; i++) {
			int c = Integer.parseInt(br.readLine());
			
			int qCnt = c/q;
			sb.append(qCnt).append(" ");
			
			c %= q; 
			
			int dCnt = c/d;
			sb.append(dCnt).append(" ");
			
			c %= d;
			
			int nCnt = c/n;
			sb.append(nCnt).append(" ");
			
			c %= n;
			
			int pCnt = c/p;
			sb.append(pCnt).append("\n");
			
		}
		System.out.println(sb);
	}
}

 

다른 풀이를 참고해보니 쿼터(q), 다임(d), 니켈(n), 페니(p)로 나눌때마다

c %= q,  c %= d, c %= n, c %= p 로 초기화줘서 더 깔끔하게 표현할 수 있었다.