Advertisement
cortez

SumOfElements

Sep 7th, 2015
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.11 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Problem_14__Sum_of_Elements
  4. {
  5.     class SumOfElements
  6.     {
  7.         public static void Main ()
  8.         {
  9.             string input = Console.ReadLine ();
  10.             string[] numbers = input.Split (' ');
  11.  
  12.             long max = FindMaxInArray (numbers);
  13.             long sum = FindSumOfArray (numbers);
  14.  
  15.             string output = CheckIfEqual (sum, max);
  16.  
  17.             Console.WriteLine (output);
  18.  
  19.         }
  20.  
  21.         public static long FindMaxInArray (string[] numbers)
  22.         {
  23.             long maxValue = int.MinValue;
  24.  
  25.             for (int i = 0; i < numbers.Length; i++)
  26.             {
  27.                 int number = int.Parse (numbers [i]);
  28.                 if (number > maxValue)
  29.                 {
  30.                     maxValue = number;
  31.                 }
  32.             }
  33.  
  34.             return maxValue;
  35.         }
  36.  
  37.         static long FindSumOfArray (string[] numbers)
  38.         {
  39.             long sum = 0;
  40.  
  41.             for (int i = 0; i < numbers.Length; i++)
  42.             {
  43.                 sum += int.Parse (numbers [i]);
  44.             }
  45.  
  46.             return sum;
  47.         }
  48.  
  49.         static string CheckIfEqual (long sum, long max)
  50.         {
  51.             long maxDoubled = 2 * max;
  52.  
  53.             if (sum == maxDoubled)
  54.             {
  55.                 return "Yes, sum=" + max;
  56.             }
  57.             else
  58.             {
  59.                 long diff = Math.Abs (sum - maxDoubled);
  60.                 return "No, diff=" + diff;
  61.             }
  62.         }
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement