Подсчитайте количество элементов в массиве, которые делятся на k
Опубликовано: 3 Декабря, 2021
Дан массив целых чисел. Задача состоит в том, чтобы подсчитать количество элементов, которые делятся на заданное число k.
Примеры:
Ввод: arr [] = {2, 6, 7, 12, 14, 18}, k = 3
Выход: 3
Числа, которые делятся на k: {6, 12, 18}
Ввод: arr [] = {2, 6, 7, 12, 14, 18}, k = 2
Выход: 5 Рекомендуется: сначала попробуйте свой подход в {IDE}, прежде чем переходить к решению.
Метод 1: начните обход массива и проверьте, делится ли текущий элемент на K. Если да, то увеличьте счетчик. Выведите счет, когда пройдут все элементы.
Ниже представлена реализация описанного выше подхода:
C ++
// C++ program to Count the number of elements// in an array which are divisible by k#include <iostream>using namespace std;// Function to count the elementsint CountTheElements( int arr[], int n, int k){ int counter = 0; for ( int i = 0; i < n; i++) { if (arr[i] % k == 0) counter++; } counter; return}// Driver codeint 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 program to Count the number of elements// in an array which are divisible by kimport java.util.*;class Geeks { // Function to count the elementsstatic int CountTheElements( int arr[], int n, int k){ int counter = 0 ; for ( int i = 0 ; i < n; i++) { if (arr[i] % k == 0 ) counter++; } counter; return}// Driver codepublic 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 elementsdef CountTheElements(arr, n, k): counter = 0 for i in range ( 0 , n, 1 ): if (arr[i] % k = = 0 ): counter = counter + 1 counter return# Driver codeif __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 kusing System;class Geeks { // Function to count the elementsstatic int CountTheElements( int []arr, int n, int k){ int counter = 0; for ( int i = 0; i < n; i++) { if (arr[i] % k == 0) counter++; } counter; return}// Driver codepublic 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
<?php// PHP program to Count the number of elements// in an array which are divisible by k// Function to count the elementsfunction CountTheElements( $arr , $n , $k ){ $counter = 0; for ( $i = 0; $i < $n ; $i ++) { if ( $arr [ $i ] % $k == 0) $counter ++; } return $counter ;}// Driver code$arr = array ( 2, 6, 7, 12, 14, 18 );$n = count ( $arr );$k = 3;echo CountTheElements( $arr , $n , $k );// This code is contributed by inder_verma?> |
Javascript
<script>// Javascript program to Count the number// of elements in an array which are// divisible by k// Function to count the elementsfunction CountTheElements(arr, n, k){ let counter = 0; for (let i = 0; i < n; i++) { if (arr[i] % k == 0) counter++; } counter; return}// Driver codelet arr = [ 2, 6, 7, 12, 14, 18 ];let n = arr.length;let k = 3;document.write(CountTheElements(arr, n, k));// This code is contributed by subhammahato348</script> |
Выход:
3