-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem 39
More file actions
47 lines (42 loc) · 1.25 KB
/
Copy pathproblem 39
File metadata and controls
47 lines (42 loc) · 1.25 KB
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
39
40
41
42
43
44
45
46
47
/*f p is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exactly three solutions for p = 120.
{20,48,52}, {24,45,51}, {30,40,50}
For which value of p ≤ 1000, is the number of solutions maximised?
*/
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<vector>
#include<map>
using namespace std;
void sides_less_than_1000(double side_3, int perimeter, map<int, int> & solutions){
if (side_3 == int(side_3) && perimeter < 1000){
solutions[perimeter] += 1;
//cout << solutions[perimeter] <<endl;
}
}
void fill_zeros(map<int, int> &solutions){
for(int i = 0; i < 1000; i++){
solutions.insert(pair<int,int>(i,0));
}
}
int main() {
map<int, int> solutions;
fill_zeros(solutions);
for(int side_1 = 1; side_1 < 1000; side_1++){
for(int side_2 = side_1 + 1; side_2 < 1000; side_2++){
double side_3 = pow(pow(side_2, 2) + pow(side_1, 2) , 0.5);
int perimeter = side_3 + side_2 + side_1;
sides_less_than_1000(side_3, perimeter, solutions);
}
}
int max_1 = 1, max_2;
for(int i = 0; i < solutions.size(); i++){
if(solutions[i] > max_1){
max_1 = solutions[i];
max_2 = i;
}
}
cout << max_1 << " : " << max_2 << endl;
return 0;
}