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©的范围内,而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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值