📌  相关文章
📜  找出排成一行的圆的半径,并给出第一个和最后一个圆的中心之间的距离

📅  最后修改于: 2021-10-23 08:07:18             🧑  作者: Mango

这里给出了n 个在外部相互接触的圆圈,并排成一排。给出了第一个圆和最后一个圆的中心之间的距离。圆具有等长的半径。任务是找到每个圆的半径。
例子:

Input: d = 42, n = 4
Output: The radius of each circle is 7

Input: d = 64, n = 5
Output: The radius of each circle is 8

方法
假设有n 个圆,每个圆的半径为r
让,第一个和最后一个圆之间的距离= d
从图中可以看出,
r + r + (n-2)*2r = d
2r + 2nr – 4r = d
2nr – 2r = d
所以, r = d/(2n-2)
The radius of each circle = distance between the first and last circles/(2*no.of circles -2)

C++
// C++ program to find radii of the circles
// which are lined in a row
// and distance between the
// centers of first and last circle is given
 
#include 
using namespace std;
 
void radius(int n, int d)
{
    cout << "The radius of each circle is "
         << d / (2 * n - 2) << endl;
}
 
// Driver code
int main()
{
    int d = 42, n = 4;
    radius(n, d);
    return 0;
}


Java
// Java program to find radii of the circles
// which are lined in a row
// and distance between the
// centers of first and last circle is given
import java.io.*;
 
class GFG
{
 
static void radius(int n, int d)
{
    System.out.print( "The radius of each circle is "
        +d / (2 * n - 2));
}
 
// Driver code
static public void main (String []args)
{
    int d = 42, n = 4;
    radius(n, d);
}
}
 
// This code is contributed by anuj_67..


Python3
# Python program to find radii of the circles
# which are lined in a row
# and distance between the
# centers of first and last circle is given
 
def radius(n, d):
    print("The radius of each circle is ",
        d / (2 * n - 2));
 
d = 42; n = 4;
radius(n, d);
 
 
# This code is contributed by PrinciRaj1992


C#
// C# program to find radii of the circles
// which are lined in a row
// and distance between the
// centers of first and last circle is given
using System;
 
class GFG
{
 
static void radius(int n, int d)
{
    Console.Write( "The radius of each circle is "
        +d / (2 * n - 2));
}
 
// Driver code
static public void Main ()
{
    int d = 42, n = 4;
    radius(n, d);
}
}
 
// This code is contributed by anuj_67..


Javascript


输出:
The radius of each circle is 7

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程