📌  相关文章
📜  数组中长度为 K 倍数的整数计数

📅  最后修改于: 2021-09-07 05:21:01             🧑  作者: Mango

给定一个包含N 个元素的数组a rr 和一个整数K ,任务是计算长度为K倍数的所有元素。
例子:

Input: arr[]={1, 12, 3444, 544, 9}, K = 2
Output: 2
Explanation:
There are 2 numbers whose digit count is multiple of 2 {12, 3444}.

Input: arr[]={12, 345, 2, 68, 7896}, K = 3
Output: 1
Explanation:
There is 1 number whose digit count is multiple of 3 {345}.

方法:

  1. 将数组中的数字一一遍历
  2. 计算数组中每个数字的位数
  3. 检查它的位数是否是 K 的倍数。

下面是上述方法的实现:

C++
// C++ implementation of above approach
 
#include 
using namespace std;
 
// Function to find
// digit count of numbers
int digit_count(int x)
{
    int sum = 0;
    while (x) {
        sum++;
        x = x / 10;
    }
    return sum;
}
 
// Function to find the count of numbers
int find_count(vector arr, int k)
{
 
    int ans = 0;
    for (int i : arr) {
 
        // Get the digit count of each element
        int x = digit_count(i);
 
        // Check if the digit count
        // is divisible by K
        if (x % k == 0)
 
            // Increment the count
            // of required numbers by 1
            ans += 1;
    }
 
    return ans;
}
 
// Driver code
int main()
{
    vector arr
        = { 12, 345, 2, 68, 7896 };
    int K = 2;
 
    cout << find_count(arr, K);
 
    return 0;
}


Java
// Java implementation of above approach
 
class GFG{
  
// Function to find
// digit count of numbers
static int digit_count(int x)
{
    int sum = 0;
    while (x > 0) {
        sum++;
        x = x / 10;
    }
    return sum;
}
  
// Function to find the count of numbers
static int find_count(int []arr, int k)
{
  
    int ans = 0;
    for (int i : arr) {
  
        // Get the digit count of each element
        int x = digit_count(i);
  
        // Check if the digit count
        // is divisible by K
        if (x % k == 0)
  
            // Increment the count
            // of required numbers by 1
            ans += 1;
    }
  
    return ans;
}
  
// Driver code
public static void main(String[] args)
{
    int []arr = { 12, 345, 2, 68, 7896 };
    int K = 2;
  
    System.out.print(find_count(arr, K));
  
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation of above approach
 
# Function to find
# digit count of numbers
def digit_count(x):
    sum = 0
    while (x):
        sum += 1
        x = x // 10
    return sum
 
# Function to find the count of numbers
def find_count(arr,k):
    ans = 0
    for i in arr:
        # Get the digit count of each element
        x = digit_count(i)
 
        # Check if the digit count
        # is divisible by K
        if (x % k == 0):
            # Increment the count
            # of required numbers by 1
            ans += 1
 
    return ans
 
# Driver code
if __name__ == '__main__':
    arr  =  [12, 345, 2, 68, 7896]
    K = 2
 
    print(find_count(arr, K))
 
# This code is contributed by Surendra_Gangwar


C#
// C# implementation of above approach
 
using System;
 
public class GFG{
 
// Function to find
// digit count of numbers
static int digit_count(int x)
{
    int sum = 0;
    while (x > 0) {
        sum++;
        x = x / 10;
    }
    return sum;
}
 
// Function to find the count of numbers
static int find_count(int []arr, int k)
{
 
    int ans = 0;
    foreach (int i in arr) {
 
        // Get the digit count of each element
        int x = digit_count(i);
 
        // Check if the digit count
        // is divisible by K
        if (x % k == 0)
 
            // Increment the count
            // of required numbers by 1
            ans += 1;
    }
 
    return ans;
}
 
// Driver code
public static void Main(String[] args)
{
    int []arr = { 12, 345, 2, 68, 7896 };
    int K = 2;
 
    Console.Write(find_count(arr, K));
 
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:

3

时间复杂度:- O(N*M) ,其中 N 是数组的大小,M 是数组中最大数字的位数。
空间复杂度:- O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live