반응형
Approach
출처: https://www.acmicpc.net/problem/2908
입력 받은 두수를 거꾸로 뒤집어서 비교하는 문제이다.
나눗셈과 나머지 연산으로 한자리씩 숫자를 뒤집을 수 있다.
C++
#include <stdio.h>
int A, B;
int foo(int x)
{
int ret = 0;
// 한자리씩 뒤로 밀기
for (int i = x ; i ; i /= 10)
{
ret *= 10; // 기존 숫자 앞으로 밀기
ret += (i % 10); // 1의 자리에 해당하는 숫자 채우기
}
return ret;
}
int main()
{
// freopen("input.txt", "r", stdin);
scanf("%d %d", &A, &B);
A = foo(A);
B = foo(B);
printf("%d\n", A > B ? A : B);
}
Java
import java.util.Scanner;
public class Main {
public static String changeStr(String str){
int end = str.length();
str = str.substring(end-1,end) + str.substring(1,end-1) +str.substring(0,1);
return str;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// 두 개의 숫자를 입력 받는다!
int firstInt = sc.nextInt();
int secondInt = sc.nextInt();
// 받은 숫자를 문자열로 전환 한다!
String firstStr = String.valueOf(firstInt);
String secondStr = String.valueOf(secondInt);
// 문자열 특성을 이용해 1,3번째 자리를 교환한다!
firstStr = changeStr(firstStr);
secondStr = changeStr(secondStr);
// 다시 숫자로 바꾸어 수를 비교한다.
firstInt = Integer.parseInt(firstStr);
secondInt = Integer.parseInt(secondStr);
int result = firstInt > secondInt ? firstInt : secondInt;
// (거꾸로 뒤집어진 채) 비교된 결과값을 출력한다!
System.out.println(result);
}
}
반응형
'PS 문제 풀이 > Baekjoon' 카테고리의 다른 글
[BOJ] 백준 11866 요세푸스 문제0 (0) | 2022.02.17 |
---|---|
[BOJ] 백준 14425 문자열 집합 (0) | 2022.02.15 |
[BOJ] 백준 2902 KMP는 왜 KMP일까? (0) | 2022.02.12 |
[BOJ] 백준 2864 5와 6의 차이 (0) | 2022.01.23 |
[BOJ] 백준 2864 5와 6의 차이 (0) | 2022.01.23 |
댓글