AtCoder Beginner Contest 014について(Java)
ABC014-Bのテストコードが足りない
テストケースにおいて何が不足しているのかが知りたいです。
実ソース
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int X = scan.nextInt();
StringBuilder Xreversed = new StringBuilder(Integer.toBinaryString(X)).reverse();
int sum = 0;
int bitJudge = Xreversed.length() % 4;
if(bitJudge == 0){
for(int i = 0; i < n; i++){
if(Xreversed.toString().substring(i, i+1).equals("1")){
sum += scan.nextInt();
}else{
scan.nextInt();
}
}
System.out.println(sum);
} else {
// 4ビット区切り
for(int i = 0; i < 4 - bitJudge; i++){
Xreversed = Xreversed.append("0");
}
// 出力
for(int i = 0; i < n; i++){
if(Xreversed.toString().substring(i, i+1).equals("1")){
sum += scan.nextInt();
}else{
scan.nextInt();
}
}
System.out.println(sum);
}
}
}
0 likes