junghyunjjan   3년 전

맞은 코드

#include
#include
#include

using namespace std;

int main(){
cin.tie(NULL);
cout.tie(NULL);
ios::sync_with_stdio(false);

string A, B;

cin >> A >> B;

reverse(A.begin(), A.end());
reverse(B.begin(), B.end());

string bigger = (stoi(A)>stoi(B)) ? A : B;

cout << bigger;

return 0;
}

틀린 코드

#include
#include
#include

using namespace std;

int main(){
cin.tie(NULL);
cout.tie(NULL);
ios::sync_with_stdio(false);

string A, B;

cin >> A >> B;

reverse(A.begin(), A.end());
reverse(B.begin(), B.end());

cout << (stoi(A)>stoi(B)) ? A : B;

return 0;
}

왜 2번째 코드가 틀렸다고 되나요?

qktkzpal3301   3년 전

아래 코드가 틀리는 이유는 입출력 연산자가 삼항 연산자보다 우선순위가 높아서 그럴듯 한데요

(stoi(A)>stoi(B))의 리턴값을 출력하게 될 것 같습니다.

괄호로 크게 한번 감싸면 맞겠네요.

cout << ((stoi(A)>stoi(B)) ? A : B);

junghyunjjan   3년 전

감사합니다. 연산 과정을 디버깅해보니

(count << stoi(A)) > stoi(B) ? A : B 이렇게 들어가더군요.

앞으로 연산우선순위에 주의해야겠네요.

댓글을 작성하려면 로그인해야 합니다.