Advertisement
Guest User

Untitled

a guest
Aug 25th, 2017
498
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.51 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 bulls_cows
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int numGuess = int.Parse(Console.ReadLine());
  14.             int bulls = int.Parse(Console.ReadLine());
  15.             int cows = int.Parse(Console.ReadLine());
  16.             int firstDigit = numGuess / 1000;
  17.             int secondDigit = (numGuess / 100) % 10;
  18.             int thirdDigit = (numGuess / 10) % 10;
  19.             int fourthDigit = numGuess % 10;
  20.             bool firstBull = false;
  21.             bool firstCow = false;
  22.             bool secondBull = false;
  23.             bool secondCow = false;
  24.             bool thirdBull = false;
  25.             bool thirdCow = false;
  26.             bool fourthBull = false;
  27.             bool fourthCow = false;
  28.             bool printed = false;
  29.             int countBulls = 0;
  30.             int countCows = 0;
  31.             for (int dig1 = 1; dig1 <= 9; dig1++)
  32.             {
  33.                 //check for bull at position 1
  34.                 if (firstBull) countBulls--;
  35.                 if (dig1 == firstDigit)
  36.                 {
  37.                     firstBull = true;
  38.                     countBulls++;
  39.                 }
  40.                 else
  41.                 {
  42.                     firstBull = false;
  43.                 }
  44.                 for (int dig2 = 1; dig2 <= 9; dig2++)
  45.                 {
  46.                     //check for bull at position 2
  47.                     if (secondBull) countBulls--;
  48.                     if (dig2 == secondDigit)
  49.                     {
  50.                         secondBull = true;
  51.                         countBulls++;
  52.                     }
  53.                     else
  54.                     {
  55.                         secondBull = false;
  56.                     }
  57.                     for (int dig3 = 1; dig3 <= 9; dig3++)
  58.                     {
  59.                         //check for bull at position 3
  60.  
  61.                         if (thirdBull) countBulls--;
  62.                         if (dig3 == thirdDigit)
  63.                         {
  64.                             thirdBull = true;
  65.                             countBulls++;
  66.                         }
  67.                         else
  68.                         {
  69.                             thirdBull = false;
  70.                         }
  71.                         for (int dig4 = 1; dig4 <= 9; dig4++)
  72.                         {
  73.                             //check for bull at position 4
  74.                             if (fourthBull) countBulls--;
  75.                             if (dig4 == fourthDigit)
  76.                             {
  77.                                 fourthBull = true;
  78.                                 countBulls++;
  79.                             }
  80.                             else
  81.                             {
  82.                                 fourthBull = false;
  83.                             }
  84.                             //making checks for cows
  85.                             if (firstCow) countCows--;
  86.                             if (secondCow) countCows--;
  87.                             if (thirdCow) countCows--;
  88.                             if (fourthCow) countCows--;
  89.                             if (((dig1 == secondDigit && !secondBull) || (dig1 == thirdDigit && !thirdBull) || (dig1 == fourthDigit && !fourthBull)) && !firstBull)
  90.                             {
  91.                                 firstCow = true;
  92.                                 countCows++;
  93.                             }
  94.                             else
  95.                             {
  96.                                 firstCow = false;
  97.                             }
  98.                             if (((dig2 == firstDigit && !firstBull) || (dig2 == thirdDigit && !thirdBull) || (dig2 == fourthDigit && !fourthBull)) && !secondBull)
  99.                             {
  100.                                 secondCow = true;
  101.                                 countCows++;
  102.                             }
  103.                             else
  104.                             {
  105.                                 secondCow = false;
  106.                             }
  107.                             if (((dig3 == firstDigit && !firstBull) || (dig3 == secondDigit && !secondBull) || (dig3 == fourthDigit && !fourthBull)) && !thirdBull)
  108.                             {
  109.                                 thirdCow = true;
  110.                                 countCows++;
  111.                             }
  112.                             else
  113.                             {
  114.                                 thirdCow = false;
  115.                             }
  116.                             if (((dig4 == firstDigit && !firstBull) || (dig4 == secondDigit && !secondBull) || (dig4 == thirdDigit && !thirdBull)) && !fourthBull)
  117.                             {
  118.                                 fourthCow = true;
  119.                                 countCows++;
  120.                             }
  121.                             else
  122.                             {
  123.                                 fourthCow = false;
  124.                             }
  125.                             // making logic whether to print
  126.                            
  127.                             if (countBulls == bulls && countCows == cows)
  128.                             {
  129.                                 Console.Write($"{dig1}{dig2}{dig3}{dig4} ");
  130.                                 printed = true;
  131.                             }
  132.                         }
  133.                     }
  134.                 }
  135.             }
  136.             if (!printed) Console.WriteLine("No");
  137.         }
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement