Skip to content

Commit a8e60b7

Browse files
author
Ahmad Medhat Othman
committed
SRM 504.5 round one
1 parent fc4943a commit a8e60b7

12 files changed

+728
-0
lines changed

Diff for: TheJackpotDivTwo.class

3.42 KB
Binary file not shown.

Diff for: TheJackpotDivTwo.html

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<html><body bgcolor="#ffffff" text="#000000"><table><tr><td colspan="2"><h3>Problem Statement</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td>John has recently won a jackpot, but he doesn't need the money. He decided to share it with his friends instead. He knows how much money each of his friends has, and he will use this information to perform the distribution. While he still has money left, he will repeat the following steps:
2+
<ul>
3+
<li>Choose the poorest friend. If there are multiple poorest friends, choose one of them randomly.</li>
4+
<li>Give 1 dollar to the chosen friend.</li>
5+
</ul>
6+
You are given an int <b>jackpot</b>, the number of dollars John has won, and a int[] <b>money</b>, where the i-th element is the number of dollars currently owned by the i-th friend. Return a int[] containing the same number of elements as money. The return value must contain the number of dollars owned by each friend after John has performed the above distribution, sorted in non-decreasing order.</td></tr><tr><td colspan="2"><h3>Definition</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td>Class:</td><td>TheJackpotDivTwo</td></tr><tr><td>Method:</td><td>find</td></tr><tr><td>Parameters:</td><td>int[], int</td></tr><tr><td>Returns:</td><td>int[]</td></tr><tr><td>Method signature:</td><td>int[] find(int[] money, int jackpot)</td></tr><tr><td colspan="2">(be sure your method is public)</td></tr></table></td></tr><tr><td>&#160;&#160;&#160;&#160;</td></tr><tr><td></td></tr><tr><td colspan="2"><h3>Constraints</h3></td></tr><tr><td align="center" valign="top">-</td><td><b>money</b> will contain between 1 and 47 elements, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>Each element of <b>money</b> will be between 1 and 1,000,000, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td><b>jackpot</b> will be between 1 and 1,000,000, inclusive.</td></tr><tr><td colspan="2"><h3>Examples</h3></td></tr><tr><td align="center" nowrap="true">0)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{1, 2, 3, 4}</pre></td></tr><tr><td><pre>2</pre></td></tr></table></td></tr><tr><td><pre>Returns: {2, 3, 3, 4 }</pre></td></tr><tr><td><table><tr><td colspan="2">First, John will give one dollar to the first friend. Then he will give another dollar to the first or the second friend.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">1)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{4, 7}</pre></td></tr><tr><td><pre>1</pre></td></tr></table></td></tr><tr><td><pre>Returns: {5, 7 }</pre></td></tr><tr><td><table><tr><td colspan="2">Just&nbsp;one&nbsp;action&nbsp;here.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">2)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{1}</pre></td></tr><tr><td><pre>100</pre></td></tr></table></td></tr><tr><td><pre>Returns: {101 }</pre></td></tr><tr><td><table><tr><td colspan="2">Just one friend here.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">3)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{21, 85, 6, 54, 70, 100, 91, 60, 71}</pre></td></tr><tr><td><pre>15</pre></td></tr></table></td></tr><tr><td><pre>Returns: {21, 21, 54, 60, 70, 71, 85, 91, 100 }</pre></td></tr><tr><td></td></tr></table></td></tr></table><p>This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved. </p></body></html>

Diff for: TheJackpotDivTwo.java

+189
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
import java.util.*;
2+
import java.util.regex.*;
3+
import java.text.*;
4+
import java.math.*;
5+
import java.awt.geom.*;
6+
7+
public class TheJackpotDivTwo
8+
{
9+
public int[] find(int[] money, int jackpot)
10+
{
11+
int x = jackpot;
12+
int i=0;
13+
while (x > 0) {
14+
int minn = 0;
15+
for (i = 1 ; i < money.length ; i++){
16+
if (money[i] < money[minn]) {
17+
minn = i;
18+
}
19+
}
20+
money[minn] = money[minn] + 1;
21+
x--;
22+
}
23+
Arrays.sort(money);
24+
return money;
25+
}
26+
27+
public static void main(String[] args)
28+
{
29+
long time;
30+
int[] answer;
31+
boolean errors = false;
32+
int[] desiredAnswer;
33+
34+
boolean same;
35+
36+
time = System.currentTimeMillis();
37+
answer = new TheJackpotDivTwo().find(new int[]{1, 2, 3, 4}, 2);
38+
System.out.println("Time: " + (System.currentTimeMillis()-time)/1000.0 + " seconds");
39+
desiredAnswer = new int[]{2, 3, 3, 4 };
40+
System.out.println("Your answer:");
41+
if (answer.length > 0)
42+
{
43+
System.out.print("\t{ " + answer[0]);
44+
for (int i=1; i<answer.length; i++)
45+
System.out.print(", " + answer[i]);
46+
System.out.println(" }");
47+
}
48+
else
49+
System.out.println("\t{ }");
50+
System.out.println("Desired answer:");
51+
if (desiredAnswer.length > 0)
52+
{
53+
System.out.print("\t{ " + desiredAnswer[0]);
54+
for (int i=1; i<desiredAnswer.length; i++)
55+
System.out.print(", " + desiredAnswer[i]);
56+
System.out.println(" }");
57+
}
58+
else
59+
System.out.println("\t{ }");
60+
same = desiredAnswer.length == answer.length;
61+
for (int i=0; i<answer.length && same; i++)
62+
if (answer[i] != desiredAnswer[i])
63+
same = false;
64+
if (!same)
65+
{
66+
errors = true;
67+
System.out.println("DOESN'T MATCH!!!!");
68+
}
69+
else
70+
System.out.println("Match :-)");
71+
System.out.println();
72+
time = System.currentTimeMillis();
73+
answer = new TheJackpotDivTwo().find(new int[]{4, 7}, 1);
74+
System.out.println("Time: " + (System.currentTimeMillis()-time)/1000.0 + " seconds");
75+
desiredAnswer = new int[]{5, 7 };
76+
System.out.println("Your answer:");
77+
if (answer.length > 0)
78+
{
79+
System.out.print("\t{ " + answer[0]);
80+
for (int i=1; i<answer.length; i++)
81+
System.out.print(", " + answer[i]);
82+
System.out.println(" }");
83+
}
84+
else
85+
System.out.println("\t{ }");
86+
System.out.println("Desired answer:");
87+
if (desiredAnswer.length > 0)
88+
{
89+
System.out.print("\t{ " + desiredAnswer[0]);
90+
for (int i=1; i<desiredAnswer.length; i++)
91+
System.out.print(", " + desiredAnswer[i]);
92+
System.out.println(" }");
93+
}
94+
else
95+
System.out.println("\t{ }");
96+
same = desiredAnswer.length == answer.length;
97+
for (int i=0; i<answer.length && same; i++)
98+
if (answer[i] != desiredAnswer[i])
99+
same = false;
100+
if (!same)
101+
{
102+
errors = true;
103+
System.out.println("DOESN'T MATCH!!!!");
104+
}
105+
else
106+
System.out.println("Match :-)");
107+
System.out.println();
108+
time = System.currentTimeMillis();
109+
answer = new TheJackpotDivTwo().find(new int[]{1}, 100);
110+
System.out.println("Time: " + (System.currentTimeMillis()-time)/1000.0 + " seconds");
111+
desiredAnswer = new int[]{101 };
112+
System.out.println("Your answer:");
113+
if (answer.length > 0)
114+
{
115+
System.out.print("\t{ " + answer[0]);
116+
for (int i=1; i<answer.length; i++)
117+
System.out.print(", " + answer[i]);
118+
System.out.println(" }");
119+
}
120+
else
121+
System.out.println("\t{ }");
122+
System.out.println("Desired answer:");
123+
if (desiredAnswer.length > 0)
124+
{
125+
System.out.print("\t{ " + desiredAnswer[0]);
126+
for (int i=1; i<desiredAnswer.length; i++)
127+
System.out.print(", " + desiredAnswer[i]);
128+
System.out.println(" }");
129+
}
130+
else
131+
System.out.println("\t{ }");
132+
same = desiredAnswer.length == answer.length;
133+
for (int i=0; i<answer.length && same; i++)
134+
if (answer[i] != desiredAnswer[i])
135+
same = false;
136+
if (!same)
137+
{
138+
errors = true;
139+
System.out.println("DOESN'T MATCH!!!!");
140+
}
141+
else
142+
System.out.println("Match :-)");
143+
System.out.println();
144+
time = System.currentTimeMillis();
145+
answer = new TheJackpotDivTwo().find(new int[]{21, 85, 6, 54, 70, 100, 91, 60, 71}, 15);
146+
System.out.println("Time: " + (System.currentTimeMillis()-time)/1000.0 + " seconds");
147+
desiredAnswer = new int[]{21, 21, 54, 60, 70, 71, 85, 91, 100 };
148+
System.out.println("Your answer:");
149+
if (answer.length > 0)
150+
{
151+
System.out.print("\t{ " + answer[0]);
152+
for (int i=1; i<answer.length; i++)
153+
System.out.print(", " + answer[i]);
154+
System.out.println(" }");
155+
}
156+
else
157+
System.out.println("\t{ }");
158+
System.out.println("Desired answer:");
159+
if (desiredAnswer.length > 0)
160+
{
161+
System.out.print("\t{ " + desiredAnswer[0]);
162+
for (int i=1; i<desiredAnswer.length; i++)
163+
System.out.print(", " + desiredAnswer[i]);
164+
System.out.println(" }");
165+
}
166+
else
167+
System.out.println("\t{ }");
168+
same = desiredAnswer.length == answer.length;
169+
for (int i=0; i<answer.length && same; i++)
170+
if (answer[i] != desiredAnswer[i])
171+
same = false;
172+
if (!same)
173+
{
174+
errors = true;
175+
System.out.println("DOESN'T MATCH!!!!");
176+
}
177+
else
178+
System.out.println("Match :-)");
179+
System.out.println();
180+
181+
182+
if (errors)
183+
System.out.println("Some of the test cases had errors :-(");
184+
else
185+
System.out.println("You're a stud (at least on the test data)! :-D ");
186+
}
187+
188+
}
189+
//Powered by [KawigiEdit] 2.0!

Diff for: TheNumbersWithLuckyLastDigit.class

1.59 KB
Binary file not shown.

Diff for: TheNumbersWithLuckyLastDigit.html

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<html><body bgcolor="#ffffff" text="#000000"><table><tr><td colspan="2"><h3>Problem Statement</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><p>
2+
John believes that the digits 4 and 7 are lucky, and all other digits are unlucky.
3+
A positive integer is called a lucky number if its last digit is lucky.
4+
For example, 4, 14 and 207 are lucky numbers, while 40, 741 and 3 are not lucky numbers.
5+
John would like to represent the int <b>n</b> as a sum of only lucky numbers, and he would like to do this using the minimal possible number of summands.
6+
Return the number of summands in the representation, or -1 if it is impossible to achieve the goal.
7+
</p>
8+
</td></tr><tr><td colspan="2"><h3>Definition</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td>Class:</td><td>TheNumbersWithLuckyLastDigit</td></tr><tr><td>Method:</td><td>find</td></tr><tr><td>Parameters:</td><td>int</td></tr><tr><td>Returns:</td><td>int</td></tr><tr><td>Method signature:</td><td>int find(int n)</td></tr><tr><td colspan="2">(be sure your method is public)</td></tr></table></td></tr><tr><td>&#160;&#160;&#160;&#160;</td></tr><tr><td></td></tr><tr><td colspan="2"><h3>Constraints</h3></td></tr><tr><td align="center" valign="top">-</td><td><b>n</b> will be between 1 and 1,000,000,000, inclusive.</td></tr><tr><td colspan="2"><h3>Examples</h3></td></tr><tr><td align="center" nowrap="true">0)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>99</pre></td></tr></table></td></tr><tr><td><pre>Returns: 4</pre></td></tr><tr><td><table><tr><td colspan="2">One&nbsp;of&nbsp;the&nbsp;possible&nbsp;representations&nbsp;is&nbsp;99=14+24+27+34.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">1)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>11</pre></td></tr></table></td></tr><tr><td><pre>Returns: 2</pre></td></tr><tr><td><table><tr><td colspan="2">11=4+7.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">2)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>13</pre></td></tr></table></td></tr><tr><td><pre>Returns: -1</pre></td></tr><tr><td><table><tr><td colspan="2">It is impossible to achieve the goal.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">3)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>1234567</pre></td></tr></table></td></tr><tr><td><pre>Returns: 1</pre></td></tr><tr><td></td></tr></table></td></tr></table><p>This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved. </p></body></html>

Diff for: TheNumbersWithLuckyLastDigit.java

+161
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
import java.util.*;
2+
import java.util.regex.*;
3+
import java.text.*;
4+
import java.math.*;
5+
import java.awt.geom.*;
6+
7+
public class TheNumbersWithLuckyLastDigit
8+
{
9+
public int find(int n)
10+
{
11+
return solve(n,0);
12+
}
13+
14+
int solve(int n,int flag) {
15+
int x1 = n/2;
16+
int x2 = n-x1;
17+
int ans = -1;
18+
int x = n;
19+
if (flag == 1) {
20+
if (isl(n)) {
21+
//System.out.println(" ++++++++++++++++++++++" + n);
22+
return 1;
23+
}
24+
}
25+
//System.out.println(" ---- " + n + " ----- ");
26+
//System.out.println("" + x1 + " --- " + x2 + "");
27+
if (solve2(x1,x2)) {
28+
return 2;
29+
} else {
30+
while (x > 0) {
31+
if (isl(x)) {
32+
ans = 1 + solve(n-x,1);
33+
if (ans < 1) {
34+
x--;
35+
} else {
36+
return ans;
37+
}
38+
} else {
39+
x--;
40+
}
41+
}
42+
return -1;
43+
}
44+
}
45+
46+
boolean solve2 (int x1, int x2) {
47+
48+
if ( x1 <= 0 || x2 <=0 ) return false;
49+
50+
System.out.println(")))) " + x1 + " --- " + x2 + "");
51+
52+
if (isl(x1)) {
53+
if (isl(x2)) {
54+
return true;
55+
} else {
56+
System.out.println(">>>>> " + (x1-1) + " --- " + (x2+1) + "");
57+
return solve2(x1-1,x2+1);
58+
}
59+
} else {
60+
System.out.println("xxxx " + (x1-1) + " --- " + (x2+1) + "");
61+
return solve2(x1-1,x2+1);
62+
}
63+
}
64+
65+
boolean isl(int n) {
66+
67+
Integer nn = n;
68+
String ns = nn.toString();
69+
//int res = n % (Math.pow( 10*Integer.parseInt(ns.charAt(0)), ns.length()));
70+
if (ns.charAt(ns.length()-1) == '4' || ns.charAt(ns.length()-1) == '7') {
71+
return true;
72+
} else {
73+
return false;
74+
}
75+
}
76+
77+
public static void main(String[] args)
78+
{
79+
int answer = new TheNumbersWithLuckyLastDigit().find(51);
80+
System.out.println(answer);
81+
82+
/*long time;
83+
int answer;
84+
boolean errors = false;
85+
int desiredAnswer;
86+
87+
88+
time = System.currentTimeMillis();
89+
answer = new TheNumbersWithLuckyLastDigit().find(99);
90+
System.out.println("Time: " + (System.currentTimeMillis()-time)/1000.0 + " seconds");
91+
desiredAnswer = 4;
92+
System.out.println("Your answer:");
93+
System.out.println("\t" + answer);
94+
System.out.println("Desired answer:");
95+
System.out.println("\t" + desiredAnswer);
96+
if (answer != desiredAnswer)
97+
{
98+
errors = true;
99+
System.out.println("DOESN'T MATCH!!!!");
100+
}
101+
else
102+
System.out.println("Match :-)");
103+
System.out.println();
104+
time = System.currentTimeMillis();
105+
answer = new TheNumbersWithLuckyLastDigit().find(11);
106+
System.out.println("Time: " + (System.currentTimeMillis()-time)/1000.0 + " seconds");
107+
desiredAnswer = 2;
108+
System.out.println("Your answer:");
109+
System.out.println("\t" + answer);
110+
System.out.println("Desired answer:");
111+
System.out.println("\t" + desiredAnswer);
112+
if (answer != desiredAnswer)
113+
{
114+
errors = true;
115+
System.out.println("DOESN'T MATCH!!!!");
116+
}
117+
else
118+
System.out.println("Match :-)");
119+
System.out.println();
120+
time = System.currentTimeMillis();
121+
answer = new TheNumbersWithLuckyLastDigit().find(13);
122+
System.out.println("Time: " + (System.currentTimeMillis()-time)/1000.0 + " seconds");
123+
desiredAnswer = -1;
124+
System.out.println("Your answer:");
125+
System.out.println("\t" + answer);
126+
System.out.println("Desired answer:");
127+
System.out.println("\t" + desiredAnswer);
128+
if (answer != desiredAnswer)
129+
{
130+
errors = true;
131+
System.out.println("DOESN'T MATCH!!!!");
132+
}
133+
else
134+
System.out.println("Match :-)");
135+
System.out.println();
136+
time = System.currentTimeMillis();
137+
answer = new TheNumbersWithLuckyLastDigit().find(1234567);
138+
System.out.println("Time: " + (System.currentTimeMillis()-time)/1000.0 + " seconds");
139+
desiredAnswer = 1;
140+
System.out.println("Your answer:");
141+
System.out.println("\t" + answer);
142+
System.out.println("Desired answer:");
143+
System.out.println("\t" + desiredAnswer);
144+
if (answer != desiredAnswer)
145+
{
146+
errors = true;
147+
System.out.println("DOESN'T MATCH!!!!");
148+
}
149+
else
150+
System.out.println("Match :-)");
151+
System.out.println();
152+
153+
154+
if (errors)
155+
System.out.println("Some of the test cases had errors :-(");
156+
else
157+
System.out.println("You're a stud (at least on the test data)! :-D ");*/
158+
}
159+
160+
}
161+
//Powered by [KawigiEdit] 2.0!

0 commit comments

Comments
 (0)