Java Challenge -3

CYSSSSSSSSS·2024년 3월 9일

자바 챌린지

목록 보기
3/11

Java Challenge

반복문

백준 2739

  • 사용자로부터 입력받은 숫자에 구구단을 출력하는 프로그램을 작성하시오
package 반복문;

import java.util.Scanner;

public class BOJ2739 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();

        for(int i=1;i<=9;i++){
            System.out.println(n + " * " + i + " = "  + (n*i));
        }
    }
}

백준 10950

  • 두 정수 A,B를 입력받은 다음 A+B를 출력하는 프로그램을 작성하시오.
package 반복문;

import java.util.Scanner;

public class BOJ10950 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();
        int a,b;
        for(int i = 0; i<n; i++){
            a = scanner.nextInt();
            b = scanner.nextInt();

            System.out.println(a+b);
        }
    }
}

백준 8393

  • n을 입력받아 1부터 n까지 합을 구하는 프로그램을 작성하시오.
package 반복문;

import java.util.Scanner;

public class BOJ8393 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();
        int answer = 0;
        for(int i = 1; i<=n; i++){
            answer += i;
        }
        System.out.println(answer);
    }
}

백준 25304

  • 영수증 가격과 물건의 가격의 합이 일치하는 여부를 작성하시오
package 반복문;

import java.util.Scanner;

public class BOJ25304 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int price = scanner.nextInt();
        int count = scanner.nextInt();
        int p,c;
        int sum = 0;
        for (int i = 0; i<count; i++){
            p = scanner.nextInt();
            c = scanner.nextInt();
            sum += (p *c);
        }

        if (price == sum){
            System.out.println("Yes");
        }else {
            System.out.println("No");
        }

    }
}

백준 25314

  • long 이라는 텍스트가 붙을때마다 4바이트가 추가된 값을 리턴하는 프로그램을 작성하시오.
package 반복문;

import java.util.Scanner;

public class BOJ25314 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();
        n = n / 4;
        StringBuilder answer = new StringBuilder(); // 문자열을 계속해서 붙이고 싶을때는 StringBuilder 클래스를 사용하는것이 좋다.
        for (int i = 0; i <n;i++){
            answer.append("long ");
        }
        answer.append("int");

        System.out.println(answer);
    }
}

StringBuilder

  • 문자열을 효율적으로 처리하기 위한 클래스
  • StringBuilder는 문자열의 내용을 직접 수정 가능
  • 주요 특징으로는 가변성,성능 개선,메서드체이닝이 있다.
  • String으로 변환하고 싶을때는 .toString(); 사용하면 된다.

백준 15552

  • 첫 줄에 테스트케이스의 개수 T가 주어진다. T는 최대 1,000,000이다. 다음 T줄에는 각각 두 정수 A와 B가 주어진다. A와 B는 1 이상, 1,000 이하이다.
  • 각 테스트케이스마다 A+B를 한 줄에 하나씩 순서대로 출력한다.
package 반복문;

import java.io.*;

public class BOJ15552 {
    public static void main(String[] args) throws IOException {
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
             BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out))){
            int n = Integer.parseInt(reader.readLine());
            String line;
            int a,b;
            for(int i = 0; i < n; i++){
                line = reader.readLine();
                String[]parts = line.split("\\s+");
                a = Integer.parseInt(parts[0]);
                b = Integer.parseInt(parts[1]);
                writer.write(String.valueOf(a+b));
                writer.newLine();
            }
            writer.flush();

        }catch (IOException | NumberFormatException e){
            e.printStackTrace();
        }
    }
}

백준 11021

  • A,B를 입력받아 A+B를 출력하는 프로그램을 작성하시오
package 반복문;

import java.util.Scanner;

public class BOJ11021 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int a,b;
        for (int i=0; i<n;i++){
            a = scanner.nextInt();
            b = scanner.nextInt();

            System.out.println("Case #"+(i+1)+": " + (a+b));
        }
    }
}

백준 11022

  • A,B를 입력받아 A+B를 출력하는 프로그램을 작성하시오.
package 반복문;

import java.util.Scanner;

public class BOJ11022 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();
        int a,b;
        for(int i=0; i<n;i++){
            a = scanner.nextInt();
            b = scanner.nextInt();
            System.out.println("Case #" + (i+1) + ": " +a + " + " + b +" = " + (a+b));
        }
    }
}

백준 2438

  • 숫자를 입력받고 별찍기를 구현하시오
package 반복문;

import java.util.Scanner;

public class BOJ2438 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();

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

백준 2439

  • 숫자를 입력받고 별찍기를 구현하시오
package 반복문;

import java.util.Scanner;

public class BOJ2439 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();

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

    }
}

백준 10952

  • a,b 를 입력받아 a+b를 출력하는 프로그램을 작성하시오
  • 이떄 a == 0 && b == 0 이면 프로그램을 종료하시오(무한 루프)
package 반복문;

import java.util.Scanner;

public class BOJ10952 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int a,b;

        while (true){
            a = scanner.nextInt();
            b = scanner.nextInt();

            if(a == 0 && b == 0){
                break;
            }

            System.out.println(a+b);
        }
    }
}

백준 10951

  • 입력은 여러 개의 테스트 케이스로 이루어져 있다.

  • 각 테스트 케이스는 한 줄로 이루어져 있으며, 각 줄에 A와 B가 주어진다. (0 < A, B < 10)

  • 각 테스트 케이스마다 A+B를 출력한다.

package 반복문;

import java.util.Scanner;

public class BOJ10951 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int a,b;
        while(scanner.hasNext()){
            a = scanner.nextInt();
            b = scanner.nextInt();
            System.out.println(a+b);
        }
        scanner.close();
    }
}
profile
개발자 되고 싶어요

0개의 댓글