Advertisement
dimipan80

Mirror Numbers

May 18th, 2015
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.00 KB | None | 0 0
  1. /* You are given n 4-digit numbers. Write a program to find among these numbers all pairs of mirror numbers, such that the reversed positions of digits in the first number are equal to the positions of digits of the second number. Note that both numbers should be distinct (a ≠ b). Put the sign “<!>” between the numbers.
  2.  * The first line holds the count n. The next line holds n 4-digit integer numbers, separated by a space. The input numbers will be distinct (no duplicates are allowed).
  3.  * Print at the console all mirror numbers {a, b} found in the input sequence in format "a<!>b" (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. */
  4.  
  5. namespace Mirror_Numbers
  6. {
  7.     using System;
  8.     using System.Linq;
  9.  
  10.     class MirrorNumbers
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             int n = int.Parse(Console.ReadLine());
  15.             int[] numbers = Console.ReadLine().Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
  16.  
  17.             bool isFoundMirror = false;
  18.             for (int i = 0; i < numbers.Length; i++)
  19.             {
  20.                 for (int j = 0; j < numbers.Length; j++)
  21.                 {
  22.                     if (j == i) continue;
  23.  
  24.                     string strA = numbers[i].ToString();
  25.                     string strB = numbers[j].ToString();
  26.                     if (strA.Length != strB.Length || strA.Length != 4 ||
  27.                         strA[0] != strB[3] || strA[1] != strB[2] ||
  28.                         strA[2] != strB[1] || strA[3] != strB[0]) continue;
  29.  
  30.                     isFoundMirror = true;
  31.                     Console.WriteLine("{0}<!>{1}", strA, strB);
  32.                     numbers[i] = 0;
  33.                     numbers[j] = 0;
  34.                 }
  35.             }
  36.  
  37.             if (!isFoundMirror)
  38.             {
  39.                 Console.WriteLine("No");
  40.             }
  41.         }
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement