DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Topcode Bonus Problem

10.15.2009
| 2236 views |
  • submit to reddit
        
public class Bonuses {
	public int[] getDivision(int[] points) {
		int totalPoints = 0;
		double extraPoints = 0.0;
		for(int point : points) {
			totalPoints += point;
		}
		int[] splitup = new int[points.length];
		for(int i=0; i<splitup.length; i++) {
			splitup[i] = points[i]*100/totalPoints;
			extraPoints += points[i]*100.0/totalPoints - splitup[i];					
		}
		int roundedExtraPoints = (int)(double)java.lang.Math.round(extraPoints);
		for(int i=0; i<roundedExtraPoints; i++) {
			int tmpIndex = returnIndexOfLargestNumber(points);
			splitup[tmpIndex] += 1;
			points[tmpIndex] = -1;			
		}
		return splitup;
	}
	private int returnIndexOfLargestNumber(int[] array) {
		int largestNumber = array[0];
		int largestNumberIndex = 0;
		for(int i=0; i<array.length; i++) {
			if(array[i]>largestNumber) {
				largestNumber = array[i];
				largestNumberIndex = i;
			}
		}
		return largestNumberIndex;
	}
}