U E D R , A S I H C RSS

AnEasyProblem/강소현 (rev. 1.1)

An Easy Problem/강소현

Status

Problem 2453Usertalin0528
Memory3656KTime375MS
LanguageJavaResultAccepted

Source Code

import java.util.Scanner;
public class Main{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		while(sc.hasNextInt()){
			int i = sc.nextInt();
			if(i == 0)
				break;
			printJ(i);
		}
	}
	private static void printJ(int i){
		int [] bin = binI(i);
		int num = 0, count=0;
		while(num<bin.length-1){
			if(bin[num] == 1){
				if(bin[num+1] == 0){
					bin[num+1] = 1;
					bin[num] = 0;
					break;
				}else{
					bin[num] = 0;
					bin[count++] = 1;
				}
			}
			num++;
		}
		int result = 0;
		for(int k=0; k<bin.length;k++){
			result += bin[k]*Math.pow(2,k);
		}
		System.out.println(result);
	}

	private static int [] binI(int i){
		int [] temp = new int [21];
		int num = 0;
		while(i>0){
			temp[num++] = i%2;
			i = i/2;
		}
		return temp;
	}
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:22:27
Processing time 0.0192 sec