elesis's haunt

8393 합 본문

 

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        sc.close();
        
        int num = 0;
        int sum = 0;
        
        for(int i=0; i<n; i++) {
            num = i + 1;
            sum += num;
        }
        System.out.print(sum);
    }
}

 

* BufferReader를 사용한 방법

+ Integer.valueOf + sum += n--

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

public class Main
{
	public static void main(String[] args) throws IOException
	{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.valueOf(br.readLine());
		// System.out.println(IntStream.rangeClosed(1, n).sum());
		int sum = 0;
		while (n > 0)
			sum += n--;
		System.out.println(sum);
	}
}

'백준~문풀 후 최적화 추가~ > 단계별로 풀어보기' 카테고리의 다른 글

2741 N찍기  (0) 2021.09.10
15552 빠른 A+B  (0) 2021.09.10
10950 A+B-3  (0) 2021.04.19
2739 구구단  (0) 2021.04.19
2884 알람 시계  (0) 2021.04.15
Comments