Advertisement
dimipan80

Exam 7. Biggest Triple

Jun 20th, 2014
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.17 KB | None | 0 0
  1. namespace _2.BiggestTriple
  2. {
  3.     using System;
  4.  
  5.     public class BiggestTriple
  6.     {
  7.         public static void Main(string[] args)
  8.         {
  9.             checked
  10.             {
  11.                 string inputLine = Console.ReadLine();
  12.  
  13.                 int[] nums = SplitInputStringAndCreateArrayOfNumbers(inputLine);
  14.                 if (nums.Length > 3)
  15.                 {
  16.                     int maxSum = int.MinValue;
  17.                     int startIndex = 0;
  18.                     int remainder = nums.Length % 3;
  19.                     int maxIndex = nums.Length - remainder;
  20.                     for (int i = 0; i + 2 < maxIndex; i += 3)
  21.                     {
  22.                         int currentSum = nums[i] + nums[i + 1] + nums[i + 2];
  23.                         if (currentSum > maxSum)
  24.                         {
  25.                             maxSum = currentSum;
  26.                             startIndex = i;
  27.                         }
  28.                     }
  29.  
  30.                     int currentSum2 = CalculateSumFromRemaindSequence(nums, maxSum, remainder);
  31.                    
  32.                     if (maxSum >= currentSum2)
  33.                     {
  34.                         for (int i = startIndex; i <= startIndex + 2; i++)
  35.                         {
  36.                             Console.Write("{0} ", nums[i]);
  37.                         }
  38.                     }
  39.                 }
  40.                 else
  41.                 {
  42.                     foreach (var num in nums)
  43.                     {
  44.                         Console.Write("{0} ", num);
  45.                     }
  46.                 }
  47.             }
  48.         }
  49.  
  50.         private static int CalculateSumFromRemaindSequence(int[] nums, int maxSum, int remainder)
  51.         {
  52.             checked
  53.             {
  54.                 int currentSum = int.MinValue;
  55.                 if (remainder > 0)
  56.                 {
  57.                     if (remainder == 1)
  58.                     {
  59.                         currentSum = nums[nums.Length - 1];
  60.                         if (currentSum > maxSum)
  61.                         {
  62.                             Console.WriteLine(nums[nums.Length - 1]);
  63.                         }
  64.                     }
  65.                     else if (remainder == 2)
  66.                     {
  67.                         currentSum = nums[nums.Length - 1] + nums[nums.Length - 2];
  68.                         if (currentSum > maxSum)
  69.                         {
  70.                             Console.WriteLine("{0} {1}", nums[nums.Length - 2], nums[nums.Length - 1]);
  71.                         }
  72.                     }
  73.                 }
  74.  
  75.                 return currentSum;
  76.             }
  77.         }
  78.  
  79.         private static int[] SplitInputStringAndCreateArrayOfNumbers(string inputStr)
  80.         {
  81.             checked
  82.             {
  83.                 char[] separators = new char[] { ' ', ',', ';' };
  84.                 string[] numStr = inputStr.Split(separators, StringSplitOptions.RemoveEmptyEntries);
  85.                 int[] arr = new int[numStr.Length];
  86.                 for (int i = 0; i < numStr.Length; i++)
  87.                 {
  88.                     arr[i] = int.Parse(numStr[i]);
  89.                 }
  90.  
  91.                 return arr;
  92.             }
  93.         }
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement