Advertisement
tr00per92

SumOfElements

Apr 15th, 2014
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.25 KB | None | 0 0
  1. using System;
  2.  
  3. class Program
  4. {
  5.     static void Main()
  6.     {
  7.         string[] input = Console.ReadLine().Split();
  8.         bool yes = false;
  9.         long result = long.MaxValue;
  10.         for (int i = 0; i < input.Length; i++)
  11.         {
  12.             long sum = 0;
  13.             long current = long.Parse(input[i]);
  14.             for (int j = 0; j < input.Length; j++)
  15.             {              
  16.                 if (j != i)
  17.                 {
  18.                     sum += long.Parse(input[j]);
  19.                 }                                
  20.             }
  21.             if (current == sum)
  22.             {
  23.                 result = current;
  24.                 yes = true;
  25.                 break;
  26.             }            
  27.             else
  28.             {
  29.                 long difference = (sum - current);
  30.                 difference = Math.Abs(difference);
  31.                 if (difference < result)
  32.                 {
  33.                     result = difference;
  34.                 }
  35.             }
  36.         }
  37.         if(yes)
  38.         {
  39.             Console.Write("Yes, ");
  40.             Console.WriteLine("sum={0}", result);
  41.         }
  42.         else
  43.         {
  44.             Console.Write("No, ");
  45.             Console.WriteLine("diff={0}", result);
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement