[백준/JAVA] 9070번 장보기

정은아·2024년 12월 15일

[알고리즘] 수학 모음

목록 보기
137/152
post-thumbnail

문제

백준 9070번 장보기 JAVA

내 풀이

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

public class Main {

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();

        int testCase = Integer.parseInt(br.readLine());

        for (int i = 0; i < testCase; i++) {
            int num = Integer.parseInt(br.readLine());

            double totalPrice = 0;
            double min = Double.MAX_VALUE;

            int answer = 0;
            for (int j = 0; j < num; j++) {
                StringTokenizer st = new StringTokenizer(br.readLine());

                int gram = Integer.parseInt(st.nextToken());
                int price = Integer.parseInt(st.nextToken());

                totalPrice = (double) price / gram;

                if (min > totalPrice ){
                    min = totalPrice;
                    answer = (int)price;
                }else if (totalPrice == min && price < answer){
                    answer = price;
                }
            }

            sb.append(answer).append("\n");
        }

        System.out.println(sb.toString());
    }
}

느낀점

profile
꾸준함의 가치를 믿는 개발자

0개의 댓글