📌  相关文章
📜  形成直角三角形的斜边和面积的可能对数

📅  最后修改于: 2021-10-23 09:06:44             🧑  作者: Mango

给定两个数组 H 和 S。数组 H[] 包含斜边的长度,数组 S[] 包含直角三角形的面积。任务是找到所有可能的 (H, S) 对,这样我们就可以构造一个斜边为 H 且面积为 S 的直角三角形。
例子

Input : H[] = {1, 6, 4}  ;  S[] = {23, 3, 42, 14}
Output : 2 
Possible pairs are {6, 3} {4, 3}

Input : H[] = {1, 6, 4, 3}  ;  S[] = {23, 3, 42, 5}
Output : 3
Possible pairs are {6, 3} {6, 5} {4, 3}

说,
a     = 直角三角形的底
b     = 直角三角形的高度
所以,

area S = (a*b)/2
or, 4*S*S=a*a*b*b

还,

a2 + b2 = H2

所以,

4*S2 = a2(H2-a2)

2 中求解这个二次方程,并将判别式>=0(a 存在的条件)。我们将获得,

H2 >= 4*S 

For a right-angled triangle to exist with 
hypotenuse H and area S.

朴素的方法:朴素的方法是找到所有可能的 (H,S) 对并检查它们是否满足条件H 2 >= 4*S 。计算满足此条件的对数并打印计数。
下面是朴素方法的实现:

C++
#include 
using namespace std;
 
// Function to check the condition
bool check(int H, int S)
{
    // Condition for triangle to exist
    return H * H >= 4 * S;
}
 
// Function to find all pairs
int findPairs(int H[], int n, int S[], int m)
{
    int count = 0;
 
    // Checking all possible pairs
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (check(H[i], S[j]))
                count++;
        }
    }
 
    return count;
}
 
// Driver code
int main()
{
    int H[] = { 1, 6, 4 };
    int n = sizeof(H)/sizeof(H[0]);
     
    int S[] = { 23, 3, 42, 14 };
    int m = sizeof(S)/sizeof(S[0]);
     
    cout<


Java
class GFG
{
 
// Function to check the condition
static boolean check(int H, int S)
{
    // Condition for triangle to exist
    return H * H >= 4 * S;
}
 
// Function to find all pairs
static int findPairs(int H[], int n,
                     int S[], int m)
{
    int count = 0;
 
    // Checkinag all possible pairs
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (check(H[i], S[j]))
                count++;
        }
    }
 
    return count;
}
 
// Driver code
public static void main(String args[])
{
    int H[] = { 1, 6, 4 };
    int n = H.length;
     
    int S[] = { 23, 3, 42, 14 };
    int m = S.length;
     
    System.out.println(findPairs(H, n, S, m));
}
}
 
// This code is contributed
// by ankita_saini


Python3
# Python 3 implementation
# of above approach
 
# Function to check the condition
def check(H, S) :
 
    # Condition for triangle to exist
    return H * H >= 4 * S
 
# Function to find all pairs
def findPairs(H, n, S, m):
 
    count = 0
 
    # Checking all possible pairs
    for i in range(n) :
        for j in range(m) :
            if check(H[i], S[j]) :
                count += 1
 
    return count
 
# Driver Code
if __name__ == "__main__" :
 
    H = [ 1, 6, 4]
    n = len(H)
 
    S = [ 23, 3, 42, 14]
    m = len(S)
 
    # function calling
    print(findPairs(H, n, S,m))
     
# This code is contributed by ANKITRAI1


C#
// C# implementation of above approach
using System;
 
class GFG
{
 
// Function to check the condition
static bool check(int H, int S)
{
    // Condition for triangle to exist
    return H * H >= 4 * S;
}
 
// Function to find all pairs
static int findPairs(int[] H, int n,
                      int[] S, int m)
{
    int count = 0;
 
    // Checkinag all possible pairs
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (check(H[i], S[j]))
                count++;
        }
    }
 
    return count;
}
 
// Driver code
public static void Main()
{
    int[] H = { 1, 6, 4 };
    int n = H.Length;
     
    int[] S = { 23, 3, 42, 14 };
    int m = S.Length;
     
    Console.Write(findPairs(H, n, S, m));
}
}
 
// This code is contributed
// by ChitraNayal


PHP
= 4 * $S;
}
 
