結果

問題 No.3 ビットすごろく
ユーザー koukonko
提出日時 2015-12-24 22:43:18
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 957 bytes
コンパイル時間 3,806 ms
コンパイル使用メモリ 77,000 KB
実行使用メモリ 52,172 KB
最終ジャッジ日時 2024-09-18 23:01:37
合計ジャッジ時間 5,408 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 18 WA * 15
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
	public static void main(String[] args) {
		BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));

		try {
			int N = Integer.parseInt(buff.readLine());

			int ans = 1; // 移動数
			boolean[] check = new boolean[N];
			int i = 1;
			while (i < N) {
				String str = Integer.toBinaryString(i);
				int count = 0;
				for (int j = 0; j < str.length(); ++j) {
					if (str.charAt(j) == '1') {
						count++;
					}
				}
				if (count + i == N) {
					ans++;
					break;
				} else if (count + i < N) {
					ans++;
					i += count;
					check[i] = true;
				} else if(count + i > N){
					if(check[i - count] || i - count <= 0){
						ans = -1;
						break;
					} else {
						ans++;
						i -= count;
						check[i] = true;
					}
				}
			}
			System.out.println(ans);
		} catch (Exception e) {
			e.getStackTrace();
		}
	}
}
0