백준 Mathematics

KIMYEONGJUN·2025년 4월 20일
0
post-thumbnail

문제

내가 생각했을때 문제에서 원하는부분

The first line will contain a single integer n that indicates the number of integers to add together.
The next n lines will each contain one integer.
Your task is to write a program that adds all of the integers together.

Output the resulting integer.
The output should be one line containing one integer value.

내가 이 문제를 보고 생각해본 부분

BufferedReader로 입력을 받고 Integer.parseInt()로 정수 변환한다.
합의 범위를 고려해 long 타입을 사용한다.
입력받은 숫자들을 모두 더한 뒤 한 줄에 결과를 출력한다.

코드로 구현

package baekjoon.baekjoon_27;

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

// 백준 26545번 문제
public class Main997 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int n = Integer.parseInt(br.readLine());
        long sum = 0;  // 합이 커질 수 있으므로 long 타입 사용

        for(int i = 0; i < n; i++) {
            sum += Integer.parseInt(br.readLine());
        }

        System.out.println(sum);
        br.close();
    }
}

마무리

코드와 설명이 부족할수 있습니다. 코드를 보시고 문제가 있거나 코드 개선이 필요한 부분이 있다면 댓글로 말해주시면 감사한 마음으로 참고해 코드를 수정 하겠습니다.

profile
Junior backend developer

0개의 댓글