Advertisement
Guest User

Untitled

a guest
Aug 25th, 2017
498
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.87 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. public class Program
  4. {
  5.     static bool Compare(string n1, string n2, int bulls, int cows)
  6.     {
  7.         int b = 0;
  8.         int c = 0;
  9.         List<int> bPos = new List<int> { };
  10.  
  11.         //count the bulls
  12.         for (int i = 0; i < 4; i++)
  13.         {
  14.             if (n1[i] == n2[i])
  15.             {
  16.                 b++;
  17.                 if (!bPos.Contains(i))
  18.                 {
  19.                     bPos.Add(i);
  20.                 }
  21.             }
  22.         }
  23.         string newN1 = "";
  24.         string newN2 = "";
  25.         for (int i = 0; i < 4; i++)
  26.         {
  27.             if (n1[i] != n2[i])
  28.             {
  29.                 newN1 += n1[i];
  30.                 newN2 += n2[i];
  31.             }
  32.         }
  33.         List<int> cPos = new List<int> { };
  34.         for (int i = 0; i < 4 - b; i++)
  35.         {
  36.             char candidate = newN2[i];
  37.             if (newN1.Contains(candidate.ToString()))
  38.             {
  39.                 int index = newN1.IndexOf(candidate);
  40.                 if (!cPos.Contains(index))
  41.                 {
  42.                     c++;
  43.                     cPos.Add(index);
  44.                 }
  45.             }
  46.         }
  47.        
  48.         if (b == bulls && c == cows) //check the bulls and the cows
  49.         {
  50.             return true;
  51.         }
  52.         return false;
  53.     }
  54.  
  55.     public static void Main()
  56.     {
  57.         string n1 = Console.ReadLine();
  58.         int bulls = int.Parse(Console.ReadLine());
  59.         int cows = int.Parse(Console.ReadLine());
  60.         bool isFound = true;
  61.         for (int i = 1000; i < 10000; i++)
  62.         {
  63.             if (!i.ToString().Contains("0") && Compare(i.ToString(), n1, bulls, cows))
  64.             {
  65.                 Console.Write(i + " ");
  66.                 isFound = false;
  67.             }
  68.         }
  69.         if (isFound)
  70.         {
  71.             Console.WriteLine("No");
  72.         }
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement