sylviapsh

Presents on a rectangular table

Dec 26th, 2012
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.59 KB | None | 0 0
  1. using System;
  2. class Presents
  3. {
  4.   static void Main()
  5.   {
  6.     //Two rectangular presents should be positioned on the smallest possible rectangular table without overlapping.
  7.     //If there are 2 possible solutions, the one with the smallest perimeter is the result.
  8.    
  9.     //Read the console input of the 4 sides of the presents.
  10.     string inputDimensions = Console.ReadLine();
  11.  
  12.     //Split the input string by space
  13.     string[] inputSplitted = inputDimensions.Split(' ');
  14.     //convert the string into numbers and save them in an array
  15.     int[] dimensions = Array.ConvertAll<string, int>(inputSplitted, int.Parse);
  16.     int smallSide = 0,
  17.         bigSide = 0;
  18.  
  19.     //Go through all possible cases
  20.     //case 1
  21.     // -----
  22.     // b    |---
  23.     // |    |  d
  24.     // --a-- -c-
  25.     int big1 = dimensions[0] + dimensions[2],
  26.       small1 = (dimensions[1] >= dimensions[3] ? dimensions[1] : dimensions[3]),
  27.       s1 = big1 * small1,
  28.       p1 = 2 * big1 + 2 * small1;
  29.  
  30.     //case 2
  31.     // -----
  32.     // b    |---
  33.     // |    |  c
  34.     // --a-- -d-
  35.     int big2 = dimensions[0] + dimensions[3],
  36.       small2 = (dimensions[1] >= dimensions[2] ? dimensions[1] : dimensions[2]),
  37.       s2 = big2 * small2,
  38.       p2 = 2 * big2 + 2 * small2;
  39.  
  40.     //case 3
  41.     // -----
  42.     // a    |---
  43.     // |    |  d
  44.     // --b-- -c-
  45.     int big3 = dimensions[1] + dimensions[2],
  46.       small3 = (dimensions[0] >= dimensions[3] ? dimensions[0] : dimensions[3]),
  47.       s3 = big3 * small3,
  48.       p3 = 2 * big3 + 2 * small3;
  49.  
  50.     //case 4
  51.     // -----
  52.     // a    |---
  53.     // |    |  c
  54.     // --b-- -d-
  55.     int big4 = dimensions[1] + dimensions[3],
  56.       small4 = (dimensions[0] >= dimensions[2] ? dimensions[0] : dimensions[2]),
  57.       s4 = big4 * small4,
  58.       p4 = 2 * big4 + 2 * small4;
  59.  
  60.     //Finds the smallest area S
  61.     int smallestS = Math.Min(Math.Min(s1, s2), Math.Min(s3, s4));
  62.  
  63.     //Checks if s1 is the smallest area
  64.     if (s1 < s2 && s1 < s3 && s1 < s4)
  65.     {
  66.       smallSide = Math.Min(small1, big1);
  67.       bigSide = Math.Max(small1,big1);
  68.     }
  69.     //Checks if s2 is the smallest area
  70.     else if (s2 < s1 && s2 < s3 && s2 < s4)
  71.     {
  72.       smallSide = Math.Min(small2, big2);
  73.       bigSide = Math.Max(small2,big2);
  74.     }
  75.     //Checks if s3 is the smallest area
  76.     else if (s3 < s1 && s3 < s2 && s3 < s4)
  77.     {
  78.       smallSide = Math.Min(small3, big3);
  79.       bigSide = Math.Max(small3, big3);
  80.     }
  81.     //Checks if s4 is the smallest area
  82.     else if (s4 < s1 && s4 < s2 && s4 < s3)
  83.     {
  84.       smallSide = Math.Min(small4, big4);
  85.       bigSide = Math.Max(small4, big4);
  86.     }
  87.  
  88.     //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.
  89.    
  90.     if (s1 == s2 && s1 == smallestS)
  91.     {
  92.       if (p1 < p2)
  93.       {
  94.         smallSide = Math.Min(small1, big1);
  95.         bigSide = Math.Max(small1, big1);
  96.       }
  97.       else
  98.       {
  99.         smallSide = Math.Min(small2, big2);
  100.         bigSide = Math.Max(small2, big2);
  101.       }
  102.     }
  103.     if (s1 == s3 && s1 == smallestS)
  104.     {
  105.       if (p1 < p3)
  106.       {
  107.         smallSide = Math.Min(small1, big1);
  108.         bigSide = Math.Max(small1, big1);
  109.       }
  110.       else
  111.       {
  112.         smallSide = Math.Min(small3, big3);
  113.         bigSide = Math.Max(small3, big3);
  114.       }
  115.     }
  116.     if (s1 == s4 && s1 == smallestS)
  117.     {
  118.       if (p1 < p4)
  119.       {
  120.         smallSide = Math.Min(small1, big1);
  121.         bigSide = Math.Max(small1, big1);
  122.       }
  123.       else
  124.       {
  125.         smallSide = Math.Min(small4, big4);
  126.         bigSide = Math.Max(small4, big4);
  127.       }
  128.     }
  129.     if (s2 == s3 && s2 == smallestS)
  130.     {
  131.       if (p2 < p3)
  132.       {
  133.         smallSide = Math.Min(small2, big2);
  134.         bigSide = Math.Max(small2, big2);
  135.       }
  136.       else
  137.       {
  138.         smallSide = Math.Min(small3, big3);
  139.         bigSide = Math.Max(small3, big3);
  140.       }
  141.     }
  142.     if (s2 == s4 && s2 == smallestS)
  143.     {
  144.       if (p2 < p4)
  145.       {
  146.         smallSide = Math.Min(small2, big2);
  147.         bigSide = Math.Max(small2, big2);
  148.       }
  149.       else
  150.       {
  151.         smallSide = Math.Min(small4, big4);
  152.         bigSide = Math.Max(small4, big4);
  153.       }
  154.     }
  155.     if (s3 == s4 && s3 == smallestS)
  156.     {
  157.       if (p3 < p4)
  158.       {
  159.         smallSide = Math.Min(small3, big3);
  160.         bigSide = Math.Max(small3, big3);
  161.       }
  162.       else
  163.       {
  164.         smallSide = Math.Min(small4, big4);
  165.         bigSide = Math.Max(small4, big4);
  166.       }
  167.     }
  168.  
  169.   //Prints on the console the result
  170.     Console.WriteLine("{0} {1}", smallSide,bigSide);
  171.   }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment