모든 챌린지
쉬움 0명 해결 정답률 0%

Absolute Value Without abs

Compute an absolute value with a condition.

문제

The absolute value of a number is its distance from zero. Compute it with an if statement instead of a built-in abs function.

Implement absolute_value(n).

Input

n: Any integer.

Output

Return n if it is zero or positive, otherwise -n.

제약 조건

-1,000,000,000 ≤ n ≤ 1,000,000,000

예시

예시 1
입력:
n = -42

예상 출력:
expected = 42
입력:
long long n = -42;

예상 출력:
long long expected = 42;
입력:
long n = -42;

예상 출력:
long expected = 42;

설명

-42 is negative, so the answer is 42.

예시 2
입력:
n = 15

예상 출력:
expected = 15
입력:
long long n = 15;

예상 출력:
long long expected = 15;
입력:
long n = 15;

예상 출력:
long expected = 15;

설명

15 is already positive, so it stays 15.

참고 자료

이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.

다른 사람의 풀이 보기

커뮤니티 솔루션

아직 공개된 풀이가 없습니다. 문제를 풀고 첫 풀이를 공유하세요.