Cuente el número de rectángulos de modo que la proporción de los lados se encuentre en el rango [a, b]

Dada la longitud y el ancho de N rectángulos y un rango, es decir, [a, b], la tarea es contar el número de rectángulos cuya relación de lados (mayor/menor) está en el rango [a, b].
Ejemplos: 

Entrada: {{165, 100}, {180, 100}, {100, 170}}, a = 1,6, b = 1,7 
Salida:
165/100 = 1,65 
170/100 = 1,7 
Entrada: {{10, 12} , {26, 19}}, a = 0,8, b = 1,2 
Salida: 1

Enfoque: Iterar en la array de pares y aumentar el contador cuando max (a[i].primero, a[i].segundo)/ min (a[i].primero, a[i].segundo) se encuentra en el rango a y b. 
A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ program to print the length of the shortest
// subarray with all elements greater than X
#include <bits/stdc++.h>
using namespace std;
 
// Function to count the number of ratios
int countRatios(pair<int, int> arr[], int n,
                           double a, double b)
{
    int count = 0;
 
    // count the number of ratios
    // by iterating
    for (int i = 0; i < n; i++) {
 
        double large = max(arr[i].first, arr[i].second);
        double small = min(arr[i].first, arr[i].second);
 
        // find ratio
        double ratio = large / small;
 
        // check if lies in range
        if (ratio >= a && ratio <= b)
            count += 1;
    }
 
    return count;
}
 
// Driver Code
int main()
{
    pair<int, int> arr[] = { { 165, 100 },
                             { 180, 100 },
                             { 100, 170 } };
    double a = 1.6, b = 1.7;
    int n = 3;
 
    cout << countRatios(arr, n, a, b);
 
    return 0;
}

Java

// Java program to print the length of the shortest
// subarray with all elements greater than X
class GFG
{
static int n = 3;
static class pair
{
    int first, second;
    public pair(int first, int second)
    {
        this.first = first;
        this.second = second;
    }
}
 
// Function to count the number of ratios
static int countRatios(pair []arr, int n,
                       double a, double b)
{
    int count = 0;
 
    // count the number of ratios
    // by iterating
    for (int i = 0; i < n; i++)
    {
        double large = Math.max(arr[i].first,
                                arr[i].second);
        double small = Math.min(arr[i].first,
                                arr[i].second);
 
        // find ratio
        double ratio = large / small;
 
        // check if lies in range
        if (ratio >= a && ratio <= b)
            count += 1;
    }
    return count;
}
 
// Driver Code
public static void main(String[] args)
{
    pair []arr = {new pair(165, 100),
                  new pair(180, 100),
                  new pair(100, 170)};
    double a = 1.6, b = 1.7;
    int n = 3;
 
    System.out.println(countRatios(arr, n, a, b));
}
}
 
// This code is contributed by PrinciRaj1992

Python3

# Python3 program to print the
# length of the shortest subarray
# with all elements greater than X
 
# Function to count the number of
# ratios
def countRatios(arr, n, a, b):
 
    count = 0
 
    # count the number of ratios
    # by iterating
    for i in range(n):
 
        large = max(arr[i][0],
                    arr[i][1])
        small = min(arr[i][0],
                    arr[i][1])
 
        # find ratio
        ratio = large / small
 
        # check if lies in range
        if (ratio >= a and
            ratio <= b):
            count += 1
 
    return count
 
# Driver Code
if __name__ == "__main__":
 
    arr = [[165, 100],
           [180, 100],
           [100, 170]]
    a = 1.6
    b = 1.7
    n = 3
    print(countRatios(arr, n, a, b))
 
# This code is contributed by Chitranayal

C#

// C# program to print the length of the shortest
// subarray with all elements greater than X
using System;
     
class GFG
{
static int n = 3;
class pair
{
    public int first, second;
    public pair(int first, int second)
    {
        this.first = first;
        this.second = second;
    }
}
 
// Function to count the number of ratios
static int countRatios(pair []arr, int n,
                       double a, double b)
{
    int count = 0;
 
    // count the number of ratios
    // by iterating
    for (int i = 0; i < n; i++)
    {
        double large = Math.Max(arr[i].first,
                                arr[i].second);
        double small = Math.Min(arr[i].first,
                                arr[i].second);
 
        // find ratio
        double ratio = large / small;
 
        // check if lies in range
        if (ratio >= a && ratio <= b)
            count += 1;
    }
    return count;
}
 
// Driver Code
public static void Main(String[] args)
{
    pair []arr = {new pair(165, 100),
                  new pair(180, 100),
                  new pair(100, 170)};
    double a = 1.6, b = 1.7;
    int n = 3;
 
    Console.WriteLine(countRatios(arr, n, a, b));
}
}
 
// This code is contributed by 29AjayKumar

Javascript

<script>
 
// JavaScript program to print
// the length of the shortest
// subarray with all elements greater than X
 
// Function to count the number of ratios
function countRatios(arr,n,a,b)
{
    let count = 0;
  
    // count the number of ratios
    // by iterating
    for (let i = 0; i < n; i++)
    {
        let large = Math.max(arr[i][0],
                                arr[i][1]);
        let small = Math.min(arr[i][0],
                                arr[i][1]);
  
        // find ratio
        let ratio = large / small;
  
        // check if lies in range
        if (ratio >= a && ratio <= b)
            count += 1;
    }
    return count;
}
 
// Driver Code
let arr = [[165, 100],
           [180, 100],
           [100, 170]];
            
let a = 1.6, b = 1.7;
let n = 3;
document.write(countRatios(arr, n, a, b));
 
 
// This code is contributed by avanitrachhadiya2155
 
</script>
Producción: 

2

 

Publicación traducida automáticamente

Artículo escrito por swetankmodi y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *