
나의 코드
#include <string>
#include <vector>
using namespace std;
int solution(int num1, int num2) {
int answer = 0;
if (num1 == num2) {
answer = 1;
}
else {
answer = -1;
}
return answer;
}
다른 사람의 코드
#include <string>
#include <vector>
using namespace std;
int solution(int num1, int num2) {
return num1 == num2 ? 1 : -1;
}
짧고 간결한 코드가 무조건 좋은 것일까?
그렇게 생각하던 때도 있었지만 나중에 그렇게 짠 코드를 볼 때
오히려 한 눈에 알아보기 힘들었던 기억도 있다.
이런 간단한 코드라면 저렇게 삼항연산자를 쓰는게 좋을 것 같다고 생각하긴 하지만...
'Practice & Study > 프로그래머스' 카테고리의 다른 글
프로그래머스>코딩테스트 입문>최빈값 구하기 (C++) | sort, max_element (0) | 2023.06.04 |
---|---|
프로그래머스>코딩테스트 입문>짝수 홀수 개수 (C++) (0) | 2023.06.03 |
프로그래머스>코딩테스트 입문배열 두 배 만들기 (C++) | auto& (0) | 2023.06.03 |
프로그래머스>코딩테스트 입문>분수의 덧셈 (C++) | 최대공약수, 최소공배수 (0) | 2023.06.03 |
프로그래머스>코딩테스트 입문>두 수의 나눗셈 (C++) (0) | 2023.06.03 |