forked from matthewsamuel95/ACM-ICPC-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRectangle_in_circle.cpp
54 lines (42 loc) · 1.41 KB
/
Rectangle_in_circle.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
50
51
52
53
54
// C++ program to find the number of rectangles
// that can be cut from a circle of Radius R
#include <bits/stdc++.h>
using namespace std;
// Function to return the total possible
// rectangles that can be cut from the circle
int countRectangles(int radius)
{
int rectangles = 0;
// Diameter = 2 * Radius
int diameter = 2 * radius;
// Square of diameter which is the square
// of the maximum length diagnal
int diameterSquare = diameter * diameter;
// generate all combinations of a and b
// in the range (1, (2 * Radius - 1))(Both inclusive)
for (int a = 1; a < 2 * radius; a++) {
for (int b = 1; b < 2 * radius; b++) {
// Calculate the Diagnal length of
// this rectange
int diagnalLengthSquare = (a * a + b * b);
// If this rectangle's Diagonal Length
// is less than the Diameter, it is a
// valid rectangle, thus increment counter
if (diagnalLengthSquare <= diameterSquare) {
rectangles++;
}
}
}
return rectangles;
}
// Driver Code
int main()
{
// Radius of the circle
int radius = 2;
int totalRectangles;
totalRectangles = countRectangles(radius);
cout << totalRectangles << " rectangles can be"
<< "cut from a circle of Radius " << radius;
return 0;
}