Advertisement
Guest User

Biggest Triple

a guest
Apr 21st, 2014
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* We are given n numbers, e.g. {3, 7, 2, 8, 1, 4, 6, 9}. We split them into triples:
  2.  * sequences of 3 consecutive numbers (except the last sequence that could have 1 or 2 numbers).
  3.  * In our example, the numbers are split into these triples: the first three numbers {3, 7, 2};
  4.  * the second three numbers {8, 1, 4}; the last two numbers {6, 9}. Write a program that enters n numbers
  5.  * and finds the triple with biggest sum of numbers. In our example this is the last triple: {6, 9}.
  6.  * In case there are several triples with the same biggest sum, print the leftmost of them. */
  7.  
  8.  
  9. using System;
  10.  
  11. public class BiggestTriple
  12. {
  13.     public static void Main()
  14.     {
  15.         string input = Console.ReadLine();
  16.  
  17.         // Create array of strings, splitting the input by space (' ').
  18.         // Elements of the array might be strings like "5" or "-5".
  19.         string[] elements = input.Split(' ');
  20.  
  21.         // Initialize maximal triple with three empty strings ("").      
  22.         string[] maxTriple = new string[3];
  23.  
  24.         // Initialize maximal sum to the minimal value of the Int32 type.
  25.         int maxSum = int.MinValue;
  26.  
  27.         // Initialize index to iterate through the array of elements.
  28.         int index = 0;
  29.  
  30.         // Start to iterate through the elements of the array of elements.
  31.         while (index < elements.Length)
  32.         {
  33.             int currentSum = 0;
  34.  
  35.             // Initialize current triple with three empty strings ("").
  36.             string[] currentTriple = new string[3];
  37.  
  38.             for (int i = index; i < index + 3; i++)
  39.             {
  40.                 // If i is valid index in the array of elements, assign it to the current triple.                
  41.                 if (i < elements.Length)
  42.                 {
  43.                     // Assign at index 0, 1 or 2 in the current triple.
  44.                     currentTriple[i % 3] = elements[i];
  45.  
  46.                     // Collect the sum of the current triple.                    
  47.                     currentSum += int.Parse(currentTriple[i % 3]);
  48.                 }
  49.  
  50.                 // Else the element of the current triple is left empty string ("").
  51.             }
  52.  
  53.             // Assign current triple to the maximal triple, if it's sum is greater than
  54.             // the sum of the maximal triple, found so far.
  55.             // As the comparison is '>', we always keep the leftmost triple
  56.             // in case of the triples with same sum. */
  57.             if (currentSum > maxSum)
  58.             {
  59.                 maxSum = currentSum;
  60.  
  61.                 // Get the values of the current triple as maximal triple.                
  62.                 for (int i = 0; i < 3; i++)
  63.                 {
  64.                     maxTriple[i] = currentTriple[i];
  65.                 }
  66.             }
  67.  
  68.             // Advance to the start index of the next triple.
  69.             index += 3;
  70.         }
  71.  
  72.         // Print the contents of the maximal triple array,
  73.         // converting it to string, containing all elements of the array, separated by space.
  74.         Console.WriteLine(string.Join(" ", maxTriple));
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement