Advertisement
Guest User

Untitled

a guest
May 3rd, 2014
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. class BullsAndCows
  5. {
  6. static void Main()
  7. {
  8. string input = Console.ReadLine();
  9. byte bulls = byte.Parse(Console.ReadLine());
  10. byte cows = byte.Parse(Console.ReadLine());
  11. byte[] secretNum = new byte[input.Length];
  12. bool consoleEmpty = true;
  13. for (byte i = 0; i < input.Length; i++)
  14. {
  15. secretNum[i] = Convert.ToByte(Convert.ToString(input[i]));
  16. }
  17.  
  18. byte[] givenNum = new byte[4];
  19.  
  20. for (byte firstDigit = 1; firstDigit < 10; firstDigit++)
  21. {
  22. for (byte secondDigit = 1; secondDigit < 10; secondDigit++)
  23. {
  24. for (byte thirdDigit = 1; thirdDigit < 10; thirdDigit++)
  25. {
  26. for (byte fourthDigit = 1; fourthDigit < 10; fourthDigit++)
  27. {
  28. for (byte i = 0; i < input.Length; i++)
  29. {
  30. secretNum[i] = Convert.ToByte(Convert.ToString(input[i]));
  31. }
  32.  
  33. givenNum[0] = firstDigit;
  34. givenNum[1] = secondDigit;
  35. givenNum[2] = thirdDigit;
  36. givenNum[3] = fourthDigit;
  37.  
  38. bool bullsMatch = false;
  39. bool cowsMatch = false;
  40. byte b = 0;
  41. byte c = 0;
  42. for (byte i = 0; i < 4; i++)
  43. {
  44. if (givenNum[i] == secretNum[i])
  45. {
  46. secretNum[i] = 0;
  47. givenNum[i] = 0;
  48. b++;
  49. }
  50. }
  51. for (byte i = 0; i < 4; i++)
  52. {
  53. for (byte k = 0; k < 4; k++)
  54. {
  55. if (secretNum[i] == givenNum[k] && secretNum[i] != givenNum[i])
  56. {
  57. c++;
  58. }
  59. }
  60. }
  61.  
  62. if (c == cows)
  63. {
  64. cowsMatch = true;
  65. }
  66. if (b == bulls)
  67. {
  68. bullsMatch = true;
  69. }
  70.  
  71. if (bullsMatch == true && cowsMatch == true)
  72. {
  73. givenNum[0] = firstDigit;
  74. givenNum[1] = secondDigit;
  75. givenNum[2] = thirdDigit;
  76. givenNum[3] = fourthDigit;
  77. consoleEmpty = false;
  78. for (byte i = 0; i < 4; i++)
  79. {
  80. Console.Write(givenNum[i]);
  81. }
  82. Console.Write(" ");
  83. }
  84. }
  85. }
  86. }
  87. }
  88.  
  89. if (consoleEmpty == true)
  90. {
  91. Console.WriteLine("No");
  92. }
  93.  
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement