Advertisement
Guest User

Reddit java help

a guest
Nov 15th, 2012
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.43 KB | None | 0 0
  1. import java.io.* ;
  2. public class MclaLoops//Class
  3. {
  4.  
  5.   public static void main (String[] args) throws IOException //main method
  6.   {
  7.     BufferedReader objReader = new BufferedReader (new InputStreamReader(System.in));
  8.    
  9.     System.out.println("Please enter the cost for a purple ticket"); //input prompt for cost of purple ticket
  10.     int purpleTicket = Integer.parseInt ((objReader.readLine())); //varible decleration for purple ticket
  11.     System.out.println("Please enter the cost for a gray ticket"); //input prompt for cost of gray ticket
  12.     int grayTicket = Integer.parseInt ((objReader.readLine()));// varible decleration for gray ticket
  13.     System.out.println("Please enter the cost for a yellow ticket"); //inpur prompt for cost of yellow ticket
  14.     int yellowTicket =Integer.parseInt ((objReader.readLine())); //variable decleration for yellow ticket
  15.     System.out.println("Please enter the cost for a brown ticket"); // input prompt for cost of brown ticket
  16.     int brownTicket = Integer.parseInt ((objReader.readLine())); // variable decleration for brown ticket
  17.    
  18.     System.out.println("PLease enter the amount of money you would want to rasie"); // input for raisedMoney money to be raised in ticket sales
  19.     int raisedMoney = Integer.parseInt ((objReader.readLine ())); // variable decleration for the money that is to be raised
  20.     int ctr1 = 0; //counter one, counting how many brown tickets there will be
  21.     int ctr2 = 0; // counter two, counting how many yellow tickets there will be
  22.     int ctr3 = 0; // counter three, counting how many gray tickets there will be
  23.     int ctr4 = 0; //counter four, counting how many purple tickets there will be
  24.     int ctrCombo = 0; // counter to keep track of raisedMoney combinations
  25.     int brownMax = 0; // variable for max amount of brown tickets for a given raised amount
  26.     int yellowMax = 0; // variable for max amount of yellow tickets for a given raised amount
  27.     int grayMax = 0; // variable for max amount of gray tickets for a given raised amount
  28.     int purpleMax = 0;// variable for max amount of purple tickets for a given raised amount
  29.     int r = raisedMoney = 0; // declaring raised amount to r
  30.     int ctr = 0;
  31.     int min = 999999;
  32.     if (brownTicket <= r) // if the brown ticket cost is less than or equal to the raisedMoney raised amount then execute
  33.     {
  34.       r = raisedMoney;
  35.       brownMax = (r / brownTicket); // finding the maximum times the brown ticket can go into the raisedMoney raised amount
  36.     }
  37.     if (yellowTicket <= r)//if the yellow ticket cost is less than or equal to the raisedMoney raised amount then execute
  38.     {
  39.       r = raisedMoney;
  40.       yellowMax = (r / yellowTicket);// finding the maximum times the yellow ticket can go into the raisedMoney raised amount
  41.     }
  42.     if (grayTicket <= r) //if the gray ticket cost is less than or equal to the raisedMoney raised amount then execute
  43.     {
  44.       r = raisedMoney;
  45.       grayMax = (r / grayTicket);// finding the maximum times the gray ticket can go into the raisedMoney raised amount  
  46.     }
  47.     if (purpleTicket <= r) //if the purple ticket cost is less than or equal to the raisedMoney raised amount then execute
  48.     {
  49.       r = raisedMoney;
  50.       purpleMax = (r / purpleTicket);// finding the maximum times the purple ticket can go into the raisedMoney raised amount  
  51.     }
  52.    
  53.    
  54.    
  55.     if((brownTicket > 0) || (yellowTicket >0) || (grayTicket >0) || (purpleTicket >0)) //Error checking
  56.     {
  57.       r = raisedMoney;
  58.       do
  59.       {
  60.         r = raisedMoney; // setting raised money to be assigned to r
  61.         // resetting each ctr to 0 once the loop starts again
  62.         ctr1 = 0;
  63.         ctr2 = 0;
  64.         ctr3 = 0;
  65.         ctr4 = 0;
  66.         r= r - (brownTicket * brownMax); // subtracting the max brown tickets from the raisedMoney raised amount needed  
  67.         ctr1 = ctr1 * brownMax; // adding a counter to brown
  68.        
  69.         while (yellowTicket <= r) //keep adding 1 to the counter while the yellowTicket is less than or equal too the raised amount
  70.         {
  71.           ctr2++;
  72.           r = r - yellowTicket; // subtract the yellow ticket from the raised price if possible
  73.         }
  74.         while (grayTicket <= r)
  75.         {
  76.           ctr3++;
  77.           r = r - grayTicket;
  78.         }
  79.         while (purpleTicket <= r)
  80.         {
  81.           ctr4++;
  82.           r = r - purpleTicket;
  83.         }
  84.        
  85.        
  86.         if (ctr1 + ctr2 + ctr3 + ctr4 < min)
  87.         {
  88.           min = ctr1 + ctr2 + ctr3 + ctr4;
  89.         }
  90.         if (r == raisedMoney)
  91.         {
  92.           ctrCombo = ctrCombo + 1;
  93.          
  94.          
  95.           {
  96.             ctr++;
  97.             System.out.println ("# of PURPLE is " + ctr4 + " # of GRAY is " + ctr3 + " # of YELLOW is " + ctr2 + " # of PURPLE is " + ctr1);
  98.             yellowMax = yellowMax -1;
  99.           }
  100.         }
  101.       } while (yellowMax > 0);
  102.       r = raisedMoney;
  103.       do
  104.       {
  105.         r = raisedMoney;
  106.         ctr1 = 0;
  107.         ctr2 = 0;
  108.         ctr3 = 0;
  109.         ctr4 = 0;
  110.         r= r - (yellowTicket * yellowMax);
  111.         ctr2 = ctr2 * yellowMax;
  112.        
  113.         while (grayTicket <= r)
  114.         {
  115.           ctr3++;
  116.           r = r - grayTicket;
  117.         }
  118.         while (purpleTicket <= r)
  119.         {
  120.           ctr4++;
  121.           r = r - purpleTicket;
  122.         }
  123.         if (ctr1 + ctr2 + ctr3 + ctr4 < min)
  124.         {
  125.           min = ctr1 + ctr2 + ctr3 + ctr4;
  126.         }
  127.        
  128.         if (r == raisedMoney)
  129.         {
  130.           ctrCombo = ctrCombo + 1;
  131.          
  132.           {
  133.             ctr++;
  134.             System.out.println ("# of PURPLE is " + ctr4 + " # of GRAY is " + ctr3 + " # of YELLOW is " + ctr2 + " # of PURPLE is " + ctr1);
  135.             grayMax = grayMax -1;
  136.           }
  137.         }
  138.       } while (yellowMax > 0);
  139.       r = raisedMoney;
  140.       do
  141.       {
  142.         r = raisedMoney;
  143.         ctr1 = 0;
  144.         ctr2 = 0;
  145.         ctr3 = 0;
  146.         ctr4 = 0;
  147.         r= r - (grayTicket * grayMax);
  148.         ctr3 = ctr3 * grayMax;
  149.        
  150.         while (purpleTicket <= r)
  151.         {
  152.           ctr4++;
  153.           r = r - purpleTicket;
  154.         }
  155.        
  156.         if (ctr1 + ctr2 + ctr3 + ctr4 < min)
  157.         {
  158.           min = ctr1 + ctr2 + ctr3 + ctr4;
  159.         }
  160.         if (r == raisedMoney)
  161.         {
  162.           ctrCombo = ctrCombo + 1;
  163.          
  164.           {
  165.             ctr++;
  166.             System.out.println ("# of PURPLE is " + ctr4 + " # of GRAY is " + ctr3 + " # of YELLOW is " + ctr2 + " # of PURPLE is " + ctr1);
  167.             purpleMax = purpleMax -1;
  168.           }
  169.         }
  170.       } while (grayMax > 0);
  171.       r = raisedMoney;
  172.       do
  173.       {
  174.         r = raisedMoney;
  175.         ctr1 = 0;
  176.         ctr2 = 0;
  177.         ctr3 = 0;
  178.         ctr4 = 0;
  179.         r= r - (purpleTicket * purpleMax);
  180.         ctr2 = ctr2 * purpleMax;
  181.        
  182.         if (ctr1 + ctr2 + ctr3 + ctr4 < min)
  183.         {
  184.           min = ctr1 + ctr2 + ctr3 + ctr4;
  185.         }
  186.        
  187.         if (r == raisedMoney)
  188.         {
  189.           ctrCombo = ctrCombo + 1;
  190.          
  191.           {
  192.             ctr++;
  193.             System.out.println ("# of PURPLE is " + ctr4 + " # of GRAY is " + ctr3 + " # of YELLOW is " + ctr2 + " # of PURPLE is " + ctr1);
  194.             brownMax = brownMax -1;
  195.           }
  196.         }
  197.       } while (purpleMax == raisedMoney);
  198.      
  199.      
  200.     }
  201.     else
  202.     {
  203.       System.out.println("Invalid value(s)");
  204.     }
  205.     System.out.println ("there are " + ctrCombo + " combinations");
  206.   }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement