Duplicate#541

Solution

Check the rules in priority order: three equal numbers, then a pair, then arithmetic. For example, 2 2 4 scores 20, even though two numbers sum to the third.

Sort the numbers as x \le y \le z. Since they are positive, a sum is greater than either summand. Thus two numbers can sum to the third only when x+y=z.

Subtraction gives no additional cases: p-q=r is equivalent to q+r=p. The same equality x+y=z therefore covers both operations.

Algorithm

  1. Sort the three numbers.

  2. If x=z, print 30.

  3. Otherwise, if x=y or y=z, print 20.

  4. Otherwise, if x+y=z, print 10.

  5. Otherwise, print 0.

Correctness proof

If x=z, the sorted order implies that all three numbers are equal. The algorithm correctly applies the first rule.

Otherwise, the three numbers are not all equal. Equal values are adjacent after sorting, so x=y or y=z holds exactly when two numbers are equal. The algorithm correctly applies the second rule.

If neither condition holds, all three numbers are distinct. By the observation above, a permitted arithmetic operation exists exactly when x+y=z. The algorithm therefore awards 10 points exactly when the third rule applies, and 0 otherwise. These cases cover every legal input, proving correctness.

Complexity

There are always exactly three numbers, so time and auxiliary space are . The largest sum is 12, so int is sufficient.

C++17 implementation

#include <algorithm>
#include <array>
#include <iostream>
int main() {
    std::array<int, 3> d{};
    std::cin >> d[0] >> d[1] >> d[2];
    std::sort(d.begin(), d.end());
    int answer;
    if (d[0] == d[2]) answer = 30;
    else if (d[0] == d[1] || d[1] == d[2]) answer = 20;
    else if (d[0] + d[1] == d[2]) answer = 10;
    else answer = 0;
    std::cout << answer << '\n';
}