結果

問題 No.3 ビットすごろく
ユーザー marumaru
提出日時 2017-09-22 11:17:15
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,375 bytes
コンパイル時間 3,835 ms
コンパイル使用メモリ 78,764 KB
実行使用メモリ 58,052 KB
最終ジャッジ日時 2024-04-25 22:45:41
合計ジャッジ時間 9,255 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 103 ms
53,000 KB
testcase_01 AC 109 ms
52,812 KB
testcase_02 AC 119 ms
53,900 KB
testcase_03 AC 161 ms
55,332 KB
testcase_04 AC 139 ms
54,444 KB
testcase_05 AC 169 ms
57,284 KB
testcase_06 AC 154 ms
55,288 KB
testcase_07 AC 155 ms
55,292 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 175 ms
57,408 KB
testcase_13 AC 154 ms
55,220 KB
testcase_14 AC 164 ms
57,304 KB
testcase_15 AC 180 ms
57,364 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 144 ms
55,236 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 104 ms
53,212 KB
testcase_22 AC 182 ms
58,052 KB
testcase_23 AC 190 ms
57,432 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 125 ms
54,264 KB
testcase_32 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

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;

	}

}
0