// Function to find all pairs
function findPairs($H, $n, $S, $m)
{
    $count = 0;
 
    // Checking all possible pairs
    for ($i = 0; $i < $n; $i++)
    {
        for ($j = 0; $j < $m; $j++)
        {
            if (check($H[$i], $S[$j]))
                $count++;
        }
    }
 
    return $count;
}
 
// Driver code
$H = array( 1, 6, 4 );
$n = count($H);
 
$S = array( 23, 3, 42, 14 );
$m = count($S);
 
echo findPairs($H, $n, $S, $m);
     
// This code is contributed by mits
?>


Javascript


C++
#include 
using namespace std;
 
// Function to check the condition
bool check(int H, int S)
{
    // Condition for triangle to exist
    return H * H >= 4 * S;
}
 
// Function to find all pairs
int findPairs(int H[], int n, int S[], int m)
{
    int count = 0;
     
    // Sort both the arrays
    sort(H, H + n);
    sort(S, S + m);
 
    // To keep track of last possible Area
    int index = -1;
     
    for (int i = 0; i < n; i++) {
        // Apply Binary Search for
        // each Hypotenuse Length
        int start = 0;
        int end = m - 1;
         
        while (start <= end) {
            int mid = start + (end - start) / 2;
            if (check(H[i], S[mid])) {
                index = mid;
                start = mid + 1;
            }
            else {
                end = mid - 1;
            }
        }
         
        // Check if we get any
        // possible Area or Not
        if (index != -1) {
            // All area less than area[index]
            // satisfy property
            count += index + 1;
        }
    }
 
    return count;
}
 
// Driver code
int main()
{
    int H[] = { 1, 6, 4 };
    int n = sizeof(H)/sizeof(H[0]);
     
    int S[] = { 23, 3, 42, 14 };
    int m = sizeof(S)/sizeof(S[0]);
     
    cout<


Java
/*package whatever //do not write package name here */
import java.util.Arrays;
import java.io.*;
 
class GFG {
     
// Function to check the condition
static boolean check(int H, int S)
{
    // Condition for triangle to exist
    return H * H >= 4 * S;
}
 
// Function to find all pairs
static int findPairs(int H[], int n, int S[], int m)
{
    int count = 0;
     
    // Sort both the arrays
    Arrays.sort(H);
    Arrays.sort(S);
 
    // To keep track of last possible Area
    int index = -1;
     
    for (int i = 0; i < n; i++) {
        // Apply Binary Search for
        // each Hypotenuse Length
        int start = 0;
        int end = m - 1;
         
        while (start <= end) {
            int mid = start + (end - start) / 2;
            if (check(H[i], S[mid])) {
                index = mid;
                start = mid + 1;
            }
            else {
                end = mid - 1;
            }
        }
         
        // Check if we get any
        // possible Area or Not
        if (index != -1) {
            // All area less than area[index]
            // satisfy property
            count += index + 1;
        }
    }
 
    return count;
}
 
// Driver code
    public static void main (String[] args) {
         
    int H[] = { 1, 6, 4 };
    int n = H.length;
     
    int S[] = { 23, 3, 42, 14 };
    int m = S.length;
     
    System.out.println(findPairs(H, n, S, m));
    }
     
// This code is contributed
// by ajit...
}


Python3
# Function to check the condition
def check(H, S):
     
    # Condition for triangle to exist
    return H * H >= 4 * S;
 
# Function to find all pairs
def findPairs(H, n, S, m):
    count = 0;
     
    # Sort both the arrays
    H.sort();
    S.sort();
 
    # To keep track of last possible Area
    index = -1;
     
    for i in range(n):
         
        # Apply Binary Search for
        # each Hypotenuse Length
        start = 0;
        end = m - 1;
         
        while (start <= end):
            mid = int(start + (end - start) / 2);
            if (check(H[i], S[mid])):
                index = mid;
                start = mid + 1;
            else:
                end = mid - 1;
         
        # Check if we get any possible
        # Area or Not
        if (index != -1):
             
            # All area less than area[index]
            # satisfy property
            count += index + 1;
 
    return count;
 
# Driver code
H = [ 1, 6, 4 ];
n = len(H);
 
S= [ 23, 3, 42, 14 ];
m = len(S);
 
print(findPairs(H, n, S, m));
 
# This code is contributed by mits


C#
/*package whatever //do not write package name here */
 
using System;
 
public class GFG{
         
// Function to check the condition
static bool check(int H, int S)
{
    // Condition for triangle to exist
    return H * H >= 4 * S;
}
 
// Function to find all pairs
static int findPairs(int []H, int n, int []S, int m)
{
    int count = 0;
     
    // Sort both the arrays
    Array.Sort(H);
    Array.Sort(S);
 
    // To keep track of last possible Area
    int index = -1;
     
    for (int i = 0; i < n; i++) {
        // Apply Binary Search for
        // each Hypotenuse Length
        int start = 0;
        int end = m - 1;
         
        while (start <= end) {
            int mid = start + (end - start) / 2;
            if (check(H[i], S[mid])) {
                index = mid;
                start = mid + 1;
            }
            else {
                end = mid - 1;
            }
        }
         
        // Check if we get any
        // possible Area or Not
        if (index != -1) {
            // All area less than area[index]
            // satisfy property
            count += index + 1;
        }
    }
 
    return count;
}
 
// Driver code
    static public void Main (){
        int []H = { 1, 6, 4 };
    int n = H.Length;
     
    int []S = { 23, 3, 42, 14 };
    int m = S.Length;
     
    Console.WriteLine(findPairs(H, n, S, m));
    }
     
// This code is contributed
// by  akt_mit...
}


PHP
= 4 * $S;
}
 
// Function to find all pairs
function findPairs($H, $n, $S, $m)
{
    $count = 0;
     
    // Sort both the arrays
    sort($H);
    sort($S);
 
    // To keep track of last possible Area
    $index = -1;
     
    for ($i = 0; $i < $n; $i++)
    {
         
        // Apply Binary Search for
        // each Hypotenuse Length
        $start = 0;
        $end = $m - 1;
         
        while ($start <= $end)
        {
            $mid = $start + (int)($end - $start) / 2;
            if (check($H[$i], $S[$mid]))
            {
                $index = $mid;
                $start = $mid + 1;
            }
            else
            {
                $end = $mid - 1;
            }
        }
         
        // Check if we get any possible
        // Area or Not
        if ($index != -1)
        {
            // All area less than area[index]
            // satisfy property
            $count += $index + 1;
        }
    }
 
    return $count;
}
 
// Driver code
$H = array( 1, 6, 4 );
$n = sizeof($H);
 
$S = array(23, 3, 42, 14 );
$m = sizeof($S);
 
echo findPairs($H, $n, $S, $m);
 
// This code is contributed by Sach_Code
?>


Javascript


输出:
2

有效的方法:一种有效的方法是以递增的顺序对可用的数组进行排序。然后,对于斜边的每个可能长度,应用二分搜索找到满足必要条件的最大面积。
比如说,在二进制搜索之后,最大可能区域可用在数组 S[] 的索引 4 处。然后我们可以形成 4 个这样的可能对,因为所有小于索引 4 的区域也将满足条件。
下面是高效方法的实现:

C++

#include 
using namespace std;
 
// Function to check the condition
bool check(int H, int S)
{
    // Condition for triangle to exist
    return H * H >= 4 * S;
}
 
// Function to find all pairs
int findPairs(int H[], int n, int S[], int m)
{
    int count = 0;
     
    // Sort both the arrays
    sort(H, H + n);
    sort(S, S + m);
 
    // To keep track of last possible Area
    int index = -1;
     
    for (int i = 0; i < n; i++) {
        // Apply Binary Search for
        // each Hypotenuse Length
        int start = 0;
        int end = m - 1;
         
        while (start <= end) {
            int mid = start + (end - start) / 2;
            if (check(H[i], S[mid])) {
                index = mid;
                start = mid + 1;
            }
            else {
                end = mid - 1;
            }
        }
         
        // Check if we get any
        // possible Area or Not
        if (index != -1) {
            // All area less than area[index]
            // satisfy property
            count += index + 1;
        }
    }
 
    return count;
}
 
// Driver code
int main()
{
    int H[] = { 1, 6, 4 };
    int n = sizeof(H)/sizeof(H[0]);
     
    int S[] = { 23, 3, 42, 14 };
    int m = sizeof(S)/sizeof(S[0]);
     
    cout<

Java

/*package whatever //do not write package name here */
import java.util.Arrays;
import java.io.*;
 
class GFG {
     
// Function to check the condition
static boolean check(int H, int S)
{
    // Condition for triangle to exist
    return H * H >= 4 * S;
}
 
// Function to find all pairs
static int findPairs(int H[], int n, int S[], int m)
{
    int count = 0;
     
    // Sort both the arrays
    Arrays.sort(H);
    Arrays.sort(S);
 
    // To keep track of last possible Area
    int index = -1;
     
    for (int i = 0; i < n; i++) {
        // Apply Binary Search for
        // each Hypotenuse Length
        int start = 0;
        int end = m - 1;
         
        while (start <= end) {
            int mid = start + (end - start) / 2;
            if (check(H[i], S[mid])) {
                index = mid;
                start = mid + 1;
            }
            else {
                end = mid - 1;
            }
        }
         
        // Check if we get any
        // possible Area or Not
        if (index != -1) {
            // All area less than area[index]
            // satisfy property
            count += index + 1;
        }
    }
 
    return count;
}
 
// Driver code
    public static void main (String[] args) {
         
    int H[] = { 1, 6, 4 };
    int n = H.length;
     
    int S[] = { 23, 3, 42, 14 };
    int m = S.length;
     
    System.out.println(findPairs(H, n, S, m));
    }
     
// This code is contributed
// by ajit...
}

蟒蛇3

# Function to check the condition
def check(H, S):
     
    # Condition for triangle to exist
    return H * H >= 4 * S;
 
# Function to find all pairs
def findPairs(H, n, S, m):
    count = 0;
     
    # Sort both the arrays
    H.sort();
    S.sort();
 
    # To keep track of last possible Area
    index = -1;
     
    for i in range(n):
         
        # Apply Binary Search for
        # each Hypotenuse Length
        start = 0;
        end = m - 1;
         
        while (start <= end):
            mid = int(start + (end - start) / 2);
            if (check(H[i], S[mid])):
                index = mid;
                start = mid + 1;
            else:
                end = mid - 1;
         
        # Check if we get any possible
        # Area or Not
        if (index != -1):
             
            # All area less than area[index]
            # satisfy property
            count += index + 1;
 
    return count;
 
# Driver code
H = [ 1, 6, 4 ];
n = len(H);
 
S= [ 23, 3, 42, 14 ];
m = len(S);
 
print(findPairs(H, n, S, m));
 
# This code is contributed by mits

C#

/*package whatever //do not write package name here */
 
using System;
 
public class GFG{
         
// Function to check the condition
static bool check(int H, int S)
{
    // Condition for triangle to exist
    return H * H >= 4 * S;
}
 
// Function to find all pairs
static int findPairs(int []H, int n, int []S, int m)
{
    int count = 0;
     
    // Sort both the arrays
    Array.Sort(H);
    Array.Sort(S);
 
    // To keep track of last possible Area
    int index = -1;
     
    for (int i = 0; i < n; i++) {
        // Apply Binary Search for
        // each Hypotenuse Length
        int start = 0;
        int end = m - 1;
         
        while (start <= end) {
            int mid = start + (end - start) / 2;
            if (check(H[i], S[mid])) {
                index = mid;
                start = mid + 1;
            }
            else {
                end = mid - 1;
            }
        }
         
        // Check if we get any
        // possible Area or Not
        if (index != -1) {
            // All area less than area[index]
            // satisfy property
            count += index + 1;
        }
    }
 
    return count;
}
 
// Driver code
    static public void Main (){
        int []H = { 1, 6, 4 };
    int n = H.Length;
     
    int []S = { 23, 3, 42, 14 };
    int m = S.Length;
     
    Console.WriteLine(findPairs(H, n, S, m));
    }
     
// This code is contributed
// by  akt_mit...
}

PHP

= 4 * $S;
}
 
// Function to find all pairs
function findPairs($H, $n, $S, $m)
{
    $count = 0;
     
    // Sort both the arrays
    sort($H);
    sort($S);
 
    // To keep track of last possible Area
    $index = -1;
     
    for ($i = 0; $i < $n; $i++)
    {
         
        // Apply Binary Search for
        // each Hypotenuse Length
        $start = 0;
        $end = $m - 1;
         
        while ($start <= $end)
        {
            $mid = $start + (int)($end - $start) / 2;
            if (check($H[$i], $S[$mid]))
            {
                $index = $mid;
                $start = $mid + 1;
            }
            else
            {
                $end = $mid - 1;
            }
        }
         
        // Check if we get any possible
        // Area or Not
        if ($index != -1)
        {
            // All area less than area[index]
            // satisfy property
            $count += $index + 1;
        }
    }
 
    return $count;
}
 
// Driver code
$H = array( 1, 6, 4 );
$n = sizeof($H);
 
$S = array(23, 3, 42, 14 );
$m = sizeof($S);
 
echo findPairs($H, $n, $S, $m);
 
// This code is contributed by Sach_Code
?>

Javascript


输出:
2

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