File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 55// доп: вывести кол во серий из единиц длинной больше 2
66
77#include " sequence.h"
8+ #define DOP
89
910int main () {
1011 // вводим N
@@ -17,8 +18,12 @@ int main() {
1718 N, dtoab (N), count_max_ones (N)
1819 );
1920
20- printf (
21+ #ifdef DOP
22+
23+ printf ( // количество серий из единиц
2124 " Series with length > 2: %d\n " ,
2225 count_ones_series (N)
2326 );
27+
28+ #endif
2429}
Original file line number Diff line number Diff line change @@ -30,7 +30,6 @@ char *dtoab(unsigned long long N) {
3030 // сдвигаем чтобы получить следующий бит
3131 N = N >> 1 ;
3232 }
33-
3433 // добавляем в конец \0 и выходим
3534 arr[len] = 0 ;
3635 return arr;
@@ -55,17 +54,22 @@ int count_max_ones(unsigned long long N) {
5554// нахождение количества серий
5655// из единиц длинной больше 2
5756int count_ones_series (unsigned long long N) {
57+ // преобразуем число в двоичную строку
5858 char *str = dtoab (N);
5959 int count = 0 , current = 0 ;
6060
61+ // проходим по всем битам числа
6162 for (int i = 0 ; str[i] != 0 ; i++) {
6263 if (str[i] == ' 1' ) {
6364 current++;
6465 } else {
66+ // увеличиваем счетчик если последовательность > 2
6567 if (current > 2 ) count++;
68+ // обнуляем счетчик
6669 current = 0 ;
6770 }
6871 }
72+ // не забываем про младшие биты
6973 if (current > 2 ) count++;
7074 return count;
7175}
You can’t perform that action at this time.
0 commit comments