본문 바로가기
Coding Test/백준

[백준 8393번 - java] 합

by olli2 2021. 11. 3.

https://www.acmicpc.net/submit/8393/35033117

 

내 답안 (오답)

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

public class Main {
	public static void main(String[] args) throws IOException {
		
		int i = 0;
		int sum = 0;
		
		Scanner s = new Scanner(System.in);
		int n = s.nextInt();
		
		for (i=1; i<=n; i++) {
			sum += i;
			System.out.printf("%d\n",sum);
		}
	}
}

 

실수로 출력문을 for문 안에 포함해버려서 출력초과로 인해 오답이 되었다.

 

이후 수정한 답안

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

public class Main {
	public static void main(String[] args) throws IOException {
		
		int i = 0;
		int sum = 0;
		
		Scanner s = new Scanner(System.in);
		int n = s.nextInt();
		
		for (i=1; i<=n; i++) {
			sum += i;
		}
		System.out.printf("%d\n",sum);
	}
}

 

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

[백준 2741번 - java] N찍기  (0) 2021.11.04
[백준 15552번 - java] 빠른 A+B  (0) 2021.11.04
[백준 10950번 - java] A + B - 3  (0) 2021.11.03
[백준 2739번 - java] 구구단  (0) 2021.11.03
[백준 2884번 - java] 알람 시계  (0) 2021.11.03