이 튜토리얼에서는 x로 나눌 수 있는 이진 배열의 접두사 개수를 찾는 프로그램에 대해 설명합니다.
이를 위해 이진 배열과 값 x가 제공됩니다. 우리의 임무는 접두사가 주어진 값 x로 나누어지는 요소의 수를 찾는 것입니다.
예시
#include <bits/stdc++.h>
using namespace std;
//counting the elements with prefixes
//divisible by x
int count_divx(int arr[], int n, int x){
int number = 0;
int count = 0;
for (int i = 0; i < n; i++) {
number = number * 2 + arr[i];
//increasing count
if ((number % x == 0))
count += 1;
}
return count;
}
int main(){
int arr[] = { 1, 0, 1, 0, 1, 1, 0 };
int n = sizeof(arr) / sizeof(arr[0]);
int x = 2;
cout << count_divx(arr, n, x);
return 0;
} 출력
3