计算数组中能被 k 整除的元素个数
给定一个整数数组。任务是计算可被给定数 k 整除的元素数量。
例子:
Input: arr[] = { 2, 6, 7, 12, 14, 18 }, k = 3
Output: 3
Numbers which are divisible by k are { 6, 12, 18 }
Input: arr[] = { 2, 6, 7, 12, 14, 18 }, k = 2
Output: 5
方法一:开始遍历数组并检查当前元素是否可被 K 整除。如果是,则增加计数。当遍历所有元素时打印计数。
下面是上述方法的实现:
C++
// C++ program to Count the number of elements
// in an array which are divisible by k
#include
using namespace std;
// Function to count the elements
int CountTheElements(int arr[], int n, int k)
{
int counter = 0;
for (int i = 0; i < n; i++) {
if (arr[i] % k == 0)
counter++;
}
return counter;
}
// Driver code
int main()
{
int arr[] = { 2, 6, 7, 12, 14, 18 };
int n = sizeof(arr) / sizeof(arr[0]);
int k = 3;
cout << CountTheElements(arr, n, k);
return 0;
}
Java
// Java program to Count the number of elements
// in an array which are divisible by k
import java.util.*;
class Geeks {
// Function to count the elements
static int CountTheElements(int arr[], int n, int k)
{
int counter = 0;
for (int i = 0; i < n; i++) {
if (arr[i] % k == 0)
counter++;
}
return counter;
}
// Driver code
public static void main(String args[])
{
int arr[] = { 2, 6, 7, 12, 14, 18 };
int n = arr.length;
int k = 3;
System.out.println(CountTheElements(arr, n, k));
}
}
// This code is contributed by ankita_saini
Python3
# Python 3 program to Count the
# number of elements in an array
# which are divisible by k
# Function to count the elements
def CountTheElements(arr, n, k):
counter = 0
for i in range(0, n, 1):
if (arr[i] % k == 0):
counter = counter+1
return counter
# Driver code
if __name__ == '__main__':
arr = [2, 6, 7, 12, 14, 18];
n = len(arr)
k = 3
print(CountTheElements(arr, n, k))
# This code is contributed by
# Surendra_Gangwar
C#
// C# program to Count the number of elements
// in an array which are divisible by k
using System;
class Geeks {
// Function to count the elements
static int CountTheElements(int []arr, int n, int k)
{
int counter = 0;
for (int i = 0; i < n; i++) {
if (arr[i] % k == 0)
counter++;
}
return counter;
}
// Driver code
public static void Main()
{
int []arr = { 2, 6, 7, 12, 14, 18 };
int n = arr.Length;
int k = 3;
Console.WriteLine(CountTheElements(arr, n, k));
}
}
//This code is contributed by inder_verma..
PHP
Javascript
C++
// C++ implementation of the above approach
#include
using namespace std;
int main()
{
vector v{ 2, 6, 7, 12, 14, 18 };
// Count the number elements which when passed
// through the function (3rd argument) returns true
int res = count_if(v.begin(), v.end(),
[](int i, int k = 3) { return i % k == 0; });
cout << res;
return 0;
}
Java
// Java implementation of the above approach
import java.util.*;
class GFG
{
public static void main(String[] args)
{
int []v = { 2, 6, 7, 12, 14, 18 };
// Count the number elements which when passed
// through the function (3rd argument) returns true
int res = 0;
for(int i = 0; i < v.length; i++) {
if(v[i] % 3 == 0)
res++;
}
System.out.print(res);
}
}
// This code is contributed by gauravrajput1
Python3
# Java implementation of the above approach
v = [ 2, 6, 7, 12, 14, 18 ];
# Count the number elements which when passed
# through the function (3rd argument) returns true
res = 0
for i in range(0,len(v)):
if(v[i] % 3 == 0):
res+=1
print(res)
# This code is contributed by shivanisinghss2110
C#
// C# implementation of the above approach
using System;
class GFG
{
public static void Main(String[] args)
{
int []v = { 2, 6, 7, 12, 14, 18 };
// Count the number elements which when passed
// through the function (3rd argument) returns true
int res = 0;
for(int i = 0; i < v.Length; i++) {
if(v[i] % 3 == 0)
res++;
}
Console.Write(res);
}
}
// This code is contributed by shivanisinghss2110
Javascript
输出:
3
方法 2:另一种方法是使用内置函数– Count_if 。该函数接收指向包含元素和辅助函数的容器的开始和结束的指针。它将返回函数将返回 true 的元素的计数。
下面是上述方法的实现:
C++
// C++ implementation of the above approach
#include
using namespace std;
int main()
{
vector v{ 2, 6, 7, 12, 14, 18 };
// Count the number elements which when passed
// through the function (3rd argument) returns true
int res = count_if(v.begin(), v.end(),
[](int i, int k = 3) { return i % k == 0; });
cout << res;
return 0;
}
Java
// Java implementation of the above approach
import java.util.*;
class GFG
{
public static void main(String[] args)
{
int []v = { 2, 6, 7, 12, 14, 18 };
// Count the number elements which when passed
// through the function (3rd argument) returns true
int res = 0;
for(int i = 0; i < v.length; i++) {
if(v[i] % 3 == 0)
res++;
}
System.out.print(res);
}
}
// This code is contributed by gauravrajput1
Python3
# Java implementation of the above approach
v = [ 2, 6, 7, 12, 14, 18 ];
# Count the number elements which when passed
# through the function (3rd argument) returns true
res = 0
for i in range(0,len(v)):
if(v[i] % 3 == 0):
res+=1
print(res)
# This code is contributed by shivanisinghss2110
C#
// C# implementation of the above approach
using System;
class GFG
{
public static void Main(String[] args)
{
int []v = { 2, 6, 7, 12, 14, 18 };
// Count the number elements which when passed
// through the function (3rd argument) returns true
int res = 0;
for(int i = 0; i < v.Length; i++) {
if(v[i] % 3 == 0)
res++;
}
Console.Write(res);
}
}
// This code is contributed by shivanisinghss2110
Javascript
输出:
3
时间复杂度: O(N)