본문 바로가기
Coding Test/Java Coding Test

[백준알고리즘 / 자바] - 영수증

by nam_ji 2024. 11. 20.

영수증 - 브론즈 4

문제


테스트 (인텔리제이)

    package baekjoon;

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

    public class 영수증 {
    public static void main(String[] args) throws IOException {
        /*
        준원이는 저번 주에 살면서 처음으로 코스트코를 가 봤다.
        정말 멋졌다. 그런데, 몇 개 담지도 않았는데 수상하게 높은 금액이 나오는 것이다!
        준원이는 영수증을 보면서 정확하게 계산된 것이 맞는지 확인해보려 한다.
        영수증에 적힌,
        구매한 각 물건의 가격과 개수
        구매한 물건들의 총 금액
        을 보고, 구매한 물건의 가격과 개수로 계산한 총 금액이 영수증에 적힌 총 금액과 일치하는지 검사해보자.

        입력 설명
        첫째 줄에는 영수증에 적힌 총 금액X가 주어진다.
        둘째 줄에는 영수증에 적힌 구매한 물건의 종류의 수N이 주어진다.
        이후 N개의 줄에는 각 물건의 가격 a와 개수 b가 공백을 사이에 두고 주어진다.

        출력 설명
        구매한 물건의 가격과 개수로 계산한 총 금액이 영수증에 적힌 총 금액과 일치하면 Yes를 출력한다. 일치하지 않는다면 No를 출력한다.

        입출력 예시
        입력                  출력
        260000               Yes
        4
        20000 5
        30000 2
        10000 6
        5000 8

        250000               No
        4
        20000 5
        30000 2
        10000 6
        5000 8
         */

//        solution1();
//        solution2();
        solution3();
    }

//    public static void solution1() {
//        Scanner in = new Scanner(System.in);
//        int x = in.nextInt();
//        int n = in.nextInt();
//        int result = 0;
//        String answer = "";
//
//        for (int i = 0; i < n; i++) {
//            int a = in.nextInt();
//            int b = in.nextInt();
//            result += a * b;
//        }
//        answer = result == x ? "Yes" : "No";
//
//        System.out.println(answer);
//    }
//
//    public static void solution2() throws IOException {
//        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//        StringTokenizer st;
//        int x = Integer.parseInt(br.readLine());
//        int n = Integer.parseInt(br.readLine());
//        int result = 0;
//
//        for (int i = 0; i < n; i++) {
//            st = new StringTokenizer(br.readLine());
//            result += Integer.parseInt(st.nextToken()) * Integer.parseInt(st.nextToken());
//        }
//
//        System.out.println(result == x ? "Yes" : "No");
//    }

    public static void solution3() throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;
        int x = Integer.parseInt(br.readLine());
        int n = Integer.parseInt(br.readLine());

        for (int i = 0; i < n; i++) {
            st = new StringTokenizer(br.readLine());
            x -= Integer.parseInt(st.nextToken()) * Integer.parseInt(st.nextToken());
        }

        System.out.println(x == 0 ? "Yes" : "No");
    }
}

백준알고리즘 코드

import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
//        solution1();
//        solution2();
        solution3();
    }

//    public static void solution1() {
//        Scanner in = new Scanner(System.in);
//        int x = in.nextInt();
//        int n = in.nextInt();
//        int result = 0;
//        String answer = "";
//
//        for (int i = 0; i < n; i++) {
//            int a = in.nextInt();
//            int b = in.nextInt();
//            result += a * b;
//        }
//        answer = result == x ? "Yes" : "No";
//
//        System.out.println(answer);
//    }
//
//    public static void solution2() throws IOException {
//        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//        StringTokenizer st;
//        int x = Integer.parseInt(br.readLine());
//        int n = Integer.parseInt(br.readLine());
//        int result = 0;
//
//        for (int i = 0; i < n; i++) {
//            st = new StringTokenizer(br.readLine());
//            result += Integer.parseInt(st.nextToken()) * Integer.parseInt(st.nextToken());
//        }
//
//        System.out.println(result == x ? "Yes" : "No");
//    }

    public static void solution3() throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;
        int x = Integer.parseInt(br.readLine());
        int n = Integer.parseInt(br.readLine());

        for (int i = 0; i < n; i++) {
            st = new StringTokenizer(br.readLine());
            x -= Integer.parseInt(st.nextToken()) * Integer.parseInt(st.nextToken());
        }

        System.out.println(x == 0 ? "Yes" : "No");
    }
}