Advertisement
kuruku

BinaryDigitsCount

Apr 14th, 2014
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.38 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Numerics;
  7.  
  8. namespace BinaryDigitsCount
  9. {
  10.     class BinaryDigitsCount
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             int b = int.Parse(Console.ReadLine());
  15.             int n = int.Parse(Console.ReadLine());
  16.             int counter = 0;
  17.             long[] arr = new long[n];
  18.             for (int i = 0; i < n; i++)
  19.             {
  20.                 counter = 0;
  21.                 long m = long.Parse(Console.ReadLine());
  22.                 string mBynari = Convert.ToString(m,2);
  23.                 for (int col = 0; col < mBynari.Length; col++)
  24.                 {
  25.                     if (b == 1)
  26.                     {
  27.                         if (mBynari[col] == '1')
  28.                         {
  29.                             counter++;
  30.                         }
  31.                     }
  32.                     else if (b == 0)
  33.                     {
  34.                         if (mBynari[col] == '0')
  35.                         {
  36.                             counter++;
  37.                         }  
  38.                     }
  39.                    
  40.                 }
  41.                 arr[i] = counter;
  42.             }
  43.  
  44.  
  45.             for (int i = 0; i < arr.Length; i++)
  46.             {
  47.                 Console.WriteLine(arr[i]);
  48.             }
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement