Advertisement
mustafov

Double Downs

Sep 3rd, 2015
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.89 KB | None | 0 0
  1. //Problem 5 – Double Downs
  2. //You are given a number N and N integers. Write a program to count all couples of bits between every two integers (num[0] and //num[1], num[1] and num[2], …, num[N-2] and num[N-1]). You should count 3 kinds of couples: vertical, left-diagonal and right-//diagonal like at the example on the right. Every “1” bit can be part of multiple couples. Check the comments in the examples to //understand your task better.
  3.  
  4.  
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10.  
  11. namespace ConsoleApplication2
  12. {
  13.     class Program
  14.     {
  15.         static void Main(string[] args)
  16.         {
  17.             int n = int.Parse(Console.ReadLine());
  18.  
  19.             int vertical = 0;
  20.             int rightDiagonal = 0;
  21.             int leftDiagonal = 0;
  22.  
  23.             int[] numbers = new int[n];
  24.             for (int i = 0; i < n; i++)
  25.             {
  26.                 int num = int.Parse(Console.ReadLine());
  27.                 numbers[i] = num;
  28.             }
  29.  
  30.             int first = 0;
  31.             int firstBitValue = 0;
  32.             int second = 0;
  33.             int secondBitValue = 0;
  34.             int maxCount = 0;
  35.  
  36.             for (int j = 0; j < n; j++)
  37.             {
  38.                 first = numbers[j];
  39.                 string firstBin = Convert.ToString(first, 2);
  40.                 maxCount = firstBin.Length;
  41.                 if (j + 1 == n)
  42.                 {
  43.                     break;
  44.                 }
  45.                 second = numbers[j + 1];
  46.                 string secondBin = Convert.ToString(second, 2);
  47.                 if (maxCount < secondBin.Length)
  48.                 {
  49.                     maxCount = secondBin.Length;
  50.                 }
  51.                 for (int k = 0; k < maxCount; k++)
  52.                 {
  53.                     firstBitValue = first & (1 << k);
  54.                     firstBitValue = firstBitValue >> k;
  55.                     for (int p = 0; p < maxCount; p++)
  56.                     {
  57.                         secondBitValue = second & (1 << p);
  58.                         secondBitValue = secondBitValue >> p;
  59.                         if (firstBitValue == 1 && secondBitValue == 1)
  60.                         {
  61.                             if (k == p)
  62.                             {
  63.                                 vertical++;
  64.                             }
  65.                             else if (k == p + 1)
  66.                             {
  67.                                 rightDiagonal++;
  68.                             }
  69.                             else if (k == p - 1)
  70.                             {
  71.                                 leftDiagonal++;
  72.                             }
  73.                         }
  74.                     }
  75.                 }
  76.             }
  77.             Console.WriteLine(rightDiagonal);
  78.             Console.WriteLine(leftDiagonal);
  79.             Console.WriteLine(vertical);
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement