코딩 테스트/알고리즘
알고리즘 5번 -js, c++
GREEN나무
2024. 11. 12. 08:48
728x90
JS
문제
정수 num1과 num2가 매개변수로 주어집니다. 두 수가 같으면 1 다르면 -1을 retrun하도록 solution 함수를 완성해주세요.
제한사항
0 ≤ num1 ≤ 10,000
0 ≤ num2 ≤ 10,000
계획
삼항연산자 쓰
참고, 풀이
return isMember ? '$2.00' : '$10.00';
답
function solution(num1, num2) {
return (num1 == num2) ? 1 : -1;
}
코드 간략화하기
function solution(num1, num2) {
return num1 == num2 ? 1 : -1;
}
// 삼항연산자에도 ()가 필요 없네요
출처
삼항연산자 : https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Operators/Conditional_operator
◆ C++
답
#include <string>
#include <vector>
using namespace std;
int solution(int num1, int num2) {
return num1 == num2 ? 1 : -1;
}