staafl

problem 4 - bulls and cows

Mar 30th, 2014
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. using System;
  2.  
  3. class Program
  4. {
  5. static void Main()
  6. {
  7. string secret = Console.ReadLine();
  8. int bulls = int.Parse(Console.ReadLine());
  9. int cows = int.Parse(Console.ReadLine());
  10. int answers = 0;
  11. for (int i1 = 1; i1 <= 9; i1++)
  12. {
  13. for (int i2 = 1; i2 <= 9; i2++)
  14. {
  15. for (int i3 = 1; i3 <= 9; i3++)
  16. {
  17. for (int i4 = 1; i4 <= 9; i4++)
  18. {
  19. int guess = i1 * 1000 + i2 * 100 + i3 * 10 + i4;
  20. string guessStr = guess.ToString();
  21. int guessBulls = 0;
  22. int guessCows = 0;
  23. for (int digit = 0; digit < 4; digit++)
  24. {
  25. char currentSecret = secret[digit];
  26. bool isBull = false;
  27. if (currentSecret == guessStr[digit])
  28. {
  29. isBull = true;
  30. guessBulls += 1;
  31. }
  32. if (isBull)
  33. continue;
  34. for (int otherDigit = 0; otherDigit < 4; otherDigit++)
  35. {
  36. if (otherDigit == digit)
  37. continue;
  38. if (currentSecret == guessStr[otherDigit] &&
  39. guessStr[otherDigit] != secret[otherDigit])
  40. {
  41. guessCows += 1;
  42. break;
  43. }
  44. }
  45. }
  46. if (guessCows == cows && guessBulls == bulls)
  47. {
  48. Console.Write(guess + " ");
  49. answers += 1;
  50. }
  51. }
  52. }
  53. }
  54. }
  55. if (answers == 0)
  56. Console.WriteLine("No");
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment