📜  计数岩石样本| TCS Codevita 2020

📅  最后修改于: 2021-05-05 02:13:32             🧑  作者: Mango

约翰是地质学家,他需要对岩石样品进行计数才能将其发送到化学实验室。他有问题。实验室仅接受大小范围为ppm(百万分之一)的岩石样品。约翰需要您的帮助。您的任务是开发一个程序,以获取实验室接受的每个范围内的岩石数量。

问题陈述:给定的阵列样品[]表示岩石样品的尺寸和2D阵列范围[],任务是计数岩石样品是在范围范围[I] [0]至范围[I] [1] ,对于每个可能的1 <= i <= N。

例子:

方法:想法是为每个范围[i]迭代sample []并计算位于指定范围内的样本数。请按照以下步骤解决问题:

  • 遍历数组范围[]。
  • 对于每行range [i] ,遍历数组samples []并计算在[ranges [i] [0],ranges [i] [1]]范围内的岩石样本数。

下面是上述方法的实现:

C++
// C++ program of the
// above approach
#include
using namespace std;
 
void findRockSample(vector>ranges,
             int n, int r, vectorarr)
{
    vectora;
     
    // Iterate over the ranges
    for(int i = 0; i < r; i++)
    {
       int  c = 0;
       int l = ranges[i][0];
       int h = ranges[i][1];
        
       for(int j = 0; j < arr.size(); j++)
       {
           if (l <= arr[j] && arr[j] <= h)
               c += 1;
        }
        a.push_back(c);
    }
    for(auto i:a)
        cout << i << " ";
}
 
// Driver Code
int main()
{
    int n = 5;
    int r = 2;
     
    vectorarr = { 400, 567, 890, 765, 987 };
    vector>ranges = { { 300, 380 },
                                  { 800, 1000 } };
     
    // Function call
    findRockSample(ranges, n, r, arr);
}
 
// This code is contributed by Stream_Cipher


Java
// Java program of the
// above approach
import java.util.*;
import java.io.*;
 
class GFG{
     
// Function to find the rock
// samples in the ranges
static ArrayListfindRockSample(int ranges[][],
                                        int n, int r,
                                        int  arr[])
{
    ArrayList a = new ArrayList<>();
     
    // Iterate over the ranges
    for(int i = 0; i < r; i++)
    {
       int  c = 0;
       int l = ranges[i][0];
       int h = ranges[i][1];
        
       for(int j = 0; j < arr.length; j++)
       {
           if (l <= arr[j] && arr[j] <= h)
               c += 1;
        }
        a.add(c);
    }
    return a;
}
 
// Driver Code
public static void main(String args[])
{
    int n = 5;
    int r = 2;
    int arr[] = { 400, 567, 890, 765, 987 };
    int ranges[][] = { { 300, 380 }, { 800, 1000 } };
     
    ArrayList answer = new ArrayList<>();
     
    // Function call
    answer = findRockSample(ranges, n, r, arr);
 
    for(int i = 0; i < answer.size(); i++)
        System.out.print(answer.get(i) + " ");
         
    System.out.println();
}
}
 
// This code is contributed by bikram2001jha


Python3
# Python3 program of the
# above approach
 
# Function to find the rock
# samples in the ranges
def findRockSample(ranges,
                   n, r, arr):
    a = []
 
# Iterate over the ranges
    for i in range(r):
        c = 0
        l, h = ranges[i][0], ranges[i][1]
        for val in arr:
            if l <= val <= h:
                c += 1
        a.append(c)
    return a
 
 
# Driver Code
if __name__ == "__main__":
    n = 5
    r = 2
    arr = [400, 567, 890, 765, 987]
    ranges = [[300, 380], [800, 1000]]
 
# Function Call
    print(*findRockSample(ranges, n, r, arr))


C#
// C# program of the
// above approach
using System.Collections.Generic;
using System;
 
class GFG{
     
// Function to find the rock
// samples in the ranges
static void findRockSample(int [,]ranges,
                           int n, int r,
                           int [] arr)
{
    List a = new List();
     
    // Iterate over the ranges
    for(int i = 0; i < r; i++)
    {
        int  c = 0;
        int l = ranges[i, 0];
        int h = ranges[i, 1];
 
        for(int j = 0; j < arr.Length; j++)
        {
            if (l <= arr[j] && arr[j] <= h)
                c += 1;
        }
        a.Add(c);
    }
    foreach (var i in a)
    {
        Console.Write(i + " ");
    }
}
 
// Driver Code
public static void Main()
{
    int n = 5;
    int r = 2;
     
    int []arr = { 400, 567, 890, 765, 987 };
    int [,]ranges = { { 300, 380 },
                      { 800, 1000 } };
     
    // Function call
    findRockSample(ranges, n, r, arr);
}
}
 
// This code is contributed by Stream_Cipher


输出:
0 2












时间复杂度: O(N 2 )
辅助空间: O(1)