In this very first task, let us look at a boring mathematics problem. :-)
We have three different integers, x, y and z, which satisfy the following three relations:
- x + y + z = A
- xyz = B
- x2 + y2 + z2 = C
You are asked to write a program that solves for x, y and z for given values of A, B and C.
For the Extreme version of this problem, please click here.
Input
The first line of the input file gives the number of test cases N (< 20). Each of the following N lines gives the values of A, B and C (1 ≤ A, B, C ≤ 10000).
Output
For each test case, output the corresponding values of x, y and z. If there are many possible answers, choose the one with the least value of x. If there is a tie, output the one with the least value of y. If there is no solution, output the line "No solution." instead.
Sample Input
2 1 2 3 6 6 14
Sample Output
No solution. 1 2 3
#include <cstdio>
using namespace std;
int a, b, c;
bool input();
void solve();
int main()
{
#ifndef ONLINE_JUDGE
freopen("d:\\OJ\\uva_in.txt", "r", stdin);
#endif
int n;
scanf("%d", &n);
while (n--) {
input();
solve();
}
return 0;
}
bool input()
{
scanf("%d%d%d", &a, &b, &c);
return true;
}
void solve()
{
bool found = false;
int x, y, z;
for (x = -22; x <= 22 && !found; x++) {
if (x * x <= c) {
for (y = -100; y <= 100 && !found; y++) {
if (y != x && x * x + y * y <= c) {
for (z = -100; z <= 100 && !found; z++) {
if (z != x && z != y && x + y + z == a
&& x * y * z == b
&& x * x + y * y + z * z == c) {
printf("%d %d %d\n", x, y, z);
found = true;
}
}
}
}
}
}
if (!found) {
printf("No solution.\n");
}
}