Java/Java 알고리즘

백준 25304번 영수증 JAVA 구현해보기

kimc 2022. 6. 27. 04:24

```

백준 25304번 영수증 JAVA 구현해보기

```

이번 글을 통해 배워갈 내용

  1. 백준 25304번 풀이

https://www.acmicpc.net/problem/25304

 

25304번: 영수증

준원이는 저번 주에 살면서 처음으로 코스트코를 가 봤다. 정말 멋졌다. 그런데, 몇 개 담지도 않았는데 수상하게 높은 금액이 나오는 것이다! 준원이는 영수증을 보면서 정확하게 계산된 것

www.acmicpc.net

 

백준 25304번 영수증은 난이도 브론즈 등급의 문제로서

 

구매한 각 물건의 가격과 개수

그리고

구매한 물건들의 총액이 주어지면

구매한 물건들의 가격과 개수의 곱이 총액과 같은지 판별해주면 되는 문제입니다.

 


30분 정도 위에 링크를 방문하셔서 풀어보시고

안 풀리시는 경우에만 아래 해답을 봐주시면 감사하겠습니다.


입력을 받으면서 리스트를 안 쓰고

풀 수도 있지만

입력과 출력을 나누고 싶고 정보를 보존해서 재사용하고자 하는 마음에 리스트를 사용하였고

 

스트림을 활용해서 합을 구해서 비교했습니다.

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) throws IOException {
        // 입력
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        final int totalPrice = Integer.parseInt(br.readLine());
        final int productCount = Integer.parseInt(br.readLine());
        List<Product> products = new ArrayList<>();
        for (int i = 0; i < productCount; i++) {
            String[] input = br.readLine().split(" ");
            int price = Integer.parseInt(input[0]);
            int count = Integer.parseInt(input[1]);
            products.add(new Product(price, count));
        }

        //출력
        System.out.print(isTotalPriceMatchProducts(totalPrice, products) ? "Yes" : "No");

    }

    private static boolean isTotalPriceMatchProducts(int totalPrice, List<Product> products) {
        return totalPrice == products.stream().mapToInt(p -> p.count * p.product).sum();
    }

    private static class Product {
        private final int product;
        private final int count;

        public Product(int product, int count) {
            this.product = product;
            this.count = count;
        }
    }
}

 

 

읽어주셔서 감사합니다

 

무엇인가 얻어가셨기를 바라며

 

오늘도 즐거운 코딩 하시길 바랍니다 ~ :)

 


 

728x90