Advertisement
dimipan80

Stuck Numbers

May 7th, 2015
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.29 KB | None | 0 0
  1. /* You are given n numbers. Write a program to find among these numbers all sets of 4 numbers {a, b, c, d}, such that a|b == c|d, where a ≠ b ≠ c ≠ d. Assume that "a|b" means to append the number b after a. We call these numbers {a, b, c, d} stuck numbers: if we append a and b, we get the same result as if we appended c and d. Note that the numbers a, b, c and d should be distinct (a ≠ b ≠ c ≠ d).
  2.  * Print at the console all stuck numbers {a, b, c, d} found in the input sequence in format "a|b==c|d" (without any spaces), each at a separate line. The order of the output lines is not important. Print "No" in case no stuck numbers exist among the input sequence of numbers. */
  3.  
  4. namespace _09.StuckNumbers
  5. {
  6.     using System;
  7.  
  8.     class StuckNumbers
  9.     {
  10.         private static bool _isFoundStuckNum = false;
  11.  
  12.         static void Main(string[] args)
  13.         {
  14.             int count = int.Parse(Console.ReadLine());
  15.             string[] numbers = Console.ReadLine().Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
  16.  
  17.             foreach (string a in numbers)
  18.             {
  19.                 foreach (string b in numbers)
  20.                 {
  21.                     if (b != a)
  22.                     {
  23.                         foreach (string c in numbers)
  24.                         {
  25.                             if (c != a && c != b)
  26.                             {
  27.                                 foreach (string d in numbers)
  28.                                 {
  29.                                     if (d != a && d != b && d != c)
  30.                                     {
  31.                                         CheckForStuckNumber(a, b, c, d);
  32.                                     }
  33.                                 }
  34.                             }
  35.                         }
  36.                     }
  37.                 }
  38.             }
  39.  
  40.             if (!_isFoundStuckNum)
  41.             {
  42.                 Console.WriteLine("No");
  43.             }
  44.         }
  45.  
  46.         private static void CheckForStuckNumber(string numA, string numB, string numC, string numD)
  47.         {
  48.             if ((numA + numB).Equals((numC + numD)))
  49.             {
  50.                 _isFoundStuckNum = true;
  51.                 Console.WriteLine("{0}|{1}=={2}|{3}", numA, numB, numC, numD);
  52.             }
  53.         }
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement