結果
| 問題 |
No.3 ビットすごろく
|
| コンテスト | |
| ユーザー |
maru
|
| 提出日時 | 2017-09-23 12:18:52 |
| 言語 | Java (openjdk 23) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,189 bytes |
| コンパイル時間 | 3,729 ms |
| コンパイル使用メモリ | 78,004 KB |
| 実行使用メモリ | 56,156 KB |
| 最終ジャッジ日時 | 2024-11-14 04:08:47 |
| 合計ジャッジ時間 | 9,228 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 18 WA * 15 |
ソースコード
package bitsgoroku;
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 now = 1;
List<Integer> resultList = new ArrayList<Integer>();
List<Integer> backList = new ArrayList<>();
while (now != n) {
int bitCount = changeBinary(now);
if ((now + bitCount) > n) {// nを超えてしまう時
resultList.add(now);
if (backList.contains(now)) {
break;
}
backList.add(now);
now -= bitCount;
} else if ((now + bitCount) <= n) {// nを超えない時
resultList.add(now);
now += bitCount;
}
}
if (now != n) {
System.out.println("-1");
} else {
resultList.add(now);
System.out.println(resultList.size());
}
} catch (Exception e) {
System.out.println(e);
}
}
private static int changeBinary(int result) {
String bin = Integer.toBinaryString(result);
// 2進数の1の数を数える
int binCount = 0;
for (char sss : bin.toCharArray()) {
if (sss == '1') {
binCount++;
}
}
return binCount;
}
}
maru