結果

問題 No.3 ビットすごろく
ユーザー ShotaroSuzu
提出日時 2020-05-20 14:16:56
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 1,181 bytes
コンパイル時間 3,864 ms
コンパイル使用メモリ 79,164 KB
実行使用メモリ 56,088 KB
最終ジャッジ日時 2024-10-01 23:32:48
合計ジャッジ時間 9,939 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 18 WA * 15
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder.level2.bitsugoroku;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class BitSugoroku {

	public static void main(String[] args) {
		new BitSugoroku().execute();
	}

	private void execute() {
		int goalNum = read();
		int count = 1;
		int currentNum = 1;
		List<Integer> stoppedNum = new ArrayList<>();
		
		while(currentNum != goalNum) {
			int  numOfOneByBynary = getNumOfOneByBynary(currentNum);
			int tempDest = currentNum + numOfOneByBynary;
			
			if(stoppedNum.contains(tempDest)) {
				count = -1;
				break;
			}
			
			if(tempDest > goalNum) {
				tempDest = currentNum - numOfOneByBynary;
			}
			currentNum = tempDest;
			stoppedNum.add(currentNum);
			count++;
		}
		
		System.out.println(count);
		
	}

	private int getNumOfOneByBynary(int num) {
		char[] numOfBynary = Integer.toBinaryString(num).toCharArray();
		Arrays.sort(numOfBynary);
		String numOfBynaryString = new String(numOfBynary);
		return numOfBynary.length - (numOfBynaryString.indexOf("1"));
	}

	private int read() {
		@SuppressWarnings("resource")
		Scanner sc = new Scanner(System.in);
		return sc.nextInt();
	}

}
0