結果

問題 No.3 ビットすごろく
ユーザー marumaru
提出日時 2017-09-22 23:20:59
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,224 bytes
コンパイル時間 3,783 ms
コンパイル使用メモリ 78,072 KB
実行使用メモリ 46,132 KB
最終ジャッジ日時 2024-04-26 02:18:55
合計ジャッジ時間 10,340 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
41,316 KB
testcase_01 AC 133 ms
41,288 KB
testcase_02 AC 128 ms
41,320 KB
testcase_03 AC 168 ms
43,452 KB
testcase_04 AC 153 ms
41,524 KB
testcase_05 AC 181 ms
43,880 KB
testcase_06 AC 168 ms
43,088 KB
testcase_07 AC 156 ms
43,380 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 178 ms
44,040 KB
testcase_13 AC 155 ms
43,000 KB
testcase_14 AC 191 ms
44,916 KB
testcase_15 AC 183 ms
45,076 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 152 ms
42,596 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 105 ms
39,824 KB
testcase_22 AC 170 ms
44,424 KB
testcase_23 AC 194 ms
44,952 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 115 ms
40,280 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 129 ms
41,860 KB
testcase_32 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

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);
		String[] binArray = bin.split("");

		// 2進数の1の数を数える
		int binCount = 0;
		for (String sss : binArray) {
			if (sss.equals("1")) {
				binCount++;
			}
		}
		return binCount;
	}

}
0