给定两个表示矩形长和宽的整数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.
Javascript
输出:
2
时间复杂度: O(1)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。