这里给出的是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