4 분 소요

👉 백준 조건문 https://www.acmicpc.net/step/4


백준 단계별로 풀어보기 2단계 - 조건문(1~4번) 입니다.

오늘도 화이팅 🔥🔥


1. 두 수 비교하기 (1330번)

image-20231005155807645

https://www.acmicpc.net/problem/1330

두 수 A, B를 입력받은 다음, 두 수의 크기를 비교해서

알맞은 연산자(>, <, ==)를 출력해 주면 되는 간단한 문제이다.


Java (Scanner)

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();
	//조건비교 및 출력
    if(A > B) {
      System.out.println(">");
    }
    else if(A < B){
      System.out.println("<");
    }
    else {
      System.out.println("==");
    }
  }
}

Java (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 A = Integer.parseInt(st.nextToken());
    int B = Integer.parseInt(st.nextToken());
	//조건비교 및 출력
    if(A > B) {
      System.out.println(">");
    }
    else if(A < B){
      System.out.println("<");
    }
    else {
      System.out.println("==");
    }
  }
}

image-20231005160536714

제출 번호 67560693 - BufferedReader + StringTokenizer (124ms)

제출 번호 67560474 - Scanner (208ms)

ifelse if를 적절히 활용해주면 되는 쉬운 문제이다


2. 시험 성적 (9498번)

image-20231005160740727

https://www.acmicpc.net/problem/9498

정수를 입력 받아 조건문을 통해

90~100A, 80~89B

60 미만F를 출력해주면 되는 문제이다.


Java (Scanner) - if문

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
	//입력
    int score = sc.nextInt();
    char rank = ' ';
	//비교하기
    if(score >= 90 && score <= 100){
      rank = 'A';
    }
    else if(score >= 80 && score <= 89){
      rank = 'B';
    }
    else if(score >= 70 && score <= 79){
      rank = 'C';
    }
    else if(score >= 60 && score <= 69){
      rank = 'D';
    }
    else{
      rank = 'F';
    }
    //출력
    System.out.println(rank);
  }
}

비교 연산자만 주의해서 잘 써주면 된다.

하지만 if - else if로 하면 조건을 써주기가 귀찮다.

손을 조금 더 편하게 하는 방법이 있다.

-

Java (Scanner) - switch case 문

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    //입력
    int score = sc.nextInt();
    char rank = ' ';
    //비교
    switch(score/10){
      case 10:
      case 9:
        rank = 'A';
        break;
      case 8:
        rank = 'B';
        break;
      case 7:
        rank = 'C';
        break;
      case 6:
        rank = 'D';
        break;
      default:
        rank = 'F';
    }
    //출력
    System.out.println(rank);
  }
}

score / 10 을 통해 앞 자리만 잘라주고(10, 9, 8, 7, 6) 비교하면 된다.

switch-case 문을 이용해서 100점인 경우에만 예외 처리 해주고, 비교해주면 된다 !!

if문보다 손이 덜 아프다 !!

-

Java (BufferedReader) - switch case 문

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 score = Integer.parseInt(br.readLine());
    char rank = ' ';
    //비교
    switch (score / 10) {
      case 10:
      case 9:
        rank = 'A';
        break;
      case 8:
        rank = 'B';
        break;
      case 7:
        rank = 'C';
        break;
      case 6:
        rank = 'D';
        break;
      default:
        rank = 'F';
    }
    //출력
    System.out.println(rank);
  }
}

image-20231005163541708

제출 번호 67563442 - BufferedReader (switch case) (124ms)

제출 번호 67563096 - Scanner (switch case) (208ms)

제출 번호 67562321 - Scanner (if) (212ms)

본인 취향대로 하면 된다!


3. 윤년 (2753번)

image-20231005163954603

https://www.acmicpc.net/problem/2753

입력 받은 수가 4의 배수이면서(AND) 100의 배수가 아닐 때,

또는(OR) 400의 배수일 때 1을 출력하고, 아닐 때 0을 출력해주면 된다 !!


Java (Scanner)

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    //입력
    int year = sc.nextInt();
    //비교 및 출력
    if(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)){
      System.out.println(1);
    }
    else {
      System.out.println(0);
    }
  }
}

-

Java (BufferedReader)

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 year = Integer.parseInt(br.readLine());
    //비교 및 출력
    if(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)){
      System.out.println(1);
    }
    else {
      System.out.println(0);
    }
  }
}

image-20231005165849212

제출 번호 67564990 - BufferedReader (124ms)

제출 번호 67564974 - Scanner (212ms)

윤년인 조건을 유의해서 코드를 짜주면 된다.

4의 배수이면서(AND) 100의 배수가 아닐 때

또는(OR)

400의 배수일 때 윤년이다.

((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)

이렇게 해주면 된다 !!


4. 사분면 고르기 (14681번)

image-20231005170706729

https://www.acmicpc.net/problem/14681

두 수 x, y를 입력 받아서, 좌표가 위치해 있는 사분면을 출력해주면 된다.


Java (Scanner)

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    //입력
    int x = sc.nextInt();
    int y = sc.nextInt();
    //비교 및 출력
    if(x > 0 && y > 0){
      System.out.println(1);
    }
    else if(x < 0 && y > 0){
      System.out.println(2);
    }
    else if(x < 0 && y < 0){
      System.out.println(3);
    }
    else{
      System.out.println(4);
    }
  }
}

-

Java (BufferedReader)

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 x = Integer.parseInt(br.readLine());
    int y = Integer.parseInt(br.readLine());
    //비교 및 출력
    if(x > 0 && y > 0){
      System.out.println(1);
    }
    else if(x < 0 && y > 0){
      System.out.println(2);
    }
    else if(x < 0 && y < 0){
      System.out.println(3);
    }
    else{
      System.out.println(4);
    }
  }
}

image-20231005172003280

제출 번호 67566352 - BufferedReader (124ms)

제출 번호 67566183 - Scanner (216ms)

좌표 값을 비교해서 해당 사분면을 출력해주면 된다.

&&(AND)연산을 적절하게 이용하면 된다!


thumbdochi

열심히 아자자자자자 🔥🔥🔥

댓글남기기