Don't Look Back In Anger

백준 2355번. 시그마 본문

IT/백준

백준 2355번. 시그마

버로나 2022. 4. 26. 20:24

백준 2355번. 시그마

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

public class Main {

	public static void main(String[] args) throws IOException {
    
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		
		//연산결과가 int의 범위를 벗어나므로 long형 사용
		long n = Long.parseLong(st.nextToken());
		long m = Long.parseLong(st.nextToken());
		
		//작은 수를 a, 큰 수를 b에 저장
		long a = Math.min(n, m);
		long b = Math.max(n, m);
		
		//시그마 합 공식 : n(n+1)/2
		//a부터 b까지의 합이므로 1~b까지의 합에서 1~(a-1)까지 빼줘야 함
		long sum = (b*(b+1)-a*(a-1))/2;
		System.out.print(sum);
	}
}

'IT > 백준' 카테고리의 다른 글

백준 15552번. 빠른 A+B  (0) 2022.05.02
백준 8958번. OX퀴즈  (0) 2022.05.01