As I have mentioned here, I have participated in a competition for C# programmers. The second problem was concerning something quite standard – finding the biggest triple from a line of numbers. With other words, one get as an input the following numbers:
2 3 4 3 3 3 |
The program runs and gives as input the three sequential numbers, which have the highest sum. In our case this is 2 3 4 because it is the leftmost sequence as well (part of the description of the problem).
So, I have started with the problem quite well – I managed to note that we do not have a number, telling us how many numbers should we have in a column, thus I have decided to record all of the values as string. After this I simply recorded the values of the string into an array, splited by the space and the rest was quite easy – assigning the highest sum comparing it with the sum of the next digits.
Thus, within about 40 minutes I was able to have 100 points from this problem. However, I liked the fact that there were tests with negative numbers and thus I have failed some of the tests initially:
Anyway, at this task my code looked more presentable and understandable. Here it is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; class Program { static void Main() { string myInput = Console.ReadLine(); string[] myArray = myInput.Split(' '); int myArrayLength = myArray.Count(); int sum = -3333; int newSum = -3333; int a = 0; int b = 0; int c = 0; int x = 0; int y = 0; int z = 0; if (myArrayLength % 3 == 0) { for (int i = 0; i < myArrayLength; i++) { if ((i + 1) % 3 == 0) { x = int.Parse(myArray[i]); y = int.Parse(myArray[i - 1]); z = int.Parse(myArray[i - 2]); newSum = x + y + z; if (newSum > sum) { sum = newSum; a = z; b = y; c = x; } } } Console.WriteLine("{0} {1} {2}", a, b, c); } else if (myArrayLength == 1) { Console.WriteLine("{0}", int.Parse(myArray[0])); } else if (myArrayLength % 3 == 1) { for (int i = 0; i < myArrayLength-1; i++) { if ((i + 1) % 3 == 0) { x = int.Parse(myArray[i]); y = int.Parse(myArray[i - 1]); z = int.Parse(myArray[i - 2]); newSum = x + y + z; if (newSum > sum) { sum = newSum; a = z; b = y; c = x; } } } if (int.Parse((myArray[myArrayLength-1]))>sum) { Console.WriteLine(myArray[myArrayLength-1]); } else { Console.WriteLine("{0} {1} {2}", a, b, c); } } else if (myArrayLength % 3 == 2) { for (int i = 0; i < myArrayLength - 2; i++) { if ((i + 1) % 3 == 0) { x = int.Parse(myArray[i]); y = int.Parse(myArray[i - 1]); z = int.Parse(myArray[i - 2]); newSum = x + y + z; if (newSum > sum) { sum = newSum; a = z; b = y; c = x; } } } if ((int.Parse((myArray[myArrayLength - 1])) + int.Parse((myArray[myArrayLength - 2])) )> sum) { Console.WriteLine("{0} {1}", myArray[myArrayLength - 2], myArray[myArrayLength - 1]); } else { Console.WriteLine("{0} {1} {2}", a, b, c); } } } } |
And the original solution from the author is present here:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
using System; class BiggestTriple { static void Main() { string inputLine = Console.ReadLine(); string[] numbers = inputLine.Split(' '); int index = 0; int maxSum = Int32.MinValue; int start = 0; while (index < numbers.Length) { int num1 = int.Parse(numbers[index]); int num2 = 0; if (index + 1 < numbers.Length) { num2 = int.Parse(numbers[index + 1]); } int num3 = 0; if (index + 2 < numbers.Length) { num3 = int.Parse(numbers[index + 2]); } int sum = num1 + num2 + num3; if (sum > maxSum) { maxSum = sum; start = index; } index = index + 3; } // Print the result while (maxSum != 0) { Console.Write(numbers[start]); maxSum = maxSum - int.Parse(numbers[start]); start++; if (maxSum != 0) { Console.Write(" "); } } } } |
Well so far so good 🙂