📌  相关文章
📜  可以被当前元素整除的左侧元素的计数

📅  最后修改于: 2021-04-29 07:39:27             🧑  作者: Mango

给定一个由N个整数组成的数组A [] ,任务是生成一个数组B [] ,使得B [i]包含A []中索引j的计数,从而j A [j]%A [i ] = 0

例子:

方法:从数组的第一个元素到最后一个元素运行一个循环,对于当前元素,对其左侧的元素运行另一个循环,并检查其左侧有多少个元素可被其整除。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Utility function to print the
// elements of the array
void printArr(int arr[], int n)
{
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
}
  
// Function to generate and print
// the required array
void generateArr(int A[], int n)
{
    int B[n];
  
    // For every element of the array
    for (int i = 0; i < n; i++) {
  
        // To store the count of elements
        // on the left that the current
        // element divides
        int cnt = 0;
        for (int j = 0; j < i; j++) {
            if (A[j] % A[i] == 0)
                cnt++;
        }
        B[i] = cnt;
    }
  
    // Print the generated array
    printArr(B, n);
}
  
// Driver code
int main()
{
    int A[] = { 3, 5, 1 };
    int n = sizeof(A) / sizeof(A[0]);
  
    generateArr(A, n);
  
    return 0;
}


Java
// Java implementation of above approach
class GFG
{
  
// Utility function to print the
// elements of the array
static void printArr(int arr[], int n)
{
    for (int i = 0; i < n; i++)
        System.out.print(arr[i] + " ");
}
  
// Function to generate and print
// the required array
static void generateArr(int A[], int n)
{
    int []B = new int[n];
  
    // For every element of the array
    for (int i = 0; i < n; i++) 
    {
  
        // To store the count of elements
        // on the left that the current
        // element divides
        int cnt = 0;
        for (int j = 0; j < i; j++)
        {
            if (A[j] % A[i] == 0)
                cnt++;
        }
        B[i] = cnt;
    }
  
    // Print the generated array
    printArr(B, n);
}
  
// Driver code
public static void main(String args[])
{
    int A[] = { 3, 5, 1 };
    int n = A.length;
  
    generateArr(A, n);
}
}
  
// This code is contributed by PrinciRaj1992


Python3
# Python3 implementation of the approach
  
# Utility function to print the
# elements of the array
def printArr(arr, n):
  
    for i in arr:
        print(i, end = " ")
  
# Function to generate and print
# the required array
def generateArr(A, n):
    B = [0 for i in range(n)]
  
    # For every element of the array
    for i in range(n):
  
        # To store the count of elements
        # on the left that the current
        # element divides
        cnt = 0
        for j in range(i):
            if (A[j] % A[i] == 0):
                cnt += 1
  
        B[i] = cnt
  
    # Print the generated array
    printArr(B, n)
  
# Driver code
A = [3, 5, 1]
n = len(A)
  
generateArr(A, n)
  
# This code is contributed by Mohit Kumar


C#
// C# implementation of above approach
using System;
      
class GFG
{
  
// Utility function to print the
// elements of the array
static void printArr(int []arr, int n)
{
    for (int i = 0; i < n; i++)
        Console.Write(arr[i] + " ");
}
  
// Function to generate and print
// the required array
static void generateArr(int []A, int n)
{
    int []B = new int[n];
  
    // For every element of the array
    for (int i = 0; i < n; i++) 
    {
  
        // To store the count of elements
        // on the left that the current
        // element divides
        int cnt = 0;
        for (int j = 0; j < i; j++)
        {
            if (A[j] % A[i] == 0)
                cnt++;
        }
        B[i] = cnt;
    }
  
    // Print the generated array
    printArr(B, n);
}
  
// Driver code
public static void Main(String []args)
{
    int []A = { 3, 5, 1 };
    int n = A.Length;
  
    generateArr(A, n);
}
}
  
// This code is contributed by Rajput-Ji


输出:
0 0 2