Algorithm/BackJoon

[Java | 자바] 백준 알고리즘 단계별로 풀기 - 3단계

Dev다D 2021. 8. 26. 02:29
반응형

2739번

N을 입력받은 뒤, 구구단 N단을 출력하는 프로그램을 작성하시오. 출력 형식에 맞춰서 출력하면 된다.

package Level03;

import java.util.Scanner;

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

 

10950번

두 정수 A와 B를 입력받은 다음, A+B를 출력하는 프로그램을 작성하시오.

package Level03;

import java.util.Scanner;

public class Ex10950 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        for (int i = 0; i < T; i++) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            System.out.println(a+b);
        }
    }
}

 

8393번

n이 주어졌을 때, 1부터 n까지 합을 구하는 프로그램을 작성하시오.

package Level03;

import java.util.Scanner;

public class Ex8393 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int sum = 0;
        for (int i = 0; i <= n; i++) {
            sum += i;
        }
        System.out.println(sum);
    }
}

 

15552번

각 테스트케이스마다 A+B를 한 줄에 하나씩 순서대로 출력한다.

package Level03;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class Ex15552 {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        int n = Integer.parseInt(br.readLine());

        for (int i = 0; i < n; i++) {
            String s = br.readLine();
            int a = Integer.parseInt(s.split(" ")[0]);
            int b = Integer.parseInt(s.split(" ")[1]);
            bw.write(a + b + "\n");
        }
        bw.flush();
    }
}

 

2741번

자연수 N이 주어졌을 때, 1부터 N까지 한 줄에 하나씩 출력하는 프로그램을 작성하시오.

package Level03;

import java.util.Scanner;

public class Ex2741 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();

        for (int i = 1; i <= N; i++) {
            System.out.println(i);
        }
    }
}

 

2742번

자연수 N이 주어졌을 때, N부터 1까지 한 줄에 하나씩 출력하는 프로그램을 작성하시오.

package Level03;

import java.util.Scanner;

public class Ex2742 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();

        for (int i = 0; i < N; i++) {
            System.out.println(N - i);
        }
    }
}

 

11021번

두 정수 A와 B를 입력받은 다음, A+B를 출력하는 프로그램을 작성하시오.

package Level03;

import java.util.Scanner;

public class Ex11021 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();

        for(int i = 1; i <= T; i++) {
            int A = sc.nextInt();
            int B = sc.nextInt();
            System.out.println("Case #" + i + ": " + (A + B));
        }
    }
}

 

11022번

두 정수 A와 B를 입력받은 다음, A+B를 출력하는 프로그램을 작성하시오.

package Level03;

import java.util.Scanner;

public class Ex11022 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();

        for (int i = 1; i <= T; i++) {
            int A = sc.nextInt();
            int B = sc.nextInt();
            System.out.println("Case #" + i + ": " + A + " + " + B + " = " + (A + B));
        }
    }
}

 

2438번

첫째 줄에는 별 1개, 둘째 줄에는 별 2개, N번째 줄에는 별 N개를 찍는 문제

package Level03;

import java.util.Scanner;

public class Ex2438 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();

        for (int i = 0; i < N; i++) {
            for (int j = 0; j < i + 1; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

 

2439번

첫째 줄에는 별 1개, 둘째 줄에는 별 2개, N번째 줄에는 별 N개를 찍는 문제

하지만, 오른쪽을 기준으로 정렬한 별

package Level03;

import java.util.Scanner;

public class Ex2439 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();

        for (int i = 0; i < N; i++) {
            for (int j = N; j > i + 1; j--) {
                System.out.print(" ");
            }
            for (int j = 0; j < i + 1; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

 

10871번

정수 N개로 이루어진 수열 A와 정수 X가 주어진다. 이때, A에서 X보다 작은 수를 모두 출력하는 프로그램을 작성하시오.

package Level03;

import java.util.Scanner;

public class Ex10871 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int X = sc.nextInt();
        int a[] = new int[N];

        for (int i = 0; i < a.length; i++) {
            a[i] = sc.nextInt();
            if (a[i] < X) {
                System.out.println(a[i]);
            }
        }
    }
}
반응형