-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem0085.cpp
49 lines (40 loc) · 1015 Bytes
/
problem0085.cpp
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
48
49
/*
By counting carefully it can be seen that a rectangular grid measuring 3 by 2 contains eighteen rectangles:
Although there exists no rectangular grid that contains exactly two million rectangles, find the area
of the grid with the nearest solution.
*/
#include <iostream>
using namespace std;
int numRects(int, int);
int choose2(int);
int abs(double);
int main(void) {
int min = 2000000;
double tmp;
int x, y;
for (int i = 1; i < 100; ++i) {
for (int j = 1; j < 100; ++j) {
tmp = abs(2000000 - numRects(i+1, j+1));
if (tmp < min) {
min = tmp;
x = i;
y = j;
}
}
}
cout << numRects(6, 8) << endl;
cout << min << " " << x << " " << y << endl;
cout << x * y << endl;
}
int abs(double x) {
if (x < 0)
return -1 * x;
else
return x;
}
int numRects(int x, int y) {
return choose2(x) * choose2(y);
}
int choose2(int n) {
return (n * (n-1)) / 2;
}