久しぶりに
全然ブログ更新していなかったのですが、そろそろ放置歴7ヶ月を過ぎそうなのでちょっと更新します。
大学に入ってからはいろいろあり過ぎて書ききれないかつ書くのめんどいので、今日は普通にマイブームについて書きます。
今、私はComputer Science を学んでいるのですが、オンライン上でプログラミングの練習ができるTopCoderというサイトを使っています。今年はHackathonに出てみたいです。
ということで、今日の問題:SRM145 DIV1 250 points
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 |
|||||||||||||
|
|||||||||||||
Limits |
|||||||||||||
|
|||||||||||||
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.
めんどいのでさすがに訳さないけど、こんな感じです。
今のところJavaが私にはやりやすいので、Javaで書きました。
public class Bonuses
{
public int getDivision(int points)
{
double total = 0.0;
int extra = 100, n = points.length;
int[] bonuses = new int[n];
for (int i = 0; i < n; i++) {
total += points[i];
}
for (int j = 0; j < n; j++) {
bonuses[j] = (int) (points[j] / total * 100);
extra -= bonuses[j] ;
}
if (extra != 0) {
for (int i = 0; i < extra; i++) {
int max = 0, index = 0;
for (int j = 0; j < n; j++) {
if (points[j] > max) {
max = points[j];
index = j;
}
}
points[index] = 0;
bonuses[index]++;
}
}
return bonuses;
}
}
コードのきれいなコピペの仕方知ってる方いらっしゃれば教えてください。points[index] = 0; にすればいいんだってことに気づくのに時間かけすぎたのと、洗濯したり友達と話したりして途中1時間くらい放置してたので、75.83しかもらえなかったけど、とりあえず通ったのでいっかって感じですねー
まだ始めたばかりですが、なるべく毎日練習しようと思います。