結果

問題 No.3 ビットすごろく
ユーザー maru
提出日時 2017-09-22 00:47:08
言語 Java
(openjdk 23)
結果
TLE  
実行時間 -
コード長 1,304 bytes
コンパイル時間 3,880 ms
コンパイル使用メモリ 77,876 KB
実行使用メモリ 46,380 KB
最終ジャッジ日時 2024-11-08 19:54:20
合計ジャッジ時間 10,595 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 2 TLE * 1 -- * 30
権限があれば一括ダウンロードができます

ソースコード

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 != 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);
				}
			}
			if(result != n) {
				System.out.println("-1");
			}else {
				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