给定一个整数N和一个由高度H组成的等腰三角形,任务是在三角形上找到(N – 1)个点,以便穿过这些点并与三角形底边平行的线将总面积分成N相等的部分。
例子:
Input: N = 3, H = 2
Output: 1.15 1.63
Explanation: Make cuts at point 1.15 and 1.63 as shown below:
Input: N = 2, H = 1000
Output: 70710.67
方法:可以通过观察以下属性来解决该问题:
Divide the trangle such that (xi / h)2 = i / N
=> xi = h*√(i/n)
xi = height of ith cut from the top vertex of the trangle
请按照以下步骤解决问题:
- 在[1,N – 1]范围内迭代。
- 在第i次迭代中,使用上述公式打印x i的值。
下面是上述方法的实现:
C++
// C++ Code for above approach
#include
using namespace std;
// Function to divide the isosceles triangle
// in equal parts by making N-1 cuts
// parallel to the base
void findPoint(int n, int h)
{
// Iterate over the range [1, n - 1]
for (int i = 1; i < n; i++)
printf("%.2f ", sqrt(i / (n*1.0)) * h);
}
// Driver code
int main()
{
// Given N
int n = 3;
// Given H
int h = 2;
// Function call
findPoint(n, h);
return 0;
}
// This code is contributed by mohit kumar 29
Java
// Java Code for above approach
import java.util.*;
class GFG
{
// Function to divide the isosceles triangle
// in equal parts by making N-1 cuts
// parallel to the base
static void findPoint(int n, int h)
{
// Iterate over the range [1, n - 1]
for (int i = 1; i < n; i++)
System.out.printf("%.2f ",
Math.sqrt(i / (n * 1.0)) * h);
}
// Driver code
public static void main(String[] args)
{
// Given N
int n = 3;
// Given H
int h = 2;
// Function call
findPoint(n, h);
}
}
// This code is contributed by shikhasingrajput
Python3
# Python Code for above approach
# Function to divide the isosceles triangle
# in equal parts by making N-1 cuts
# parallel to the base
def findPoint(n, h):
# Iterate over the range [1, n - 1]
for i in range(1, n):
print("{0:.2f}".format(((i / n) ** 0.5) * h), end =' ')
# Driver Code
if __name__ == '__main__':
# Given N
n = 3
# Given H
h = 2
# Function call
findPoint(n, h)
C#
// C# Code for above approach
using System;
class GFG
{
// Function to divide the isosceles triangle
// in equal parts by making N-1 cuts
// parallel to the base
static void findPoint(int n, int h)
{
// Iterate over the range [1, n - 1]
for (int i = 1; i < n; i++)
Console.Write("{0:F2} ",
Math.Sqrt(i / (n * 1.0)) * h);
}
// Driver code
public static void Main(String[] args)
{
// Given N
int n = 3;
// Given H
int h = 2;
// Function call
findPoint(n, h);
}
}
// This code is contributed by shikhasingrajput
输出:
1.15 1.63
时间复杂度: O(N)
辅助空间: O(1)