[C++] 프로그래머스 더 크게 합치기

문제설명

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

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

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

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


제한사항

• 1 ≤ a, b < 10,000


입출력 예

a b result
9 91 991
89 8 898


풀이

⊕ 연산의 결과로 나온 숫자중 더 큰값을 출력하는 로직을 구현하여 문제를 풀었습니다.

#include <string>
#include <vector>

using namespace std;

int solution(int a, int b) {
    int answer = 0;
    
    string str1 = to_string(a);
    string str2 = to_string(b);
    
    // a + b
    int try1 = stoi(str1 + str2);
    // b + a
    int try2 = stoi(str2 + str1);
    
    answer = max(try1, try2);
    
    return answer;
}


결과

코드 실행결과

Categories:

Updated:

Leave a comment