GerryM

BinaryDigitsCount

Apr 7th, 2014
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.22 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.  
  7. namespace BinaryDigitsCount
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.            int b = int.Parse(Console.ReadLine());
  14.            int n = int.Parse(Console.ReadLine());
  15.            int[] numbers = new int[n];
  16.            for (int i = 0; i < numbers.Length; i++)
  17.            {
  18.                int count = 0;
  19.                numbers[i] = int.Parse(Console.ReadLine());
  20.                var x = Convert.ToString(numbers[i], 2);
  21.                char[] binary = x.ToCharArray();
  22.                for (int j = 0; j < binary.Length; j++)
  23.                {
  24.  
  25.                    if ((binary[j] =='0') && (b == 0))
  26.                    {
  27.                        count++;
  28.                    }
  29.                    else if ((binary[j] == '1') && (b == 1))
  30.                    {
  31.                        count++;
  32.                    }
  33.                }
  34.                numbers[i] = count;
  35.            }
  36.            
  37.            
  38.            
  39.          
  40.            for (int i = 0; i < numbers.Length; i++)
  41.            {
  42.                Console.WriteLine(numbers[i]);
  43.            }
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment