原文:https://www . geeksforgeeks . org/数组中所有元素的乘积可被给定数字 k 整除/

给定一个包含 n 个元素和一个数 k 的数组,任务是找出该数组中所有可被 k 整除的元素的乘积

示例:

input : arr[] = {15, 16, 10, 9, 6, 7, 17}
        k = 3
output : 810
input : arr[] = {5, 3, 6, 8, 4, 1, 2, 9}
        k = 2
output : 384 

这个想法是遍历数组并逐个检查元素。如果一个元素可以被 k 整除,那么就把这个元素的值乘以到目前为止的乘积,并在到达数组末尾时继续这个过程。 下面是上述方法的实现:

c

// c   program to find product of all the elements
// in an array divisible by a given number k
#include 
using namespace std;
// function to find product of all the elements
// in an array divisible by a given number k
int findproduct(int arr[], int n, int k)
{
    int prod = 1;
    // traverse the array
    for (int i = 0; i < n; i  ) {
        // if current element is divisible by k
        // multiply with product so far
        if (arr[i] % k == 0) {
            prod *= arr[i];
        }
    }
    // return calculated product
    return prod;
}
// driver code
int main()
{
    int arr[] = { 15, 16, 10, 9, 6, 7, 17 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 3;
    cout << findproduct(arr, n, k);
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java program to find product of all the elements
// in an array divisible by a given number k
import java.io.*;
class gfg {
// function to find product of all the elements
// in an array divisible by a given number k
static int findproduct(int arr[], int n, int k)
{
    int prod = 1;
    // traverse the array
    for (int i = 0; i < n; i  ) {
        // if current element is divisible by k
        // multiply with product so far
        if (arr[i] % k == 0) {
            prod *= arr[i];
        }
    }
    // return calculated product
    return prod;
}
// driver code
    public static void main (string[] args) {
        int arr[] = { 15, 16, 10, 9, 6, 7, 17 };
    int n = arr.length;
    int k = 3;
    system.out.println(findproduct(arr, n, k));
    }
}
// this code is contributed by inder_verma..

python 3

# python3 program to find product of all
# the elements in an array divisible by
# a given number k
# function to find product of all the elements
# in an array divisible by a given number k
def findproduct(arr, n, k):
    prod = 1
    # traverse the array
    for i in range(n):
        # if current element is divisible
        # by k, multiply with product so far
        if (arr[i] % k == 0):
            prod *= arr[i]
    # return calculated product
    return prod
# driver code
if __name__ == "__main__":
    arr= [15, 16, 10, 9, 6, 7, 17 ]
    n = len(arr)
    k = 3
    print (findproduct(arr, n, k))
# this code is contributed by ita_c

c

// c# program to find product of all
// the elements in an array divisible
// by a given number k
using system;
class gfg
{
// function to find product of all
// the elements in an array divisible
// by a given number k
static int findproduct(int []arr, int n, int k)
{
    int prod = 1;
    // traverse the array
    for (int i = 0; i < n; i  )
    {
        // if current element is divisible
        // by k multiply with product so far
        if (arr[i] % k == 0)
        {
            prod *= arr[i];
        }
    }
    // return calculated product
    return prod;
}
// driver code
public static void main()
{
    int []arr = { 15, 16, 10, 9, 6, 7, 17 };
    int n = arr.length;
    int k = 3;
    console.writeline(findproduct(arr, n, k));
}
}
// this code is contributed by inder_verma

服务器端编程语言(professional hypertext preprocessor 的缩写)


java 描述语言


output: 

810

时间复杂度 : o(n),其中 n 为数组中的元素个数。 辅助空间: o(1)