Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class Presents
- {
- static void Main()
- {
- //Two rectangular presents should be positioned on the smallest possible rectangular table without overlapping.
- //If there are 2 possible solutions, the one with the smallest perimeter is the result.
- //Read the console input of the 4 sides of the presents.
- string inputDimensions = Console.ReadLine();
- //Split the input string by space
- string[] inputSplitted = inputDimensions.Split(' ');
- //convert the string into numbers and save them in an array
- int[] dimensions = Array.ConvertAll<string, int>(inputSplitted, int.Parse);
- int smallSide = 0,
- bigSide = 0;
- //Go through all possible cases
- //case 1
- // -----
- // b |---
- // | | d
- // --a-- -c-
- int big1 = dimensions[0] + dimensions[2],
- small1 = (dimensions[1] >= dimensions[3] ? dimensions[1] : dimensions[3]),
- s1 = big1 * small1,
- p1 = 2 * big1 + 2 * small1;
- //case 2
- // -----
- // b |---
- // | | c
- // --a-- -d-
- int big2 = dimensions[0] + dimensions[3],
- small2 = (dimensions[1] >= dimensions[2] ? dimensions[1] : dimensions[2]),
- s2 = big2 * small2,
- p2 = 2 * big2 + 2 * small2;
- //case 3
- // -----
- // a |---
- // | | d
- // --b-- -c-
- int big3 = dimensions[1] + dimensions[2],
- small3 = (dimensions[0] >= dimensions[3] ? dimensions[0] : dimensions[3]),
- s3 = big3 * small3,
- p3 = 2 * big3 + 2 * small3;
- //case 4
- // -----
- // a |---
- // | | c
- // --b-- -d-
- int big4 = dimensions[1] + dimensions[3],
- small4 = (dimensions[0] >= dimensions[2] ? dimensions[0] : dimensions[2]),
- s4 = big4 * small4,
- p4 = 2 * big4 + 2 * small4;
- //Finds the smallest area S
- int smallestS = Math.Min(Math.Min(s1, s2), Math.Min(s3, s4));
- //Checks if s1 is the smallest area
- if (s1 < s2 && s1 < s3 && s1 < s4)
- {
- smallSide = Math.Min(small1, big1);
- bigSide = Math.Max(small1,big1);
- }
- //Checks if s2 is the smallest area
- else if (s2 < s1 && s2 < s3 && s2 < s4)
- {
- smallSide = Math.Min(small2, big2);
- bigSide = Math.Max(small2,big2);
- }
- //Checks if s3 is the smallest area
- else if (s3 < s1 && s3 < s2 && s3 < s4)
- {
- smallSide = Math.Min(small3, big3);
- bigSide = Math.Max(small3, big3);
- }
- //Checks if s4 is the smallest area
- else if (s4 < s1 && s4 < s2 && s4 < s3)
- {
- smallSide = Math.Min(small4, big4);
- bigSide = Math.Max(small4, big4);
- }
- //Looks into cases where we have equal areas and checks if that is the smallest possible area. In these cases the smallest perimeter p is the correct solution.
- if (s1 == s2 && s1 == smallestS)
- {
- if (p1 < p2)
- {
- smallSide = Math.Min(small1, big1);
- bigSide = Math.Max(small1, big1);
- }
- else
- {
- smallSide = Math.Min(small2, big2);
- bigSide = Math.Max(small2, big2);
- }
- }
- if (s1 == s3 && s1 == smallestS)
- {
- if (p1 < p3)
- {
- smallSide = Math.Min(small1, big1);
- bigSide = Math.Max(small1, big1);
- }
- else
- {
- smallSide = Math.Min(small3, big3);
- bigSide = Math.Max(small3, big3);
- }
- }
- if (s1 == s4 && s1 == smallestS)
- {
- if (p1 < p4)
- {
- smallSide = Math.Min(small1, big1);
- bigSide = Math.Max(small1, big1);
- }
- else
- {
- smallSide = Math.Min(small4, big4);
- bigSide = Math.Max(small4, big4);
- }
- }
- if (s2 == s3 && s2 == smallestS)
- {
- if (p2 < p3)
- {
- smallSide = Math.Min(small2, big2);
- bigSide = Math.Max(small2, big2);
- }
- else
- {
- smallSide = Math.Min(small3, big3);
- bigSide = Math.Max(small3, big3);
- }
- }
- if (s2 == s4 && s2 == smallestS)
- {
- if (p2 < p4)
- {
- smallSide = Math.Min(small2, big2);
- bigSide = Math.Max(small2, big2);
- }
- else
- {
- smallSide = Math.Min(small4, big4);
- bigSide = Math.Max(small4, big4);
- }
- }
- if (s3 == s4 && s3 == smallestS)
- {
- if (p3 < p4)
- {
- smallSide = Math.Min(small3, big3);
- bigSide = Math.Max(small3, big3);
- }
- else
- {
- smallSide = Math.Min(small4, big4);
- bigSide = Math.Max(small4, big4);
- }
- }
- //Prints on the console the result
- Console.WriteLine("{0} {1}", smallSide,bigSide);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment