結果
| 問題 | No.3 ビットすごろく |
| コンテスト | |
| ユーザー |
maru
|
| 提出日時 | 2017-09-22 11:17:15 |
| 言語 | Java (openjdk 25.0.2) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,375 bytes |
| 記録 | |
| コンパイル時間 | 2,595 ms |
| コンパイル使用メモリ | 83,772 KB |
| 実行使用メモリ | 52,812 KB |
| 最終ジャッジ日時 | 2026-05-08 16:54:39 |
| 合計ジャッジ時間 | 7,177 ms |
|
ジャッジサーバーID (参考情報) |
judge3_0 / judge2_1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 17 WA * 16 |
ソースコード
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class BitSugoroku {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
try {
int result = 1;
int first = 1;
List<Integer> resultList = new ArrayList<>();
resultList.add(first);
List<Integer> lastNumlist = new ArrayList<>();
int bitCount = changeBinary(first);
result += first;
while (result != n) {
bitCount = changeBinary(result);
if ((result + bitCount) > n) {// nを超えてしまってるとき
if (lastNumlist.contains(result)) {
break;
}
lastNumlist.add(result);
result -= bitCount;
resultList.add(result);
} else if ((result + bitCount) <= n) {// nを超えないとき
resultList.add(result);
result += bitCount;
}
}
if (result == n) {
resultList.add(result);
System.out.println(resultList.size());
} else if (result != n) {
System.out.println("-1");
}
} catch (Exception e) {
// TODO: handle exception
}
}
private static int changeBinary(int result) {
String bit = Integer.toBinaryString(result);
String[] bitArray = bit.split("");
// 2進数の1の数を数える
int bitCount = 0;
for (String sss : bitArray) {
if (sss.equals("1")) {
bitCount++;
}
}
return bitCount;
}
}
maru