개발공부
백준 8393번 문제 대체 뭐가 문제인가
pumaclass
2024. 8. 25. 20:35
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
int result = 0;
for (int i = 1; i <= num ; i++) {
result += i;
System.out.println(result);
}
sc.close();
}
}
자꾸 출력초과라고 나오는데 이유를 모르겠다.
내가 뭘 놓친거지
계속 이것저것 바꿔가면서 시도하다가 도저히 모르겠어서 chatGpt한테 물어봤다.
sout가 for문 안에 들어있어서 돌때마다 출력되는게 문제란다..
아..
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
int result = 0;
for (int i = 1; i <= num ; i++) {
result += i;
}
System.out.println(result);
sc.close();
}
}
귀신같이 해결됐다.
다음부터는 예제출력을 좀더 눈여겨 봐야겠다.
생각해보니 출력 초과가 나왔던 이유는 저 for문이 돌았을때
입력값이 3일때
1
3
6
이렇게 돌아서 출력됐으니 한번의 출력만 받는곳에서는 당연히 에러를 띄웠겠지
오늘도 백준에 속는다