zoukankan      html  css  js  c++  java
  • 欧拉计划第9题题解

    Special Pythagorean triplet

    A Pythagorean triplet is a set of three natural numbers, (a < b < c) , for which,

    (a^2 + b^2 = c^2)

    For example, (3^2 + 4^2 = 9 + 16 = 25 = 5^2) .

    There exists exactly one Pythagorean triplet for which (a + b + c = 1000) .Find the product (abc).

    特殊毕达哥拉斯三元组

    毕达哥拉斯三元组是三个自然数 (a < b < c) 组成的集合,并满足

    (a^2 + b^2 = c^2)
    例如,(3^2 + 4^2 = 9 + 16 = 25 = 5^2)

    有且只有一个毕达哥拉斯三元组满足 (a + b + c = 1000)。求这个三元组的乘积 (abc)

    解题思路

    没有想到比较好的办法,尝试枚举找出答案。

    实现代码如下:

    #include <bits/stdc++.h>
    using namespace std;
    int main() {
        for (int i = 1; i <= 1000/3; i ++) {
            for (int j = i; j < 1000-i-j; j ++) {
                int k = 1000 - i - j;
                if (i*i + j*j == k*k) {
                    // cout << i << ", " << j << ", " << k << endl;
                    cout << i * j * k << endl;
                }
            }
        }
        return 0;
    }
    

    运行后发现这对三元组为 (200, 375, 425),答案为 (31875000)

  • 相关阅读:
    Python3.x基础学习-静态方法和类方法
    Python3.x基础学习-@property
    Python3.x基础学习-装饰器
    SpringMVC学习(七)
    SpringMVC学习(六)
    SpringMVC学习(五)
    整合SSM(二)
    整合SSM(一)
    SpringMVC学习(四)
    SpringMVC学习(三)
  • 原文地址:https://www.cnblogs.com/quanjun/p/12322970.html
Copyright © 2011-2022 走看看