본문 바로가기

코딩테스트

프로그래머스_0단계_ 더 크게 합치기C++

 

문제풀이: ** 잘풀었다 생각 

문자열로 바꿔준후 이어붙이고 다시 형변환하여 크기비교하기

#include <string>
#include <algorithm> // max 함수를 사용하기 위한 헤더

using namespace std;

int solution(int a, int b) {
    // 두 숫자를 문자열로 변환하여 이어붙인 값 중 더 큰 값을 반환
    string a_str = to_string(a);
    string b_str = to_string(b);
    
    int ab = stoi(a_str + b_str); // a를 앞에, b를 뒤에 붙인 값
    int ba = stoi(b_str + a_str); // b를 앞에, a를 뒤에 붙인 값
    
    return max(ab, ba);
}