Problem Statement | |||||||||||||
You have a certain amount of money to give out as a bonus to employees. The trouble is, who do you pick to receive what bonus? You decide to assign a number of points to each employee, which corresponds to how much they helped the company in the last year. You are given an int[] points, where each element contains the points earned by the corresponding employee (i.e. points[0] is the number of points awarded to employee 0). Using this, you are to calculate the bonuses as follows: - First, add up all the points, this is the pool of total points awarded. The return value should be a int[], one element per employee in the order they were passed in. Each element should be the percent of the bonus that the employee gets. | |||||||||||||
Definition | |||||||||||||
| |||||||||||||
Constraints | |||||||||||||
- | points will have between 1 and 50 elements, inclusive. | ||||||||||||
- | Each element of points will be between 1 and 500, inclusive. | ||||||||||||
Examples | |||||||||||||
0) | |||||||||||||
| |||||||||||||
1) | |||||||||||||
| |||||||||||||
2) | |||||||||||||
|
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.
这次好象也用了1小时
package srms1_20_r3;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
public class Bonuses {
/**
* @param args
*/
public static void main(String[] args) {
int s[] = { 485, 324, 263, 143, 470, 292, 304, 188, 100, 254, 296, 255,
360, 231, 311, 275, 93, 463, 115, 366, 197, 470 };
int r[] = getDivision(s);
for (int i = 0; i < r.length; i++)
System.out.print(r[i] + ",");
}
public static int[] getDivision(int[] points) {
TreeMap<String, Integer> map = new TreeMap<String, Integer>(
new Comparator<String>() {
public int compare(String s1, String s2) {
String array1[] = s1.split("-");
String array2[] = s2.split("-");
int v1, v2, v3, v4;
v1 = Integer.parseInt(array1[0]);
v2 = Integer.parseInt(array1[1]);
v3 = Integer.parseInt(array2[0]);
v4 = Integer.parseInt(array2[1]);
if (v1 > v3)
return -1;
else if (v1 < v3)
return 1;
else {
if (v2 > v4)
return 1;
else
return -1;
}
}
});
int sum = 0;
int len = points.length;
int num = 100;
int r[] = new int[len];
for (int i = 0; i < len; i++)
sum += points[i];
for (int i = 0; i < len; i++) {
r[i] = points[i] * 100 / sum;
num -= r[i];
map.put(points[i] + "-" + i, i);
}
if (num > 0) {
int index = -1;
Iterator titer = map.entrySet().iterator();
while (titer.hasNext()) {
if (num < 1) {
break;
}
Map.Entry ent = (Map.Entry) titer.next();
String keyt = ent.getKey().toString();
String valuet = ent.getValue().toString();
index = Integer.parseInt(valuet);
r[index]++;
num--;
}
}
return r;
}
}