結果

問題 No.3 ビットすごろく
ユーザー ShotaroSuzuShotaroSuzu
提出日時 2020-05-20 14:16:56
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,181 bytes
コンパイル時間 3,744 ms
コンパイル使用メモリ 79,548 KB
実行使用メモリ 54,548 KB
最終ジャッジ日時 2024-04-09 22:56:12
合計ジャッジ時間 9,929 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 136 ms
54,336 KB
testcase_01 AC 130 ms
53,944 KB
testcase_02 AC 132 ms
54,220 KB
testcase_03 AC 154 ms
54,252 KB
testcase_04 AC 138 ms
54,188 KB
testcase_05 AC 165 ms
54,124 KB
testcase_06 AC 156 ms
54,264 KB
testcase_07 AC 147 ms
54,308 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 149 ms
54,208 KB
testcase_13 AC 149 ms
54,164 KB
testcase_14 AC 164 ms
54,004 KB
testcase_15 AC 158 ms
54,088 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 133 ms
53,256 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 130 ms
53,996 KB
testcase_22 AC 157 ms
54,548 KB
testcase_23 AC 163 ms
54,328 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 130 ms
54,140 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 130 ms
54,396 KB
testcase_32 WA -
権限があれば一括ダウンロードができます

ソースコード

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