問題文概略
1~6の目が出るサイコロを5回投げる。
出た目がポイントとなり、目が被ったらその目の数を足しあわせてポイントとする。
ポイントの最大値を求めよ。
例)
1,2,3,5,6 →ポイントは6
1,4,4,3,5 →ポイントは4+4=8
書いたコード
public class YahtzeeScore {
public int maxPoints(int[] toss) {
//どの目が何回出たかを入れる配列
int a[]=new int[6];
//ポイント
int max=0;
//どの目が何回出たかを配列に入れる
for(int i=0; i<toss.length; i++) {
a[toss[i]-1]++;
}
//上の配列から最大点数を計算
for (int k=0; k<6; k++){
if(max<a[k]*(k+1) == true) {
max = a[k]*(k+1);
}
}
return max;
}
}