UVA11565 Simple Equations数学+暴力

Posted tigerisland45

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA11565 Simple Equations数学+暴力相关的知识,希望对你有一定的参考价值。

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
", x, y, z);
        else
            printf("No solution.
");
    }

    return 0;
}

以上是关于UVA11565 Simple Equations数学+暴力的主要内容,如果未能解决你的问题,请参考以下文章

UVA10912 Simple Minded HashingDP

UVA10994 Simple Addition前缀和

UVA11532 Simple Adjacency Maximization位运算

python dbhelper(simple orm)

UVA12253 简单加密法 Simple Encryption

hdu 1496 Equations