본문 바로가기
Algorithm(알고리즘)/Java

Java 백준 단계별 풀기 (기초 문법 정리) for문, while문

by Jun_N 2020. 12. 23.

2739 - 구구단

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int A = sc.nextInt();
        for (int i = 1; i < 10; i++) {
            System.out.println(A + " * " + i + " = " + A * i);
        }
    }
}r

 

15552 - 빠른 A + B

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int tc = Integer.parseInt(br.readLine());

        for (int i = 1; i <= tc; i++) {
            String[] line = br.readLine().split(" ");
            int a = Integer.parseInt(line[0]);
            int b = Integer.parseInt(line[1]);
            bw.write(String.valueOf(a + b));
            bw.newLine();
        }

        bw.flush();
        br.close();
        bw.close();

    }
}

BufferedReader 와 BufferedWriter를 사용하면 더 빠르게 입력과 출력을 할 수 있다.

BufferedWriter.flush 는 마지막에 한번만 해주면 된다.

 

2742 - 기찍 N

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

public class Main {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        for (int i = n; i > 0; i--) {
            System.out.println(i);
        }
    }
}

 

10871 - X보다 작은 수

방법 1 ) array 사용

import java.util.Scanner;
 
public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
 
		int N = in.nextInt();
		int X = in.nextInt();
		int arr[] = new int[N];
        
		for (int i = 0; i < N; i++) {
			arr[i] = in.nextInt();
		}
 
		in.close();
        
		for (int i = 0; i < N; i++) {
			if (arr[i] < X) {
				System.out.print(arr[i] + " ");
			}
		}
	}
}

 

방법 2) StringBuilder 사용

import java.util.Scanner;
 
public class Main {
 
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int N = in.nextInt();
		int X = in.nextInt();
        
		StringBuilder sb = new StringBuilder();
 
		for(int i = 0 ; i < N ; i++) {
			
			int value = in.nextInt();
			if(value < X) {
				sb.append(value+" ");
			}
		}
		System.out.println(sb);	
	}
}

 

방법 3) BufferedReader, StringTokenizer 사용

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
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 N = Integer.parseInt(st.nextToken());
		int X = Integer.parseInt(st.nextToken());
 
		StringBuilder sb = new StringBuilder();
 
		st = new StringTokenizer(br.readLine(), " ");
		
		for (int i = 0; i < N; i++) {
			int value = Integer.parseInt(st.nextToken());
 
			if (value < X)
				sb.append(value).append(' ');
		}
		System.out.println(sb);
	}
}
 

 

속도는 3 > 2 > 1 순으로 빠르다

 

1110 - 더하기 사이클 (While)

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int left, right, newValue = 0, count = 0;
        int N = sc.nextInt();
        int check = N;
        while (true) {
            if (N < 10) {
                left = 0;
                right = N;
            } else {
                left = N / 10;
                right = N % 10;
            }
            newValue = left + right;
            N = right * 10 + newValue % 10;
            count++;
            if (check == N)
                break;
        }
        sc.close();
        System.out.println(count);
    }
}