Advertisement
Guest User

Biggest Tripple

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