fbinnzhivko

04. Nested Loops

Mar 20th, 2016
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.72 KB | None | 0 0
  1. using System;
  2.  
  3. class Problem_4_Bulls_and_Cows
  4. {
  5.     static void Main()
  6.     {
  7.         int guessNum = int.Parse(Console.ReadLine());
  8.         int targetBulls = int.Parse(Console.ReadLine());
  9.         int targetCows = int.Parse(Console.ReadLine());
  10.  
  11.         bool solutionFound = false;
  12.  
  13.         // Check all possible sequences of 4 digits
  14.         for (int d1 = 1; d1 <= 9; d1++)
  15.         {
  16.             for (int d2 = 1; d2 <= 9; d2++)
  17.             {
  18.                 for (int d3 = 1; d3 <= 9; d3++)
  19.                 {
  20.                     for (int d4 = 1; d4 <= 9; d4++)
  21.                     {
  22.                         int guess1 = (guessNum / 1000) % 10;
  23.                         int guess2 = (guessNum / 100) % 10;
  24.                         int guess3 = (guessNum / 10) % 10;
  25.                         int guess4 = (guessNum / 1) % 10;
  26.  
  27.                         int oldD1 = d1;
  28.                         int oldD2 = d2;
  29.                         int oldD3 = d3;
  30.                         int oldD4 = d4;
  31.  
  32.                         // Calculate the number of bulls and cows between the numbers
  33.                         // (d1, d2, d3, d4) and (guess1, guess2, guess3, guess4)
  34.  
  35.                         int bulls = 0;
  36.                         int cows = 0;
  37.  
  38.                         // Find all bulls, count them and remove them (assign -1 and -2)
  39.                         if (d1 == guess1)
  40.                         {
  41.                             // Bull at position #1 found -> count it and remove it
  42.                             bulls++;
  43.                             guess1 = -1;
  44.                             d1 = -2;
  45.                         }
  46.                         if (d2 == guess2)
  47.                         {
  48.                             // Bull at position #2 found -> count it and remove it
  49.                             bulls++;
  50.                             guess2 = -1;
  51.                             d2 = -2;
  52.                         }
  53.                         if (d3 == guess3)
  54.                         {
  55.                             // Bull at position #3 found -> count it and remove it
  56.                             bulls++;
  57.                             guess3 = -1;
  58.                             d3 = -2;
  59.                         }
  60.                         if (d4 == guess4)
  61.                         {
  62.                             // Bull at position #4 found -> count it and remove it
  63.                             bulls++;
  64.                             guess4 = -1;
  65.                             d4 = -2;
  66.                         }
  67.  
  68.                         // Find all cows for d1, count them and remove them (assign -1)
  69.                         if (d1 == guess2)
  70.                         {
  71.                             // Cow at position #2 found -> count it and remove it
  72.                             cows++;
  73.                             guess2 = -1;
  74.                         }
  75.                         else if (d1 == guess3)
  76.                         {
  77.                             // Cow at position #3 found -> count it and remove it
  78.                             cows++;
  79.                             guess3 = -1;
  80.                         }
  81.                         else if (d1 == guess4)
  82.                         {
  83.                             // Cow at position #4 found -> count it and remove it
  84.                             cows++;
  85.                             guess4 = -1;
  86.                         }
  87.  
  88.                         // Find all cows for d2, count them and remove them (assign -1)
  89.                         if (d2 == guess1)
  90.                         {
  91.                             // Cow at position #1 found -> count it and remove it
  92.                             cows++;
  93.                             guess1 = -1;
  94.                         }
  95.                         else if (d2 == guess3)
  96.                         {
  97.                             // Cow at position #3 found -> count it and remove it
  98.                             cows++;
  99.                             guess3 = -1;
  100.                         }
  101.                         else if (d2 == guess4)
  102.                         {
  103.                             // Cow at position #4 found -> count it and remove it
  104.                             cows++;
  105.                             guess4 = -1;
  106.                         }
  107.  
  108.                         // Find all cows for d3, count them and remove them (assign -1)
  109.                         if (d3 == guess1)
  110.                         {
  111.                             // Cows at position #1 found -> count it and remove it
  112.                             cows++;
  113.                             guess1 = -1;
  114.                         }
  115.                         else if (d3 == guess2)
  116.                         {
  117.                             // Cow at position #2 found -> count it and remove it
  118.                             cows++;
  119.                             guess2 = -1;
  120.                         }
  121.                         else if (d3 == guess4)
  122.                         {
  123.                             // Cow at position #4 found -> count it and remove it
  124.                             cows++;
  125.                             guess4 = -1;
  126.                         }
  127.  
  128.                         // Find all cows for d4, count them and remove them (assign -1)
  129.                         if (d4 == guess1)
  130.                         {
  131.                             // Cows at position #1 found -> count it and remove it
  132.                             cows++;
  133.                             guess1 = -1;
  134.                         }
  135.                         else if (d4 == guess2)
  136.                         {
  137.                             // Cow at position #2 found -> count it and remove it
  138.                             cows++;
  139.                             guess2 = -1;
  140.                         }
  141.                         else if (d4 == guess3)
  142.                         {
  143.                             // Cow at position #3 found -> count it and remove it
  144.                             cows++;
  145.                             guess3 = -1;
  146.                         }
  147.  
  148.                         d1 = oldD1;
  149.                         d2 = oldD2;
  150.                         d3 = oldD3;
  151.                         d4 = oldD4;
  152.  
  153.                         // Print the configuration if the bulls and cows match the target
  154.                         if (bulls == targetBulls && cows == targetCows)
  155.                         {
  156.                             if (solutionFound)
  157.                             {
  158.                                 Console.Write(" ");
  159.                             }
  160.                             Console.Write("" + d1 + d2 + d3 + d4);
  161.                             solutionFound = true;
  162.                         }
  163.                     }
  164.                 }
  165.             }
  166.         }
  167.  
  168.         if (!solutionFound)
  169.         {
  170.             Console.WriteLine("No");
  171.         }
  172.     }
  173. }
Add Comment
Please, Sign In to add comment