Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- public class Program
- {
- static bool Compare(string n1, string n2, int bulls, int cows)
- {
- int b = 0;
- int c = 0;
- List<int> bPos = new List<int> { };
- //count the bulls
- for (int i = 0; i < 4; i++)
- {
- if (n1[i] == n2[i])
- {
- b++;
- if (!bPos.Contains(i))
- {
- bPos.Add(i);
- }
- }
- }
- string newN1 = "";
- string newN2 = "";
- for (int i = 0; i < 4; i++)
- {
- if (n1[i] != n2[i])
- {
- newN1 += n1[i];
- newN2 += n2[i];
- }
- }
- List<int> cPos = new List<int> { };
- for (int i = 0; i < 4 - b; i++)
- {
- char candidate = newN2[i];
- if (newN1.Contains(candidate.ToString()))
- {
- int index = newN1.IndexOf(candidate);
- if (!cPos.Contains(index))
- {
- c++;
- cPos.Add(index);
- }
- }
- }
- if (b == bulls && c == cows) //check the bulls and the cows
- {
- return true;
- }
- return false;
- }
- public static void Main()
- {
- string n1 = Console.ReadLine();
- int bulls = int.Parse(Console.ReadLine());
- int cows = int.Parse(Console.ReadLine());
- bool isFound = true;
- for (int i = 1000; i < 10000; i++)
- {
- if (!i.ToString().Contains("0") && Compare(i.ToString(), n1, bulls, cows))
- {
- Console.Write(i + " ");
- isFound = false;
- }
- }
- if (isFound)
- {
- Console.WriteLine("No");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement