문제 링크
난이도 : Lv. 0

문제 내용

연산 ⊕는 두 정수에 대한 연산으로 두 정수를 붙여서 쓴 값을 반환합니다. 예를 들면 다음과 같습니다.

  • 12 ⊕ 3 = 123
  • 3 ⊕ 12 = 312

양의 정수 a와 b가 주어졌을 때, a ⊕ b와 b ⊕ a 중 더 큰 값을 return 하는 solution 함수를 완성해 주세요.

단, a ⊕ b와 b ⊕ a가 같다면 a ⊕ b를 return 합니다.

입력

a = 9

b = 91

출력

991

문제 분석

int → char로 a, b를 변환

ab 순서대로 붙인거랑 ba 순서대로 붙인거랑 다시 int화 해서 크기 비교

작성한 코드

#include <string>
#include <vector>
 
using namespace std;
 
int solution(int a, int b) {
    char ch_ab[100000000], ch_ba[100000000];
    int answer = 0;
    sprintf(ch_ab, "%d%d", a,b);
    sprintf(ch_ba, "%d%d", b,a);
    int ab = atoi(ch_ab);
    int ba = atoi(ch_ba);
    answer = ab>=ba?ab:ba;
    return answer;
}

우수 코드 분석

#include <string>
#include <vector>
 
using namespace std;
 
int solution(int a, int b) {
    return max(stoi(to_string(a)+to_string(b)),stoi(to_string(b)+to_string(a)));
}

레전드 벽느낌.. 라이브러리를 잘 알아야겠다..

stoi, to_string 함수 string 헤더파일에 있음!