백준- 조건문, 반복문

박경희·2024년 1월 17일

코딩테스트

목록 보기
22/69


2480

import java.util.Scanner;

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

        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();

        if (a == b && b == c) {
            System.out.println(10000 + (a * 1000));
        } else if (a == b || a == c) {
            System.out.println(1000 + (a * 100));
        } else if (b == c) {
            System.out.println(1000 + (b * 100));
        } else {
            int maxNumber = Math.max(a, Math.max(b, c));
            System.out.println(maxNumber * 100);
        }
        sc.close();
    }
}

삼항 연산자로 풀이

import java.util.Scanner;

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

        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();

        if (a == b && b == c) {
            System.out.println(10000 + (a * 1000));
        } else if (a == b || c == b || a == c) {
            int sameNumber = (a == b) ? a : (b == c) ? b : c;
            System.out.println(1000 + (sameNumber * 100));
        } else {
            int maxNumber = Math.max(a, Math.max(b, c));
            System.out.println(maxNumber * 100);
        }
    }
}

반복문

2739

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

        int num = sc.nextInt();

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

printf

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

        int num = sc.nextInt();

        for (int i = 1; i < 10; i++) {
            System.out.printf("%d * %d = %d%n", num, i, num * i);
        }
    }
}

10950

import java.util.Scanner;

public class Main {
    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);
        }
    }
}

  • 처음에는 출력을 한번에 나오게 하라는 건가 싶었는데
    '각 테스트 케이스마다 A +B를 출력한다.' 라고 나와있어서 제출해보니 이렇게 나오는게 맞는 문제였다.

8393

import java.util.Scanner;

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

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

25304

import java.util.Scanner;

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

        int x = sc.nextInt();
        int n = sc.nextInt();
        int sum = 0;

        for (int i = 0; i < n; i++) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            sum += (a * b);
        }

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

    }
}

25314

import java.util.Scanner;

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

        int n = sc.nextInt();

        for (int i = 0; i < n / 4; i++) {
            System.out.print("long ");
        }
        System.out.println("int");
    }
}

15552 -BufferedReader

BufferedReader는 Java에서 텍스트를 입력하거나 파일을 읽을 때 사용하는 클래스다.
일반적으로 효율적인 문자열 처리를 위해 사용을 제한하고, 회수를 통해 데이터를 한 번에 처리하는 속도를 높인다.
BufferedReader 일반적으로 InputStreamReader 나 파일 클래스와 함께 사용된다.

BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
  • new InputStreamReader(System.in): 키보드 입력을 읽는다.
  • readLine() : 파일에서 한 줄씩 읽는다.
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 T = Integer.parseInt(br.readLine());

        //반복문으로 각 케이스 처리
        for (int i = 0; i < T; i++) {
            String[] input = br.readLine().split(" "); // 공백으로 분리
            int A = Integer.parseInt(input[0]);
            int B = Integer.parseInt(input[1]);
            bw.write((A + B) + "\n");
        }
        bw.flush();
    }
}
  • BufferedReader로 입력을 받아, readLine()으로 한 줄씩 읽는다.
  • BufferedWriter출력 결과를 출력한다.
  • write()메서드가 데이터를 반환하여 수신하고, 마지막에 한 번만 flush()호출하여 모든 데이터를 출력한다.
  • Scanner와 같이 System.out.println반응하는 비율이 있어 반복적으로 많은 데이터를 처리할 때 유리하다.

11021

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 T = sc.nextInt();

        for (int i = 1; i <= T; i++) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            System.out.printf("Case #%d: %d%n", i, a + b);
        }
    }
}


11022

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 T = sc.nextInt();

        for (int i = 1; i <= T; i++) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            System.out.printf("Case #%d: %d + %d = %d%n", i, a, b, a + b);
        }
    }
}

2438

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 = 0; i < n; i++) {
            for (int j = 0; j < i + 1; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}


별찍기를 다시 해볼 때 마다 뭔가 조금 부족하게 생각하여 별이 옆으로 나열되거나 아래로 나열됐는데,
이제야 어떤 참고도 없이 작성할 수 있게 됐다.

아직 아주 미미한 수준이지만 매일 꾸준히 하다보면 언젠가 어려운 문제도 이렇게 풀게 될 거라 믿는다.

0개의 댓글