给定两个代表矩形的长度和宽度的整数L和B ,任务是找到可以在给定的矩形中内切而不重叠的最大可能圆的最大数量。
例子:
Input: L = 3, B = 8
Output: 2
Explanation:
From the above figure it can be clearly seen that the largest circle with a diameter of 3 cm can be inscribed in the given rectangle.
Therefore, the count of such circles is 2.
Input: L = 2, B = 9
Output: 4
方法:可以根据以下观察结果解决给定问题:
- 可以内接在矩形中的最大圆的直径将等于矩形的较小边。
- 因此,这种最大可能的圆的最大数目等于(最大边的长度)/(最小边的长度) 。
因此,根据以上观察结果,只需打印(最大边的长度)/(最小边的长度)的值即可。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count the number of
// largest circles in a rectangle
int totalCircles(int L, int B)
{
// If length exceeds breadth
if (L > B) {
// Swap to reduce length
// to smaller than breadth
int temp = L;
L = B;
B = temp;
}
// Return total count
// of circles inscribed
return B / L;
}
// Driver Code
int main()
{
int L = 3;
int B = 8;
cout << totalCircles(L, B);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG
{
// Function to count the number of
// largest circles in a rectangle
static int totalCircles(int L, int B)
{
// If length exceeds breadth
if (L > B) {
// Swap to reduce length
// to smaller than breadth
int temp = L;
L = B;
B = temp;
}
// Return total count
// of circles inscribed
return B / L;
}
// Driver Code
public static void main(String[] args)
{
int L = 3;
int B = 8;
System.out.print(totalCircles(L, B));
}
}
// This code is contributed by susmitakundugoaldanga.
Python3
# Python3 program for the above approach
# Function to count the number of
# largest circles in a rectangle
def totalCircles(L, B) :
# If length exceeds breadth
if (L > B) :
# Swap to reduce length
# to smaller than breadth
temp = L
L = B
B = temp
# Return total count
# of circles inscribed
return B // L
# Driver Code
L = 3
B = 8
print(totalCircles(L, B))
# This code is contributed by splevel62.
C#
// C# program to implement
// the above approach
using System;
public class GFG
{
// Function to count the number of
// largest circles in a rectangle
static int totalCircles(int L, int B)
{
// If length exceeds breadth
if (L > B) {
// Swap to reduce length
// to smaller than breadth
int temp = L;
L = B;
B = temp;
}
// Return total count
// of circles inscribed
return B / L;
}
// Driver Code
public static void Main(String[] args)
{
int L = 3;
int B = 8;
Console.Write(totalCircles(L, B));
}
}
// This code is contributed by souravghosh0416.
输出:
2
时间复杂度: O(1)
辅助空间: O(1)