結果

問題 No.3 ビットすごろく
ユーザー FpmpAmpm
提出日時 2016-10-22 17:12:29
言語 Java
(openjdk 23)
結果
TLE  
実行時間 -
コード長 1,214 bytes
コンパイル時間 2,198 ms
コンパイル使用メモリ 74,400 KB
実行使用メモリ 110,908 KB
最終ジャッジ日時 2024-11-23 17:07:41
合計ジャッジ時間 43,039 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 13 WA * 14 TLE * 6
権限があれば一括ダウンロードができます

ソースコード

diff #

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner sc = new Scanner(System.in);
    	int N = sc.nextInt();
    	int m = 1;
    	int distance = 0;
    	int target;
    	int muripo = 0;
    	int count = 1;
    	while (m != N) {
    		count++;
    		distance = 0;
    		//System.out.println("----count:" + count);
    		char[] c = Integer.toBinaryString(m).toCharArray();
    		for (int i = 0;i < c.length;i++) {
    			if (c[i] == '1') {
    				distance++;
    			}
    		}
    		//System.out.println("distance:" + distance);
    		//get target
    		if (N < m + distance) {
    			target = m - distance;
    			// 以前同じmで戻っている
    			if (m == muripo) {
    				System.out.println(-1);
    				return;
    			}
    			muripo = m;
    		} else {
    			target = m + distance;
    		}
    		//System.out.println("target:" + target);
    		//move
    		m = target;
    		//System.out.println("m:" + m);
    	}
    	System.out.println(count);
	}
}
0