Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- //You are given a sequence of N positive integer numbers and one binary digit B (0 or 1).
- //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
- //system looks like this: 10100. The number of binary digits 0 of the number 20 in the binary numeral system is 3.
- //Input
- //The input data is being read from the console.
- //On the first input line there will be the digit B.
- //On the second line you must read the number N.
- //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.
- //The input data will always be valid and in the format described. There is no need to check it explicitly.
- //Output
- //The output must be printed on the console.
- //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.
- class Program
- {
- static void Main()
- {
- int b = int.Parse(Console.ReadLine());
- int n = int.Parse(Console.ReadLine());
- long[] num = new long[n];
- for (int i = 0; i < num.Length; i++)
- {
- num[i] = long.Parse(Console.ReadLine());
- }
- int score = 0;
- string length;
- for (int i = 0; i < num.Length; i++)
- {
- length = Convert.ToString(num[i],2);
- for (int j = 0; j < length.Length;j++)
- {
- if (((num[i] >> j) & 1) == b)
- {
- score++;
- }
- }
- Console.WriteLine(score);
- score = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement