Algorithm, Data structure/Solved Algorithmic Problem

Project Euler #9 - Special Pythagorean triplet

JaykayChoi 2016. 6. 12. 20:33

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

a2 + b2 = c2

For example, 32 + 42 = 9 + 16 = 25 = 52.

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


출처: https://projecteuler.net/



피타고라스의 정리를 만족하는 a b c가 있다고 할 때 a + b + c = 1000 인 피타고라스 수 a, b, c 를 구하는 문제입니다. a*b*c 를 출력하면 됩니다. 

완전 탐색으로 풀 수 있는 문제네요.




my solving

c++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
typedef long long ll;
 
 
int main() 
{
    const int sum = 1000;
 
    int ret = 0;
    for (int a = 1; a < sum; a++)
    {
        for (int b = a + 1; b < sum; b++)
        {
            for (int c = b + 1; c < sum; c++)
            {
                if (a + b + c != sum)
                    continue;
                if (c * c == a * a + b * b)
                {
                    ret = a * b * c;
                    break;
                }
            }
            if (ret != 0)
                break;
        }
        if (ret != 0)
            break;
    }
 
    cout << ret << endl;
 
    system("pause");
    return 0;
}
cs