- 문제

아까 1~N까지의 숫자합한걸 이번엔 그대로 합하지 않고 내빼서 세로로 출력해주면된다.
- 코드 1
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner in=new Scanner(System.in);
        int N=in.nextInt();
        
        in.close();
        
        for(int i=1; i<=N; i++){
        	System.out.println(i);
        }
    }
}for문대신 while을 사용해도 된다. 둘의 성능은 유의미한 차이가 없다.
//for문 사용
for(int i=1; i<=N; i++){
	System.out.println(i);
}
//while문 사용
int i=1;
while(i<=N;){
	System.out.println(i);
    i++
}- 결과

- 코드 2
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
 
public class Main {
    public static void main(String[] args) throws IOException {
	    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
	    int N = Integer.parseInt(br.readLine());
	    br.close();
        
	    int i = 1;
	    while(i<= N) {
		    System.out.println(i);
		    i++;
	    }	
    }
}- 결과

- 코드 1,2 성능 비교


'Computer Science > BAEKJOON JAVA Practice' 카테고리의 다른 글
| [백준]11022번: A+B-8 (0) | 2022.01.11 | 
|---|---|
| [백준]2742번: 기찍N (0) | 2022.01.11 | 
| [백준]15552번: 빠른 A+B (0) | 2022.01.09 | 
| [백준]8393번: 합 (0) | 2022.01.09 | 
| [백준]10950번: A+B-3 (0) | 2022.01.08 | 
 
