Notice
Recent Posts
Recent Comments
Link
«   2025/02   »
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
more
Archives
Today
Total
관리 메뉴

forDevLife

[백준] 11047 - 동전0 (자바) 본문

알고리즘

[백준] 11047 - 동전0 (자바)

JH_Lucid 2021. 4. 30. 14:02

 

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

public class Main {
    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String[] arr = br.readLine().split(" ");
        int N = Integer.parseInt(arr[0]);
        int K = Integer.parseInt(arr[1]);
        int[] coin = new int[N];
        int count = 0;
        for(int i=0; i<N; i++) {
            coin[i] = Integer.parseInt(br.readLine());
        }
        for(int i=N-1; i >=0; i--) {
            if(coin[i] <= K) {
                count += K/coin[i];
                K %= coin[i];
            }
        }
        System.out.print(count);
    }
}

- scanner 보다 bufferedreader가 훨씬 메모리 / 속도 면에서 빠르다.

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine(), " ");

        int N = Integer.parseInt(st.nextToken());
        int K = Integer.parseInt(st.nextToken());
        int[] coin = new int[N];
        int count = 0;
        for(int i=0; i<N; i++) {
            coin[i] = Integer.parseInt(br.readLine());
        }
        for(int i=N-1; i >=0; i--) {
            if(coin[i] <= K) {
                count += K/coin[i];
                K %= coin[i];
            }
        }
        System.out.print(count);
    }
}

- StringTokenizer를 사용하여, String[] 없이도 사용 가능하다.

Comments