Posts [Java] 백준 #2908: 상수
Post
Cancel

[Java] 백준 #2908: 상수

문제

상근이는 수의 크기를 비교하는 문제를 내주었다. 첫째 줄에 상근이가 칠판에 적은 두 수 A와 B가 주어진다. 두 수는 같지 않은 세 자리 수이며, 0이 포함되어 있지 않다. 상수는 수를 거꾸로 읽는다. 상수의 대답을 출력하는 프로그램을 작성하라.

풀이

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int first = sc.nextInt();
    int second = sc.nextInt();
    if (reverse(first) > reverse(second)) {
      System.out.println(reverse(first));
    } else {
      System.out.println(reverse(second));
    }
  }

  static int reverse(int input) {
    String result = "";
    while (input != 0) {
      result += input % 10;
      input /= 10;
    }
    return Integer.parseInt(result);
  }
}

This post is licensed under CC BY 4.0 by the author.

[Java] 백준 #1152: 단어의 개수 (Java)

[JS] 생활코딩 #23: 함수와 this

Loading comments from Disqus ...