結果

問題 No.3 ビットすごろく
ユーザー marumaru
提出日時 2017-09-21 23:55:37
言語 Java21
(openjdk 21)
結果
MLE  
実行時間 -
コード長 1,064 bytes
コンパイル時間 3,245 ms
コンパイル使用メモリ 75,464 KB
実行使用メモリ 523,576 KB
最終ジャッジ日時 2024-04-26 06:55:11
合計ジャッジ時間 110,657 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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 result = 1;
			List<Integer> resultList = new ArrayList<Integer>();
			resultList.add(result);
			int binCount = changeBinary(result);
			while (result + binCount != n) {
				if (result + binCount > n) {// nを超えてしまう時
					result -= binCount;
					resultList.add(result);
				} else if (result + binCount < n) {// nを超えない時
					result += binCount;
					resultList.add(result);
				}
			}

			System.out.println(resultList.size());
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	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 == "1") {
				binCount++;
			}
		}
		return binCount;
	}

}
0