这里给出了n 个在外部相互接触的正方形,并排成一排。给出了第一个和最后一个正方形的中心之间的距离。正方形的边长相等。任务是找到每个正方形的边。
例子:
Input: d = 42, n = 4
Output: The side of each square is 14
Input: d = 36, n = 5
Output: The side of each square is 9
方法:
假设有 n 个正方形,每个正方形的边长为a 。
让,第一个和最后一个方格之间的距离 = d
从图中可以看出,
a/2 + a/2 + (n-2)*a = d
a + na – 2a = d
na – a = d
所以, a = d/(n-1)
C++
// C++ program to find side of the squares
// which are lined in a row and distance between the
// centers of first and last squares is given
#include
using namespace std;
void radius(int n, int d)
{
cout << "The side of each square is "
<< d / (n - 1) << endl;
}
// Driver code
int main()
{
int d = 42, n = 4;
radius(n, d);
return 0;
}
Java
// Java program to find side of the squares
// which are lined in a row and distance between the
// centers of first and last squares is given
import java.io.*;
class GFG
{
static void radius(int n, int d)
{
System.out.print( "The side of each square is "
+ d / (n - 1));
}
// Driver code
public static void main (String[] args)
{
int d = 42, n = 4;
radius(n, d);
}
}
// This code is contributed by vt_m.
Python3
# Python program to find side of the squares
# which are lined in a row and distance between the
# centers of first and last squares is given
def radius(n, d):
print("The side of each square is ",
d / (n - 1));
d = 42; n = 4;
radius(n, d);
# This code contributed by PrinciRaj1992
C#
// C# program to find side of the squares
// which are lined in a row and distance between the
// centers of first and last squares is given
using System;
class GFG
{
static void radius(int n, int d)
{
Console.Write( "The side of each square is "
+ d / (n - 1));
}
// Driver code
public static void Main ()
{
int d = 42, n = 4;
radius(n, d);
}
}
// This code is contributed by anuj_67..
Javascript
输出:
The side of each square is 14
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。