As you have probably noticed, this is probably my 5th or article, concerning Java Exam problems from the SoftUni. Today, I will take a deeper look into a problem, which is supposed to calculate the best sum of triangles. The problem description is available here. Before going into the solution, I simply want to put 6 eclipse shortcuts, which may be useful for those who struggle with this platform. The tricky thing in Eclipse is that you should put a BreakPoint, in order to start the step by step debug. It took me quite a lot of time to learn it:
Shortcut Key |
Description |
F5 | Step Into the Function |
F6 | Next Step (as F8 in VBA) |
F7 | Step Out of the Function |
F8 | Go to next Breakpoint (as F5 in VBA) |
ALT + Shift + D + J | Debug Java Application |
Ctrl+ Shift + F | Auto Format |
I have tried the Excel shortcuts and I really learned quite a few from there – check my advanced list.
Back to the Java Problem – we have the following input and output requirements (copy + paste from the problem description):
Input |
Output |
Comments |
[3×3][3×2][4×3][1×4][5×3][3×1] | 31 | 4*3+1*4+5*3=31 |
What we need to do is to find a way to read the input line and to calculate the highest sum of consecutive triangles. Quite trivial, but the input seems to be problematic – we have three signs, which seem to be a little useless and still we need them to differentiate the triangles – ‘[‘ ‘x’ ‘]’.
As far as I was not able to solve the problem myself, I decided to take the original solution and to analyse it a little:
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 |
import java.util.*; public class Rectangulars { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String input = scanner.nextLine(); input = input.replace("[", ""); input = input.replace(" ", ""); String[] rects = input.split("]"); int[] areas = new int[rects.length]; for (int i = 0; i < rects.length; i++) { String rect = rects[i]; rect = rect.replace("[", ""); String[] sides = rect.split("x"); int firstSide = Integer.parseInt(sides[0]); int secondSide = Integer.parseInt(sides[1]); areas[i] = firstSide * secondSide; } // Calculate the max sequence of 3 rectangle areas int max = Integer.MIN_VALUE; for (int i = 2; i < areas.length; i++) { int sum = areas[i - 2] + areas[i - 1] + areas[i]; if (sum > max) { max = sum; } } System.out.println(max); } } |
Here is what we do:
- We read the first line;
- We remove the empty spaces and the ‘[‘ sign from the string we have read;
- We split the string using the ‘]’ as a divider;
- We initialize an array ‘areas’, with the length of the splitted units of the array ‘rects’;
- With the first for loop we fill the integer array areas with the result from the multiplication of the first side * second side;
- Once we have this, it is easier – we have the numbers in array and we should select the three highest consecutive;
- Due to the fact, that the minimum number of triangles is 3, it means that we should start our second for loop from 2;
- Thus, we sum one by one, recording the highest result in the variable ‘max’;
- Once the loop is over, we print the variable ‘max’;
So long!