Advertisement
Guest User

Untitled

a guest
Aug 25th, 2017
777
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.35 KB | None | 0 0
  1. using System;
  2. class Program
  3. {
  4.     static void Main()
  5.     {
  6.         string guessNum = Console.ReadLine();
  7.         int targetBulls = int.Parse(Console.ReadLine());
  8.         int targetCows = int.Parse(Console.ReadLine());
  9.         bool hasSolution = true;
  10.         bool isFirst = true;
  11.  
  12.         for (int num = 1111; num <= 9999; num++)
  13.         {
  14.             int bulls = 0;
  15.             int cows = 0;
  16.             char[] numStr = num.ToString().ToCharArray();
  17.             bool[] isGuessVisted = new bool[numStr.Length];
  18.             bool[] isNumVisted = new bool[numStr.Length];
  19.            // added another bool array to track the digits that are visited from the number we are checking
  20.  
  21.             if (!num.ToString().Contains("0"))
  22.             {
  23.                 for (int i = 0; i < guessNum.Length; i++)  // count bulls and cows
  24.                 {
  25.                     if (guessNum[i] == numStr[i])
  26.                     {
  27.                         bulls++;
  28.                         isGuessVisted[i] = true; // set that we have visited this digit at index i
  29.                         isNumVisted[i] = true; // set that we have visited this digit at index i
  30.                     }
  31.                 }
  32.  
  33.                 for (int i = 0; i < guessNum.Length; i++)
  34.                 {
  35.                     for (int j = 0; j < numStr.Length; j++)
  36.                     {
  37.                         if (i != j &&
  38.                             !isNumVisted[j] &&
  39.                             !isGuessVisted[i] &&
  40.                             guessNum[i] == numStr[j]) // check if digits are the same
  41.                         {
  42.                             cows++;
  43.                             isGuessVisted[i] = true; // set that we have visited this digit at index i
  44.                             isNumVisted[j] = true; // set that we have visited this digit at index j
  45.                         }
  46.                     }
  47.                 }
  48.                 if (bulls == targetBulls && cows == targetCows)
  49.                 {
  50.                     hasSolution = false;
  51.                     if (!isFirst)
  52.                     {
  53.                         Console.Write(" ");
  54.                     }
  55.                     Console.Write(num);
  56.                     isFirst = false;
  57.                 }
  58.             }
  59.         }
  60.  
  61.         if (hasSolution)
  62.         {
  63.             Console.WriteLine("No");
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement