結果

問題 No.3 ビットすごろく
ユーザー marumaru
提出日時 2017-09-22 00:40:46
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,248 bytes
コンパイル時間 4,837 ms
コンパイル使用メモリ 78,368 KB
実行使用メモリ 58,284 KB
最終ジャッジ日時 2024-04-26 06:51:12
合計ジャッジ時間 9,574 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 117 ms
54,268 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 181 ms
57,308 KB
testcase_25 AC 176 ms
57,512 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
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 result = 1;
			List<Integer> resultList = new ArrayList<Integer>();
			resultList.add(result);
			int binCount = changeBinary(result);
			int dameCount = 0;
			while ((result + binCount) != n) {
				if ((result + binCount) > n) {// nを超えてしまう時
					dameCount++;
					if (dameCount < 2) {
						binCount = changeBinary(result);
						result -= binCount;
						resultList.add(result);
					}else {
						break;
					}
				} else if ((result + binCount) < n) {// nを超えない時
					binCount = changeBinary(result);
					result += binCount;
					resultList.add(result);
				}
			}

			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