Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace bulls_cows
- {
- class Program
- {
- static void Main(string[] args)
- {
- int numGuess = int.Parse(Console.ReadLine());
- int bulls = int.Parse(Console.ReadLine());
- int cows = int.Parse(Console.ReadLine());
- int firstDigit = numGuess / 1000;
- int secondDigit = (numGuess / 100) % 10;
- int thirdDigit = (numGuess / 10) % 10;
- int fourthDigit = numGuess % 10;
- bool firstBull = false;
- bool firstCow = false;
- bool secondBull = false;
- bool secondCow = false;
- bool thirdBull = false;
- bool thirdCow = false;
- bool fourthBull = false;
- bool fourthCow = false;
- bool printed = false;
- int countBulls = 0;
- int countCows = 0;
- for (int dig1 = 1; dig1 <= 9; dig1++)
- {
- //check for bull at position 1
- if (firstBull) countBulls--;
- if (dig1 == firstDigit)
- {
- firstBull = true;
- countBulls++;
- }
- else
- {
- firstBull = false;
- }
- for (int dig2 = 1; dig2 <= 9; dig2++)
- {
- //check for bull at position 2
- if (secondBull) countBulls--;
- if (dig2 == secondDigit)
- {
- secondBull = true;
- countBulls++;
- }
- else
- {
- secondBull = false;
- }
- for (int dig3 = 1; dig3 <= 9; dig3++)
- {
- //check for bull at position 3
- if (thirdBull) countBulls--;
- if (dig3 == thirdDigit)
- {
- thirdBull = true;
- countBulls++;
- }
- else
- {
- thirdBull = false;
- }
- for (int dig4 = 1; dig4 <= 9; dig4++)
- {
- //check for bull at position 4
- if (fourthBull) countBulls--;
- if (dig4 == fourthDigit)
- {
- fourthBull = true;
- countBulls++;
- }
- else
- {
- fourthBull = false;
- }
- //making checks for cows
- if (firstCow) countCows--;
- if (secondCow) countCows--;
- if (thirdCow) countCows--;
- if (fourthCow) countCows--;
- if (((dig1 == secondDigit && !secondBull) || (dig1 == thirdDigit && !thirdBull) || (dig1 == fourthDigit && !fourthBull)) && !firstBull)
- {
- firstCow = true;
- countCows++;
- }
- else
- {
- firstCow = false;
- }
- if (((dig2 == firstDigit && !firstBull) || (dig2 == thirdDigit && !thirdBull) || (dig2 == fourthDigit && !fourthBull)) && !secondBull)
- {
- secondCow = true;
- countCows++;
- }
- else
- {
- secondCow = false;
- }
- if (((dig3 == firstDigit && !firstBull) || (dig3 == secondDigit && !secondBull) || (dig3 == fourthDigit && !fourthBull)) && !thirdBull)
- {
- thirdCow = true;
- countCows++;
- }
- else
- {
- thirdCow = false;
- }
- if (((dig4 == firstDigit && !firstBull) || (dig4 == secondDigit && !secondBull) || (dig4 == thirdDigit && !thirdBull)) && !fourthBull)
- {
- fourthCow = true;
- countCows++;
- }
- else
- {
- fourthCow = false;
- }
- // making logic whether to print
- if (countBulls == bulls && countCows == cows)
- {
- Console.Write($"{dig1}{dig2}{dig3}{dig4} ");
- printed = true;
- }
- }
- }
- }
- }
- if (!printed) Console.WriteLine("No");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement