给定一个半径为R的圆形薄板,任务是查找可以从圆形薄板上切下的具有整数长度和宽度的矩形的总数,一次一次。
例子:
Input: R = 2
Output: 8
8 rectangles can be cut from a circular sheet of radius 2.
These are: 1×1, 1×2, 2×1, 2×2, 1×3, 3×1, 2×3, 3×2.
Input: R = 1
Output: 1
Only one rectangle with dimensions 1 X 1 is possible.
方法
考虑下图,
不难看出,ABCD是可以在给定圆中以半径R和中心O形成的最大矩形,尺寸为a X b
放下垂直AO,使∠AOD=∠AOB= 90°
考虑下图以进行进一步分析,
Consider triangles AOD and AOB,
In these triangles,
AO = AO (Common Side)
∠AOD = ∠AOB = 90°
OD = OB = R
Thus, by SAS congruence ▵AOD ≅ ▵AOB
∴ AD = AB by CPCT(i.e Corresponding Parts on Congruent Triangles)
or, a = b
=> The rectangle ABCD is a square
直径BD是矩形必须能够从“圆片”中切出的最大对角线。
因此,可以检查a和b的所有组合以形成所有可能的矩形,并且如果任何此类矩形的对角线小于或等于所形成的最大矩形的对角线长度(即2 * R,其中R是如上所述的圆的半径)
现在,a和b的最大长度将始终严格小于圆的直径,因此a和b的所有可能值将位于闭合区间[1,(2 * R – 1)]。
下面是上述方法的实现:
C++
// C++ program to find the number of rectangles
// that can be cut from a circle of Radius R
#include
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;
}
Java
// Java program to find the
// number of rectangles that
// can be cut from a circle
// of Radius R
import java.io.*;
class GFG
{
// Function to return
// the total possible
// rectangles that can
// be cut from the circle
static 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
public static void main (String[] args)
{
// Radius of the circle
int radius = 2;
int totalRectangles;
totalRectangles = countRectangles(radius);
System.out.println(totalRectangles +
" rectangles can be " +
"cut from a circle of" +
" Radius " + radius);
}
}
// This code is contributed
// by anuj_67.
Python3
# Python3 program to find
# the number of rectangles
# that can be cut from a
# circle of Radius R Function
# to return the total possible
# rectangles that can be cut
# from the circle
def countRectangles(radius):
rectangles = 0
# Diameter = 2 * Radius
diameter = 2 * radius
# Square of diameter which
# is the square of the
# maximum length diagnal
diameterSquare = diameter * diameter
# generate all combinations
# of a and b in the range
# (1, (2 * Radius - 1))(Both inclusive)
for a in range(1, 2 * radius):
for b in range(1, 2 * radius):
# Calculate the Diagnal
# length of this rectange
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 += 1
return rectangles
# Driver Code
# Radius of the circle
radius = 2
totalRectangles = countRectangles(radius)
print(totalRectangles , "rectangles can be" ,
"cut from a circle of Radius" , radius)
# This code is contributed by Smita
C#
// C# program to find the
// number of rectangles that
// can be cut from a circle
// of Radius R
using System;
class GFG
{
// Function to return
// the total possible
// rectangles that can
// be cut from the circle
static 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
public static void Main ()
{
// Radius of the circle
int radius = 2;
int totalRectangles;
totalRectangles = countRectangles(radius);
Console.WriteLine(totalRectangles +
" rectangles can be " +
"cut from a circle of" +
" Radius " + radius);
}
}
// This code is contributed
// by anuj_67.
PHP
Javascript
输出:
8 rectangles can be cut from a circle of Radius 2
时间复杂度: O(R 2 ),其中R是圆的半径