모든 챌린지
보통 1명 해결 정답률 100.0%

Exact Fraction Arithmetic

Perform fraction operations and return a reduced fraction.

배우게 될 내용

  • Add, subtract, multiply and divide fractions
  • Reduce results with the GCD
  • Normalize signs and zero

문제

번역본 보기

Represent a fraction as [numerator, denominator]. Apply operation to first and second: add, subtract, multiply, or divide. For absolute, use only first. For power, raise first to the integer exponent and ignore second. Return lowest terms with a positive denominator. Normalize zero to [0, 1]. All divisions and negative powers in valid inputs are defined.

Implement rational_arithmetic(operation, first, second, exponent).

Input

operation: The operation name.

first: Two integers representing the first fraction.

second: Two integers representing the second fraction; use [0, 1] when unused.

exponent: The exponent for power; use 0 for other operations.

Output

Return [numerator, denominator] in reduced form with a positive denominator.

제약 조건

operation is add, subtract, multiply, divide, absolute, or power. Both fraction components have magnitude at most 100; denominators are nonzero. −6 ≤ exponent ≤ 6. For divide, second has nonzero numerator. For a negative power, first has nonzero numerator. The 0th power is [1, 1], including 0⁰ by this task’s convention. Values fit signed 64-bit.

예시

예시 1
입력:
operation = "add"
first = [1, 6]
second = [1, 4]
exponent = 0

예상 출력:
expected = [5, 12]
입력:
std::string operation = "add";
std::vector<long long> first = {1, 6};
std::vector<long long> second = {1, 4};
long long exponent = 0;

예상 출력:
std::vector<long long> expected = {5, 12};
입력:
string operation = "add";
long[] first = new long[] {1, 6};
long[] second = new long[] {1, 4};
long exponent = 0;

예상 출력:
long[] expected = new long[] {5, 12};

설명

1/6 + 1/4 = 2/12 + 3/12 = 5/12, already reduced.

예시 2
입력:
operation = "power"
first = [2, 3]
second = [0, 1]
exponent = -2

예상 출력:
expected = [9, 4]
입력:
std::string operation = "power";
std::vector<long long> first = {2, 3};
std::vector<long long> second = {0, 1};
long long exponent = -2;

예상 출력:
std::vector<long long> expected = {9, 4};
입력:
string operation = "power";
long[] first = new long[] {2, 3};
long[] second = new long[] {0, 1};
long exponent = -2;

예상 출력:
long[] expected = new long[] {9, 4};

설명

A negative exponent inverts the fraction: (2/3)⁻² = (3/2)² = 9/4.

접근 방법

먼저 스스로 풀어 본 뒤 힌트를 하나씩 열어 보세요.

힌트 1

Convert each fraction to a common representation and combine the numerators according to the chosen operation.

힌트 2

After the operation, reduce the result by dividing by the greatest common divisor and make the denominator positive.

힌트 3

Watch special cases like zero results, absolute value, and the power rule’s zero and negative exponents.

예상 복잡도

O(log n) time, O(1) extra space

참고 자료

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

코더마다 가장 좋은 통과 풀이입니다. 더 빠르고, 메모리를 덜 쓰고, 평가가 좋은 풀이가 위에 옵니다.

# 코더 언어 시간 메모리 AI 평가 풀이
1 ННикита Python 1,093 ms - 4/5 코드 보기

다른 사람의 풀이 보기

커뮤니티 솔루션

스포일러: 아래에 1개 전체 솔루션 있음 먼저 직접 풀어보세요. 다른 개발자의 코드와 AI 리뷰도 참고할 수 있습니다. 로그인하고 문제를 풀면 토론에 참여할 수 있습니다.