본문 바로가기

JAVA-BAEKJOON/3단계 반복문

[백준/03-04] 25304 영수증

NO.25304

 

풀이코드

import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int x = sc.nextInt();
        int n = sc.nextInt();
        int total = 0;
        
        for(int i=0; i<n; i++){
            int a = sc.nextInt();
            int b = sc.nextInt();

            total += a*b;
        }
        
        sc.close();
        
        if(x==total){
            System.out.println("Yes");
        }
        else{
            System.out.println("No");
        }
    }
}

 

📌 구매한 품목의 가격과 수량을 곱해서 더하는 total 값을 만들어 x와 비교시켰다.