Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <stdio.h>
- #include <tchar.h>
- #include <iostream>
- #include <string.h>
- using namespace std;
- void decToBin(int buf, char result[], int size)
- {
- int a, q,i(0);
- while (1)
- {
- a = buf % 2;
- q = buf / 2;
- if (q == 0 && a == 1)
- break;
- if(a==1)
- result[i++] = '1';
- else
- result[i++] = '0';
- buf = q;
- }
- result[i] = '\0';
- }
- int oneCount(int u) /*Считает количество единиц в этой строке через счётчик
- и сравнение каждого символа строки с единицей*/
- {
- const int size = 128;
- char result[size];
- decToBin(u, result,size);
- int counter = 0, i(0);
- while(result[i]!='\0') {
- if (result[i++] == '1') {
- counter++;
- }
- }
- return counter;
- }
- int maxOneCount(int array[],int size) /* Находит в массиве наибольшее число единиц и пишет
- в консоль максимум единиц и само число,
- при этом возвращая максимум, т.е. не нарушает задание*/
- {
- int max = -1;
- int num = -1;
- int buf;
- for (unsigned int i = 0; i < size; i++) {
- buf = oneCount(array[i]);
- if (buf > max) {
- max = buf;
- num = array[i];
- }
- }
- cout << max << ", it's number " << num << endl;
- return max;
- }
- int main() {
- int numbers[] = { 88, 2, 1111, 4, 102, 253};
- cout << "Biggest count of 1 in dec: ";
- maxOneCount(numbers,6);
- system("Pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement