Advertisement
dimipan80

Exam July Evening 4. Nakovs Matching

Jul 27th, 2014
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.32 KB | None | 0 0
  1. using System;
  2. using System.Globalization;
  3. using System.Threading;
  4.  
  5. public class NakovsMatching
  6. {
  7.     public static void Main()
  8.     {
  9.         Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
  10.         checked
  11.         {
  12.             string wordFirst = Console.ReadLine();
  13.             string wordSecond = Console.ReadLine();
  14.             int distance = int.Parse(Console.ReadLine());
  15.  
  16.             bool foundMatchings = false;
  17.             int firstSumLeft = 0;
  18.             string firstLeftStr = string.Empty;
  19.             for (int i = 0; i < wordFirst.Length - 1; i++)            
  20.             {                
  21.                 firstLeftStr += wordFirst[i].ToString();
  22.                 firstSumLeft += wordFirst[i];
  23.                 int firstSumRight = 0;
  24.                 string firstRightStr = string.Empty;
  25.                 for (int j = i + 1; j < wordFirst.Length; j++)
  26.                 {
  27.                     firstRightStr += wordFirst[j].ToString();
  28.                     firstSumRight += wordFirst[j];                                        
  29.                 }
  30.  
  31.                 int secondSumLeft = 0;
  32.                 string secondLeftStr = string.Empty;
  33.                 for (int k = 0; k < wordSecond.Length - 1; k++)
  34.                 {
  35.                     secondLeftStr += wordSecond[k].ToString();
  36.                     secondSumLeft += wordSecond[k];
  37.                     int secondSumRight = 0;
  38.                     string secondRightStr = string.Empty;
  39.                     for (int l = k + 1; l < wordSecond.Length; l++)
  40.                     {
  41.                         secondRightStr += wordSecond[l].ToString();
  42.                         secondSumRight += wordSecond[l];                                                
  43.                     }
  44.  
  45.                     int result = Math.Abs((firstSumLeft * secondSumRight) - (firstSumRight * secondSumLeft));
  46.                     if (result <= distance)
  47.                     {
  48.                         foundMatchings = true;
  49.                         Console.WriteLine("({0}|{1}) matches ({2}|{3}) by {4} nakovs", firstLeftStr, firstRightStr, secondLeftStr, secondRightStr, result);
  50.                     }
  51.                 }
  52.             }
  53.  
  54.             if (!foundMatchings)
  55.             {
  56.                 Console.WriteLine("No");
  57.             }            
  58.         }
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement