Advertisement
sashomaga

Binari Digits

Dec 26th, 2012
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.75 KB | None | 0 0
  1. using System;
  2.  
  3. //You are given a sequence of N positive integer numbers and one binary digit B (0 or 1).
  4. //Your task is to write a program that finds the number of binary digits (B) in each of the N numbers in binary numeral system. Example: 20 in the binary numeral
  5. //system looks like this: 10100. The number of binary digits 0 of the number 20 in the binary numeral system is 3.
  6. //Input
  7. //The input data is being read from the console.
  8. //On the first input line there will be the digit B.
  9. //On the second line you must read the number N.
  10. //On each of the following N lines there is one positive integer number written – the consequent number, whose sum of binary digits B we are searching for.
  11. //The input data will always be valid and in the format described. There is no need to check it explicitly.
  12. //Output
  13. //The output must be printed on the console.
  14. //In the output you must have N lines. Each line must have 1 integer number – the number of digits B in the binary representation of the given consequent number.
  15.  
  16. class Program
  17. {
  18.     static void Main()
  19.     {
  20.         int b = int.Parse(Console.ReadLine());
  21.         int n = int.Parse(Console.ReadLine());
  22.         long[] num = new long[n];
  23.  
  24.         for (int i = 0; i < num.Length; i++)
  25.         {
  26.             num[i] = long.Parse(Console.ReadLine());
  27.         }
  28.  
  29.         int score = 0;
  30.         string length;
  31.         for (int i = 0; i < num.Length; i++)
  32.         {
  33.             length = Convert.ToString(num[i],2);
  34.             for (int j = 0; j < length.Length;j++)
  35.             {
  36.                 if (((num[i] >> j) & 1) == b)
  37.                 {
  38.                     score++;
  39.                 }
  40.             }
  41.             Console.WriteLine(score);
  42.             score = 0;
  43.         }
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement