📜  可以刻在较大圆圈中的较小圆圈的数量

📅  最后修改于: 2021-04-18 02:33:28             🧑  作者: Mango

给定两个正整数R1R2 ,其中R1R2分别代表较大和较小圆的半径,任务是找出可以放置在较大圆内的较小圆的数量,以使较小的圆接触边界较大的圆圈。

例子:

方法:可以通过找到半径为R2的较小圆在半径为R1的圆的中心处形成的角度,然后将其除以360度来解决给定的问题。
请按照以下步骤解决给定的问题:

  • 如果R1的值小于R2 ,则不可能内接一个圆。因此,打印0
  • 如果R1的值小于2 * R2 ,即较小的圆的直径大于较大的圆的半径,则只能内切一个圆。因此,打印1
  • 否则,使用以下公式找到较大圆圈中心的较小圆圈所成的角度,然后除以360度以获取可被刻写的圆圈总数并打印该值。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to count number of smaller
// circles that can be inscribed in
// the larger circle touching its boundary
int countInscribed(int R1, int R2)
{
    // If R2 is greater than R1
    if (R2 > R1)
        return 0;
 
    // Stores the angle made
    // by the smaller circle
    double angle;
 
    // Stores the ratio
    // of R2 / (R1 - R2)
    double ratio;
 
    // Stores the count of smaller
    // circles that can be inscribed
    int number_of_circles = 0;
 
    // Stores the ratio
    ratio = R2 / (double)(R1 - R2);
 
    // If the diameter of smaller
    // circle is greater than the
    // radius of the larger circle
    if (R1 < 2 * R2) {
        number_of_circles = 1;
    }
 
    // Otherwise
    else {
 
        // Find the angle using formula
        angle = abs(asin(ratio) * 180)
                / 3.14159265;
 
        // Divide 360 with angle
        // and take the floor value
        number_of_circles = 360
                            / (2
                               * floor(angle));
    }
 
    // Return the final result
    return number_of_circles;
}
 
// Driver Code
int main()
{
    int R1 = 3;
    int R2 = 1;
 
    cout << countInscribed(R1, R2);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
  
// Function to count number of smaller
// circles that can be inscribed in
// the larger circle touching its boundary
static int countInscribed(int R1, int R2)
{
     
    // If R2 is greater than R1
    if (R2 > R1)
        return 0;
 
    // Stores the angle made
    // by the smaller circle
    double angle;
 
    // Stores the ratio
    // of R2 / (R1 - R2)
    double ratio;
 
    // Stores the count of smaller
    // circles that can be inscribed
    int number_of_circles = 0;
 
    // Stores the ratio
    ratio = R2 / (double)(R1 - R2);
 
    // If the diameter of smaller
    // circle is greater than the
    // radius of the larger circle
    if (R1 < 2 * R2)
    {
        number_of_circles = 1;
    }
 
    // Otherwise
    else
    {
         
        // Find the angle using formula
        angle = Math.abs(Math.asin(ratio) * 180) /
                3.14159265;
 
        // Divide 360 with angle
        // and take the floor value
        number_of_circles = (int)(360 /
        (2 * Math.floor(angle)));
    }
     
    // Return the final result
    return number_of_circles;
}
 
// Driver Code
public static void main(String args[])
{
    int R1 = 3;
    int R2 = 1;
     
    System.out.println(countInscribed(R1, R2));
}
}
 
// This code is contributed by ipg2016107


Python3
# Python3 program for the above approach
import math
 
# Function to count number of smaller
# circles that can be inscribed in
# the larger circle touching its boundary
def countInscribed(R1, R2):
     
    # If R2 is greater than R1
    if (R2 > R1):
        return 0
 
    # Stores the angle made
    # by the smaller circle
    angle = 0
 
    # Stores the ratio
    # of R2 / (R1 - R2)
    ratio = 0
 
    # Stores the count of smaller
    # circles that can be inscribed
    number_of_circles = 0
 
    # Stores the ratio
    ratio = R2 / (R1 - R2)
 
    # If the diameter of smaller
    # circle is greater than the
    # radius of the larger circle
    if (R1 < 2 * R2):
        number_of_circles = 1
     
    # Otherwise
    else:
 
        # Find the angle using formula
        angle = (abs(math.asin(ratio) * 180) /
                 3.14159265)
        
        # Divide 360 with angle
        # and take the floor value
        number_of_circles = (360 / (2 *
               math.floor(angle)))
     
    # Return the final result
    return number_of_circles
 
# Driver Code
if __name__ == "__main__":
 
    R1 = 3
    R2 = 1
 
    print (int(countInscribed(R1, R2)))
 
# This code is contributed by ukasp


C#
// C# program for the above approach
using System;
 
class GFG{
  
// Function to count number of smaller
// circles that can be inscribed in
// the larger circle touching its boundary
static int countInscribed(int R1, int R2)
{
     
    // If R2 is greater than R1
    if (R2 > R1)
        return 0;
 
    // Stores the angle made
    // by the smaller circle
    double angle;
 
    // Stores the ratio
    // of R2 / (R1 - R2)
    double ratio;
 
    // Stores the count of smaller
    // circles that can be inscribed
    int number_of_circles = 0;
 
    // Stores the ratio
    ratio = R2 / (double)(R1 - R2);
 
    // If the diameter of smaller
    // circle is greater than the
    // radius of the larger circle
    if (R1 < 2 * R2)
    {
        number_of_circles = 1;
    }
 
    // Otherwise
    else
    {
         
        // Find the angle using formula
        angle = Math.Abs(Math.Asin(ratio) * 180) /
                3.14159265;
 
        // Divide 360 with angle
        // and take the floor value
        number_of_circles = (int)(360 /
        (2 * Math.Floor(angle)));
    }
     
    // Return the final result
    return number_of_circles;
}
 
// Driver Code
public static void Main()
{
    int R1 = 3;
    int R2 = 1;
     
    Console.WriteLine(countInscribed(R1, R2));
}
}
 
// This code is contributed by mohit kumar 29


输出:
6

时间复杂度: O(1)
辅助空间: O(1)