Farmer John's cows have been holding a daily online gathering on the "mooZ" video meeting platform. For fun, they have invented a simple number game to play during the meeting to keep themselves entertained.
Elsie has three positive integers AA, BB, and CC (A≤B≤CA≤B≤C). These integers are supposed to be secret, so she will not directly reveal them to her sister Bessie. Instead, she gives Bessie seven (not necessarily distinct) integers in the range 1…1091…109, claiming that they are AA, BB, CC, A+BA+B, B+CB+C, C+AC+A, and A+B+CA+B+C in some order.
Given a list of these seven numbers, please help Bessie determine AA, BB, and CC. It can be shown that the answer is unique.
The only line of input consists of seven space-separated integers.
Print AA, BB, and CC separated by spaces.
2 2 11 4 9 7 9
2 2 7
Problem credits: Benjamin Qi
[/hide]
(Analysis by Brian Dean and Benjamin Qi)
With some careful reasoning, we can deduce that the smallest two numbers must be AA and BB, and the largest must be A+B+CA+B+C. Subtracting the smallest two from the largest therefore gives us CC and we are done.
The only computational aspect of this problem therefore is locating the two smallest and the largest of the seven input numbers. One way to do this is by sorting the numbers, giving a very concise answer. E.g., in C++, this looks like:
#include <iostream> #include <algorithm> using namespace std; int main(void) { int nums[7]; for (int i=0; i<7; i++) cin >> nums[i]; sort (nums, nums+7); int a = nums[0], b = nums[1]; int c = nums[6] - a - b; cout << a << " " << b << " " << c << "\n"; }
In Python, a similarly-concise solution might be:
nums = list(sorted(map(int,input().split()))) a,b = nums[0],nums[1] c = nums[-1]-a-b print(a,b,c)
Sorting isn't absolutely necessary here. One could also just scan the input to find the largest number, and then scan two more times to find the two smallest numbers (being careful to account for the possibility these might be the same value). Code for this is slightly longer but not too bad. Here's an example in C++:
#include <iostream> using namespace std; int main(void) { int nums[7], A, B, C; for (int i=0; i<7; i++) cin >> nums[i]; int largest = nums[0]; for (int i=1; i<7; i++) if (nums[i] > largest) largest = nums[i]; int smallest = nums[0], count_smallest = 1; for (int i=1; i<7; i++) { if (nums[i] == smallest) count_smallest++; if (nums[i] < smallest) { smallest = nums[i]; count_smallest = 1; } } if (count_smallest > 1) { A = B = smallest; C = largest - A - B; } else { int second_smallest = nums[0]; if (second_smallest == smallest) second_smallest = nums[1]; for (int i=1; i<7; i++) if (nums[i] < second_smallest && nums[i] != smallest) second_smallest = nums[i]; A = smallest; B = second_smallest; C = largest - A - B; } cout << A << " " << B << " " << C << "\n"; }
[/hide]
© 2025. All Rights Reserved. 沪ICP备2023009024号-1