Advertisement
Guest User

Untitled

a guest
Nov 26th, 2015
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.52 KB | None | 0 0
  1.  
  2. #include "stdafx.h"
  3. #include <stdio.h>
  4. #include <tchar.h>
  5. #include <iostream>
  6. #include <string.h>
  7. using namespace std;
  8.  
  9.  void decToBin(int buf, char result[], int size)
  10.  {
  11.     int a, q,i(0);
  12.     while (1)
  13.     {
  14.         a = buf % 2;
  15.         q = buf / 2;
  16.         if (q == 0 && a == 1)
  17.             break;
  18.         if(a==1)
  19.             result[i++] = '1';
  20.         else
  21.             result[i++] = '0';
  22.         buf = q;
  23.     }
  24.     result[i] = '\0';
  25. }
  26.  
  27. int oneCount(int u) /*Считает количество единиц в этой строке через счётчик
  28.                     и сравнение каждого символа строки с единицей*/
  29. {
  30.     const int size = 128;
  31.     char result[size];
  32.     decToBin(u, result,size);
  33.     int counter = 0, i(0);
  34.     while(result[i]!='\0') {
  35.         if (result[i++] == '1') {
  36.             counter++;
  37.         }
  38.     }
  39.  
  40.     return counter;
  41. }
  42.  
  43. int maxOneCount(int array[],int size) /* Находит в массиве наибольшее число единиц и пишет
  44.                              в консоль максимум единиц и само число,
  45.                              при этом возвращая максимум, т.е. не нарушает задание*/
  46. {
  47.     int max = -1;
  48.     int num = -1;
  49.     int buf;
  50.  
  51.     for (unsigned int i = 0; i < size; i++) {
  52.         buf = oneCount(array[i]);
  53.         if (buf > max) {
  54.             max = buf;
  55.             num = array[i];
  56.         }
  57.     }
  58.     cout << max << ", it's number " << num << endl;
  59.     return max;
  60. }
  61.  
  62. int main() {
  63.     int numbers[] = { 88, 2, 1111, 4, 102, 253};
  64.  
  65.     cout << "Biggest count of 1 in dec: ";
  66.     maxOneCount(numbers,6);
  67.     system("Pause");
  68.     return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement