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
? x^2 + y^2 + z^2 = C
You are asked to write a program that solves for x, y and z for given values of A, B and C.
Input
The first line of the input file gives the number of test cases N (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
问题链接:UVA11565 Simple Equations
问题简述:(略)
问题分析:
????数学问题,用枚举法来解决。因为x^2 + y^2 + z^2 = C,所以x在正负sqrt(c)的范围内,而y^2和z^2都是正数,所以需要满足x^2<=C。因为x、y和z各不相同,且满足x<y<z,所以y最小从x+1开始试探。同理,因为x^2 + y^2 + z^2 = C,所以需要满足x^2+y^2<=C。其他按照题意公式计算和判定即可。
程序说明:(略)
参考链接:(略)
题记:(略)
AC的C语言程序如下:
/* UVA11565 Simple Equations */
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n, a, b, c, x, y, z;
scanf("%d", &n);
while(n--) {
scanf("%d%d%d", &a, &b, &c);
int xmax = sqrt(c) ;
int flag = 0;
for(x = -xmax; x <= xmax; x++) {
int x2 = x * x;
if(x2 <= c) {
for(y = x + 1; y <= xmax; y++) {
int y2 = y * y;
if(x2 + y2 <= c) {
z = a - x - y;
if(y < z && x * y * z == b && x2 + y2 + z * z == c) {
flag = 1;
break;
}
}
}
if(flag)
break;
}
}
if(flag)
printf("%d %d %d\n", x, y, z);
else
printf("No solution.\n");
}
return 0;
}
原文地址:https://www.cnblogs.com/tigerisland45/p/10357660.html