📌  相关文章
📜  可被它们的乘积或数字总和整除的数组元素的计数

📅  最后修改于: 2022-05-13 01:56:04.780000             🧑  作者: Mango

可被它们的乘积或数字总和整除的数组元素的计数

给定一个数组arr[] 。任务是计算数组中可以被数字乘积或数字总和整除的元素。

例子:

方法:这个问题是基于观察的。请按照以下步骤解决给定的问题。

  • 声明一个变量说count = 0 ,以存储满足给定条件的元素的数量。
  • 对于数组arr[]中的每个数字,求其数字之和及其数字之积。
  • 检查数字是否可以被数字之和或数字的乘积整除。
  • 如果是,请将其添加到您的计数变量中。
  • 返回计数作为最终答案。

下面是上述方法的实现。

C++
// C++ program for above approach
#include 
using namespace std;
 
// Function to find the number of elements
// in arr[] that follows the given conditions
int solve(int arr[], int n)
{
 
    // To count the number of elements
    // that follow the given conditions
    int count = 0;
 
    // Iterate through the array arr[]
    for (int i = 0; i < n; i++) {
        int p = arr[i];
        int sum = 0, prod = 1;
        while (p > 0) {
            int r = p % 10;
            sum += r;
            prod *= r;
            p /= 10;
        }
 
        // Check if condition satisfies or not
        if ((sum > 0 && arr[i] % sum == 0)
            || (prod > 0 && arr[i] % prod == 0))
            count++;
    }
 
    // Return count as the final answer
    return count;
}
 
// Driver Code
int main()
{
    int arr[] = { 123, 25, 36, 7 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    cout << solve(arr, N) << endl;
 
    return 0;
}


Java
// Java program for above approach
import java.util.*;
public class GFG {
 
  // Function to find the number of elements
  // in arr[] that follows the given conditions
  static int solve(int[] arr, int n)
  {
 
    // To count the number of elements
    // that follow the given conditions
    int count = 0;
 
    // Iterate through the array arr[]
    for (int i = 0; i < n; i++) {
      int p = arr[i];
      int sum = 0, prod = 1;
      while (p > 0) {
        int r = p % 10;
        sum += r;
        prod *= r;
        p /= 10;
      }
 
      // Check if condition satisfies or not
      if ((sum > 0 && arr[i] % sum == 0)
          || (prod > 0 && arr[i] % prod == 0))
        count++;
    }
 
    // Return count as the final answer
    return count;
  }
 
  // Driver Code
  public static void main(String args[])
  {
    int[] arr = { 123, 25, 36, 7 };
    int N = arr.length;
 
    // Function Call
    System.out.println(solve(arr, N));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Python3
# python3 program for above approach
 
# Function to find the number of elements
# in arr[] that follows the given conditions
def solve(arr, n):
 
        # To count the number of elements
        # that follow the given conditions
    count = 0
 
    # Iterate through the array arr[]
    for i in range(0, n):
        p = arr[i]
        sum = 0
        prod = 1
        while (p > 0):
            r = p % 10
            sum += r
            prod *= r
            p //= 10
 
            # Check if condition satisfies or not
        if ((sum > 0 and arr[i] % sum == 0)
                or (prod > 0 and arr[i] % prod == 0)):
            count += 1
 
        # Return count as the final answer
    return count
 
# Driver Code
if __name__ == "__main__":
 
    arr = [123, 25, 36, 7]
    N = len(arr)
 
    # Function Call
    print(solve(arr, N))
 
    # This code is contributed by rakeshsahni


C#
// C# program for above approach
using System;
class GFG {
 
  // Function to find the number of elements
  // in arr[] that follows the given conditions
  static int solve(int[] arr, int n)
  {
 
    // To count the number of elements
    // that follow the given conditions
    int count = 0;
 
    // Iterate through the array arr[]
    for (int i = 0; i < n; i++) {
      int p = arr[i];
      int sum = 0, prod = 1;
      while (p > 0) {
        int r = p % 10;
        sum += r;
        prod *= r;
        p /= 10;
      }
 
      // Check if condition satisfies or not
      if ((sum > 0 && arr[i] % sum == 0)
          || (prod > 0 && arr[i] % prod == 0))
        count++;
    }
 
    // Return count as the final answer
    return count;
  }
 
  // Driver Code
  public static void Main()
  {
    int[] arr = { 123, 25, 36, 7 };
    int N = arr.Length;
 
    // Function Call
    Console.Write(solve(arr, N));
  }
}
 
// This code is contributed by ukasp.


Javascript



输出
2

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