• API
• FAQ
• Tools
• Trends
• Archive
SHARE
TWEET

# Java Ticket Combinations

a guest Nov 18th, 2012 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import java.io.* ;
2. public class MclaLoops//Class
3. {
4.
5.   public static void main (String[] args) throws IOException //main method
6.   {
8.
9.     System.out.println("Please enter the cost for a purple ticket"); //input prompt for cost of purple ticket
11.     System.out.println("Please enter the cost for a gray ticket"); //input prompt for cost of gray ticket
13.     System.out.println("Please enter the cost for a yellow ticket"); //inpur prompt for cost of 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.
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.
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.         if (r == raisedMoney)
86.         {
87.           ctrCombo = ctrCombo + 1;
88.
89.
90.           {
91.             ctr++;
92.             System.out.println ("# of PURPLE is " + ctr4 + " # of GRAY is " + ctr3 + "# of YELLOW is " + ctr2 + " # of PURPLE is " + ctr1);
93.             yellowMax = yellowMax -1;
94.           }
95.         }
96.       } while (yellowMax > 0);
97.       do
98.       {
99.         r = raisedMoney;
100.         ctr1 = 0;
101.         ctr2 = 0;
102.         ctr3 = 0;
103.         ctr4 = 0;
104.         r= r - (yellowTicket * yellowMax);
105.         ctr2 = ctr2 * yellowMax;
106.
107.         while (grayTicket <= r)
108.         {
109.           ctr3++;
110.           r = r - grayTicket;
111.         }
112.         while (purpleTicket <= r)
113.         {
114.           ctr4++;
115.           r = r - purpleTicket;
116.         }
117.
118.         if (r == raisedMoney)
119.         {
120.           ctrCombo = ctrCombo + 1;
121.
122.           {
123.             ctr++;
124.             System.out.println ("# of PURPLE is " + ctr4 + " # of GRAY is " + ctr3 + "# of YELLOW is " + ctr2 + " # of PURPLE is " + ctr1);
125.             grayMax = grayMax -1;
126.           }
127.         }
128.       } while (yellowMax > 0);
129.       do
130.       {
131.         r = raisedMoney;
132.         ctr1 = 0;
133.         ctr2 = 0;
134.         ctr3 = 0;
135.         ctr4 = 0;
136.         r= r - (grayTicket * grayMax);
137.         ctr3 = ctr3 * grayMax;
138.
139.         while (purpleTicket <= r)
140.         {
141.           ctr4++;
142.           r = r - purpleTicket;
143.         }
144.
145.         if (r == raisedMoney)
146.         {
147.           ctrCombo = ctrCombo + 1;
148.
149.           {
150.             ctr++;
151.             System.out.println ("# of PURPLE is " + ctr4 + " # of GRAY is " + ctr3 + "# of YELLOW is " + ctr2 + " # of PURPLE is " + ctr1);
152.             purpleMax = purpleMax -1;
153.           }
154.         }
155.       } while (grayMax > 0);
156.       do
157.       {
158.         r = raisedMoney;
159.         ctr1 = 0;
160.         ctr2 = 0;
161.         ctr3 = 0;
162.         ctr4 = 0;
163.         r= r - (purpleTicket * purpleMax);
164.         ctr2 = ctr2 * purpleMax;
165.
166.
167.         if (r == raisedMoney)
168.         {
169.           ctrCombo = ctrCombo + 1;
170.
171.           {
172.             ctr++;
173.             System.out.println ("# of PURPLE is " + ctr4 + " # of GRAY is " + ctr3 + "# of YELLOW is " + ctr2 + " # of PURPLE is " + ctr1);
174.             brownMax = brownMax -1;
175.           }
176.         }
177.       } while (purpleMax == raisedMoney);
178.
179.
180.     }
181.     else
182.     {
183.       System.out.println("Invalid value(s)");
184.     }
185.     System.out.println ("there are " + ctrCombo + " combinations");
186.   }
187. }
RAW Paste Data
Top