2019년 4월 13일 토요일

# 세개의 수의 합이 1000이며 피타고라스 정리를 만족하는 경우.

문제는 아래와 같다. ( 프로젝트 오일러 9번) 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. 사실 엄청 복잡하거나.. 뭐 그런 문제는 아니다. 더 좋은 솔루션은 post에 많은 것 같던데, 찾아서 정리해봐야겠다.
 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
#include <iostream>
#include <Windows.h>

using namespace std;

#define MAX_NUMBER 1000

void main()
{

 long long max_product = 0;

 // a+b+c = 1000 , b+c = 1000 -a, c = 1000 -a -b

 for (int a = 1; a <= MAX_NUMBER; ++a)
  for (int b = 1; b <= MAX_NUMBER -a; ++b)
   for (int c = 1; c <= MAX_NUMBER -a -b; ++c)
   {
    if (a + b + c == 1000)
    {
     long long squareA = a * a;
     long long squareB = b * b;
     long long squareC = c * c;

     if ((squareA + squareB) == squareC) max_product = a * b * c;
    }
   }

 cout << max_product << endl;

 system("pause");
}

댓글 없음:

댓글 쓰기