Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace _2.BiggestTriple
- {
- using System;
- public class BiggestTriple
- {
- public static void Main(string[] args)
- {
- checked
- {
- string inputLine = Console.ReadLine();
- int[] nums = SplitInputStringAndCreateArrayOfNumbers(inputLine);
- if (nums.Length > 3)
- {
- int maxSum = int.MinValue;
- int startIndex = 0;
- int remainder = nums.Length % 3;
- int maxIndex = nums.Length - remainder;
- for (int i = 0; i + 2 < maxIndex; i += 3)
- {
- int currentSum = nums[i] + nums[i + 1] + nums[i + 2];
- if (currentSum > maxSum)
- {
- maxSum = currentSum;
- startIndex = i;
- }
- }
- int currentSum2 = CalculateSumFromRemaindSequence(nums, maxSum, remainder);
- if (maxSum >= currentSum2)
- {
- for (int i = startIndex; i <= startIndex + 2; i++)
- {
- Console.Write("{0} ", nums[i]);
- }
- }
- }
- else
- {
- foreach (var num in nums)
- {
- Console.Write("{0} ", num);
- }
- }
- }
- }
- private static int CalculateSumFromRemaindSequence(int[] nums, int maxSum, int remainder)
- {
- checked
- {
- int currentSum = int.MinValue;
- if (remainder > 0)
- {
- if (remainder == 1)
- {
- currentSum = nums[nums.Length - 1];
- if (currentSum > maxSum)
- {
- Console.WriteLine(nums[nums.Length - 1]);
- }
- }
- else if (remainder == 2)
- {
- currentSum = nums[nums.Length - 1] + nums[nums.Length - 2];
- if (currentSum > maxSum)
- {
- Console.WriteLine("{0} {1}", nums[nums.Length - 2], nums[nums.Length - 1]);
- }
- }
- }
- return currentSum;
- }
- }
- private static int[] SplitInputStringAndCreateArrayOfNumbers(string inputStr)
- {
- checked
- {
- char[] separators = new char[] { ' ', ',', ';' };
- string[] numStr = inputStr.Split(separators, StringSplitOptions.RemoveEmptyEntries);
- int[] arr = new int[numStr.Length];
- for (int i = 0; i < numStr.Length; i++)
- {
- arr[i] = int.Parse(numStr[i]);
- }
- return arr;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement