ivanov_ivan

EqualPairs

Aug 18th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.02 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace EqualPairs
  8. {
  9.     class EqualPairs
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int chisloto = int.Parse(Console.ReadLine());
  14.  
  15.             int counter = 0;
  16.  
  17.             var sum = 0;
  18.             var currentSum = 0;
  19.  
  20.             var biggestDiff = 0;
  21.  
  22.             bool equalSums = true;
  23.             bool haveASum = false; //flag
  24.  
  25.             for(int i = 0; i < chisloto * 2; i++)
  26.             {
  27.                 if (counter < 2)
  28.                 {
  29.                     int number = int.Parse(Console.ReadLine());
  30.                     currentSum += number;
  31.                     counter++;
  32.                 }
  33.                 if(haveASum)
  34.                 {
  35.                     if(sum != currentSum && counter == 2)
  36.                     {
  37.                         equalSums = false;
  38.                         var difference = Math.Abs(sum - currentSum);
  39.                         if(biggestDiff < difference)
  40.                         {
  41.                             biggestDiff = difference;
  42.                         }
  43.                         sum = currentSum;
  44.                         currentSum = 0;
  45.                     }
  46.                     else if(counter == 2)
  47.                     {
  48.                         sum = currentSum;
  49.                         currentSum = 0;
  50.                     }
  51.                 }
  52.                 else if(counter == 2 && !haveASum)
  53.                 {
  54.                     haveASum = true;
  55.                     sum = currentSum;
  56.                     currentSum = 0;
  57.                 }
  58.  
  59.                 if(counter == 2)
  60.                 {
  61.                     counter = 0;
  62.                 }
  63.             }
  64.  
  65.             if(equalSums)
  66.             {
  67.                 Console.WriteLine("Yes, value={0}" , sum);
  68.             }
  69.             else
  70.             {
  71.                 Console.WriteLine("No, maxdiff={0}" , biggestDiff);
  72.             }
  73.         }
  74.     }
  75. }
Add Comment
Please, Sign In to add comment