SHOW:
|
|
- or go back to the newest paste.
| 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. */
|
| 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 | - | public class BiggestTripple |
| 6 | + | * In case there are several triples with the same biggest sum, print the leftmost of them. */ |
| 7 | ||
| 8 | ||
| 9 | using System; | |
| 10 | - | string input = Console.ReadLine(); |
| 10 | + | |
| 11 | public class BiggestTriple | |
| 12 | {
| |
| 13 | public static void Main() | |
| 14 | {
| |
| 15 | string input = Console.ReadLine(); | |
| 16 | - | // Initialize maximal tripple with three empty strings ("").
|
| 16 | + | |
| 17 | - | string[] maxTripple = new string[3]; |
| 17 | + | |
| 18 | - | |
| 18 | + | |
| 19 | - | // Initialize maximal sum to the minimal value of the Int32 type. |
| 19 | + | |
| 20 | - | int maxSum = int.MinValue; |
| 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 | - | // Initialize current tripple with three empty strings ("").
|
| 30 | + | |
| 31 | - | string[] currentTripple = new string[3]; |
| 31 | + | |
| 32 | {
| |
| 33 | int currentSum = 0; | |
| 34 | ||
| 35 | - | // If i is valid index in the array of elements, assign it to the current tripple. |
| 35 | + | // Initialize current triple with three empty strings ("").
|
| 36 | string[] currentTriple = new string[3]; | |
| 37 | ||
| 38 | - | // Assign at index 0, 1 or 2 in the current tripple. |
| 38 | + | |
| 39 | - | currentTripple[i % 3] = elements[i]; |
| 39 | + | |
| 40 | // If i is valid index in the array of elements, assign it to the current triple. | |
| 41 | - | // Collect the sum of the current tripple. |
| 41 | + | |
| 42 | - | currentSum += int.Parse(currentTripple[i % 3]); |
| 42 | + | |
| 43 | // Assign at index 0, 1 or 2 in the current triple. | |
| 44 | currentTriple[i % 3] = elements[i]; | |
| 45 | - | // Else the element of the current tripple is left empty string ("").
|
| 45 | + | |
| 46 | // Collect the sum of the current triple. | |
| 47 | - | |
| 47 | + | currentSum += int.Parse(currentTriple[i % 3]); |
| 48 | - | // Assign current tripple to the maximal tripple, if it's sum is greater than |
| 48 | + | |
| 49 | ||
| 50 | - | // As the comparison is '>', we always keep the leftmost tripple |
| 50 | + | // Else the element of the current triple is left empty string ("").
|
| 51 | - | // in case of the tripples with same sum. */ |
| 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 | - | // Get the values of the current tripple as maximal tripple. |
| 56 | + | // in case of the triples with same sum. */ |
| 57 | if (currentSum > maxSum) | |
| 58 | {
| |
| 59 | - | maxTripple[i] = currentTripple[i]; |
| 59 | + | |
| 60 | ||
| 61 | // Get the values of the current triple as maximal triple. | |
| 62 | - | |
| 62 | + | |
| 63 | - | // Advance to the start index of the next tripple. |
| 63 | + | |
| 64 | maxTriple[i] = currentTriple[i]; | |
| 65 | - | } |
| 65 | + | |
| 66 | - | |
| 66 | + | |
| 67 | - | // Print the contents of the maximal tripple array, |
| 67 | + | |
| 68 | - | // converting it to string, containing all elements of the array, separated by space. |
| 68 | + | // Advance to the start index of the next triple. |
| 69 | - | Console.WriteLine(string.Join(" ", maxTripple));
|
| 69 | + | |
| 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 | } |