📜  大小为K的所有子数组的比率

📅  最后修改于: 2021-04-28 00:05:00             🧑  作者: Mango

给定一个数组arr []和一个整数K ,任务是计算大小为K的所有子数组的比率。

例子:

方法:想法是遍历给定数组中存在的每个大小为K的子数组,并按照以下步骤解决问题:

  1. 用子数组的第一个元素初始化一个变量,比如说比率
  2. 迭代的其余子阵列,并保持由上所遇到的元件逐个分频
  3. 最后,打印该子数组的ratio的最终值。
  4. 对所有子数组重复上述步骤。

下面是上述方法的实现:

C++
// C++ implementation of the above approach
 
#include 
using namespace std;
 
// Function to find the ratio of
// all subarrays of size K
int calcRatio(double arr[], int n, int k)
{
 
    // Traverse every subarray of size K
    for (int i = 0; i <= n - k; i++) {
 
        // Initialize ratio
        double ratio = arr[i];
 
        // Calculate ratio of the
        // current subarray
        for (int j = i + 1; j < k + i; j++)
            ratio /= arr[j];
 
        // Print ratio of the subarray
        cout << ratio << " ";
    }
}
 
// Driver Code
int main()
{
    // Given array
    double arr[] = { 24, 3, 2, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 3;
 
    // Function Call
    calcRatio(arr, n, k);
 
    return 0;
}


Java
// Java implementation of the above approach
import java.util.*;
 
class GFG{
   
// Function to find the ratio of
// all subarrays of size K
static void calcRatio(double arr[], int n,
                                    int k)
{
     
    // Traverse every subarray of size K
    for(int i = 0; i <= n - k; i++)
    {
   
        // Initialize ratio
        double ratio = arr[i];
   
        // Calculate ratio of the
        // current subarray
        for(int j = i + 1; j < k + i; j++)
            ratio /= arr[j];
   
        // Print ratio of the subarray
        System.out.print(ratio + " ");
    }
}
  
// Driver code
public static void main (String[] args)
{
     
    // Given array
    double arr[] = { 24, 3, 2, 1 };
    int n = arr.length;
    int k = 3;
     
    // Function call
    calcRatio(arr, n, k);
}
}
 
// This code is contributed by offbeat


Python3
# Python3 implementation
# of the above approach
 
# Function to find the ratio of
# all subarrays of size K
def calcRatio(arr, n, k):
 
    # Traverse every subarray
    # of size K
    for i in range(n - k + 1):
 
        # Initialize ratio
        ratio = arr[i]
 
        # Calculate ratio of the
        # current subarray
        for j in range(i + 1, k + i):
            ratio = ratio / arr[j]
 
        # Print ratio of the subarray
        print(ratio, end = " ")
 
# Given array
arr = [24, 3, 2, 1]
n = len(arr)
k = 3
 
# Function Call
calcRatio(arr, n, k)
 
# This code is contributed by divyeshrabadiya07


C#
// C# implementation of the above approach
using System;
  
class GFG{
    
// Function to find the ratio of
// all subarrays of size K
static void calcRatio(double []arr, int n,
                                    int k)
{
   
    // Traverse every subarray of size K
    for(int i = 0; i <= n - k; i++)
    {
    
        // Initialize ratio
        double ratio = arr[i];
    
        // Calculate ratio of the
        // current subarray
        for(int j = i + 1; j < k + i; j++)
            ratio /= arr[j];
    
        // Print ratio of the subarray
        Console.Write(ratio + " ");
    }
}
   
// Driver code
public static void Main(string[] args)
{
      
    // Given array
    double []arr = { 24, 3, 2, 1 };
    int n = arr.Length;
    int k = 3;
      
    // Function call
    calcRatio(arr, n, k);
}
}
 
// This code is contributed by rutvik_56


Javascript


输出
4 1.5 

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