結果

問題 No.3 ビットすごろく
ユーザー marumaru
提出日時 2017-09-22 11:09:16
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,336 bytes
コンパイル時間 4,204 ms
コンパイル使用メモリ 78,452 KB
実行使用メモリ 184,380 KB
最終ジャッジ日時 2024-04-25 22:44:35
合計ジャッジ時間 10,770 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 130 ms
54,436 KB
testcase_01 AC 132 ms
54,308 KB
testcase_02 AC 135 ms
54,236 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
権限があれば一括ダウンロードができます

ソースコード

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);
			int lastResult = 0;
			int bitCount = changeBinary(first);
			result += first;
			while (result != n) {
				bitCount = changeBinary(result);
				if ((result + bitCount) > n) {// nを超えてしまってるとき
					if (lastResult == result) {
						break;
					}
					lastResult = 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