2010年12月14日火曜日

SRM 149 DIV2 | 217.20/250

数値を指定のフォーマーっとで返す問題

[java]
import java.text.*;

public class FormatAmt {
public String amount (int dollars, int cents) {
return new DecimalFormat("$#,##0.00").format((dollars + cents/100.0));
}
}
[/java]

2010年12月12日日曜日

SRM 148 DIV2 | 247.20/250

ある整数が各桁の数で割り切れるかどうかを判定し、割り切れる個数を返す問題。
[java]
public class DivisorDigits {
public int howMany(int number) {
int count = 0;
int copy = number;
while(copy > 0) {
if( copy % 10 != 0)
count += number % (copy % 10) == 0 ? 1 : 0;
copy /= 10;
}
return count;
}
}
[/java]

SRM 147 DIV2 | 200.35/250

シーザー暗号をデコードする問題

[java]
public class CCipher {
public String decode(String cipherText, int shift) {
char[] decipher = cipherText.toCharArray();
for(int i=0; i < decipher.length; i++)
decipher[i] = (char)((decipher[i] - shift - 'A') % 26 + 'A');
return new String(decipher);
}
}
[/java]

2010年12月11日土曜日

SRM 146 DIV2 | 226.71/250

Yahtzeeゲームの問題
[java]
public class YahtzeeScore {
public int maxPoints(int[] toss) {
int points[] = new int[6];
for(int i : toss) {
points[i-1] += i;
}
int max = 0;
for(int i : points)
max = Math.max(max, i);
return max;
}
}
[/java]

SRM 145 DIV2 | 218.25/250

文字の配列の中から、指定されたcharacterの個数を求める問題。

[java]
public class ImageDithering {
public int count(String dithered, String[] screen) {
String allScreen = "";
for(String str : screen)
allScreen += str;
int cnt = allScreen.length() - allScreen.replaceAll("["+dithered+"]","").length();
return cnt;
}
}
[/java]

TCHS SRM 1 | 161.60/250

数字が範囲内に歩かないか調べ、その平均や範囲内に無い率を調べる問題
[java]
public class SpeedRadar {
public double averageSpeed(int minLimit, int maxLimit, int[] readings) {
double sum = 0.0;
int infringment = 0;
for(int read : readings) {
if (minLimit > read || maxLimit < read)
infringment++;
else
sum += read;
}
double average = sum/(readings.length-infringment);
return 10*infringment>readings.length ? 0.0 : average;
}
}
[/java]

Inv 2001 R1 | 79.61/250

単語の平均文字数による場合分けをして、点数を返す問題。
[java]
public class HowEasy {
public int pointVal(String problemStatement) {
String words[] = problemStatement.split(" ");
int noWord = 0;
int length = 0;
for(String word : words)
if( word.matches("^[a-zA-Z]+.*$"))
length += word.endsWith(".") ? word.length()-1 : word.length();
else
noWord++;
if (words.length-noWord == 0 || length / (words.length-noWord) <=3)
return 250;
else if ( length/(words.length-noWord) <=5)
return 500;
else
return 1000;
}
}
[/java]

SRM 144 DIV 2 | 193.89/200

与えられた秒数を時間:分:秒に直す問題。
[java]
public class Time {
public String whatTime(int seconds) {
return seconds/3600+":"+(seconds%3600)/60+":"+(seconds%60);
}
}
[/java]