archangelmihail

BullsAndCows

Nov 27th, 2013
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.65 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3.  
  4. class BullsAndCows
  5. {
  6.     static void Main()
  7.     {
  8.         ushort secretNumber = ushort.Parse(Console.ReadLine());
  9.         byte[] number = new byte[4];
  10.         for (int i = 3; i >= 0; i--)
  11.         {
  12.             byte digit = (byte)(secretNumber % 10);
  13.             number[i] = digit;
  14.             secretNumber /= 10;
  15.         }
  16.         byte[] original = new byte[4];
  17.         for (int i = 0; i < 4; i++)
  18.         {
  19.             original[i] = number[i];
  20.         }
  21.  
  22.         byte bulls = byte.Parse(Console.ReadLine());
  23.         byte cows = byte.Parse(Console.ReadLine());
  24.  
  25.         byte counterBulls = 0, counterCows = 0;
  26.         StringBuilder result = new StringBuilder();
  27.  
  28.         for (int num = 1111; num <= 9999; num++)
  29.         {
  30.             bool isZero = false;
  31.             int check = num;
  32.             byte[] guess = new byte[4];
  33.             for (int i = 3; i >= 0; i--)
  34.             {
  35.                 byte digit = (byte)(check % 10);
  36.                 // check if number contains 0
  37.                 if (digit == 0)
  38.                 {
  39.                     isZero = true;
  40.                     break;
  41.                 }
  42.                 guess[i] = digit;
  43.                 check /= 10;
  44.             }
  45.             if (isZero)
  46.             {
  47.                 continue;
  48.             }
  49.  
  50.             //check for bulls
  51.             for (int i = 3; i >= 0; i--)
  52.             {
  53.                 if (guess[i] == number[i])
  54.                 {
  55.                     counterBulls++;
  56.                     number[i] = 0;
  57.                     guess[i] = 0;
  58.                 }
  59.             }
  60.  
  61.             //check for cows
  62.             if (counterBulls == bulls)
  63.             {
  64.                 for (int i = 3; i >= 0; i--)
  65.                 {
  66.                     for (int j = 3; j >= 0; j--)
  67.                     {
  68.                         if (i != j && guess[i] == number[j] && guess[i] > 0 && number[j] > 0)
  69.                         {
  70.                             counterCows++;
  71.                             guess[i] = 0;
  72.                             number[j] = 0;
  73.                         }
  74.                     }
  75.                 }
  76.                 if (counterCows == cows)
  77.                 {
  78.                     result.Append(num + " ");
  79.                 }
  80.             }
  81.  
  82.             for (int i = 0; i < 4; i++)
  83.             {
  84.                 number[i] = original[i];
  85.             }
  86.             counterBulls = counterCows = 0;
  87.         }
  88.  
  89.         if (result.Length == 0)
  90.         {
  91.             Console.WriteLine("No");
  92.         }
  93.         else
  94.         {
  95.             result.Length--;
  96.             Console.WriteLine(result);
  97.         }
  98.     }
Advertisement
Add Comment
Please, Sign In to add comment