U E D R , A S I H C RSS

An Easy Problem/강소현

Status

Problem 2453Usertalin0528
Memory3656KTime375MS
LanguageJavaResultAccepted

Idea

bit 연산� �숙치가 않아, 10진수 -> 2진수 변환해서 배열� 넣었습니다ㅠㅠ(�내고 다시 반대로..;�;..�런 비효율��!)
1�란 숫�를 찾았� 때 그 앞� 숫�가 0�면 1� 앞으로 보내고, 1�면 맨 �쪽으로 보내는...
ex)
0010 0000 -> 0100 0000
0011 1100 -> 0100 0111

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;
	}
}

Trial and error

  • Compile error
    • í�´ëž˜ìФ ì�´ë¦„ì�€ í•­ìƒ� "Main"으로 해주어야 합니다. 깜ì°�한 poj 같으니:)
  • Runtime error
    • printJ 함수 ë‚´ì—�서 while(numnum+1ê°€ index bound of exceptionì�´ 났었습니다.
  • Wrong answer
    • 2^20 = 1048576 으로 1000000보다 í�¬ë‹ˆê¹Œ ë°°ì—´ì�˜ í�¬ê¸°ë¥¼ 20으로 잡았었는ë�°, 1ê³¼ 0ì�„ 옮기면서 ì�´ 숫ìž�보다 ë�” 커지는 경우를 고려하지 못했습니다. ê·¸ ê²°ê³¼ 1~1000000 중ì—�서 4ê°œì�˜ 숫ìž�ê°€ 맨 앞ì�˜ 1ì�´ 짤려서 숫ìž�ê°€ 작게 출력ë�œ! ;ã……; 언뜻 보길래 ë§žì�€ 줄 알아서 뻘짓ì�„ ë§Žì�´ 한 ã…‹ã…‹ poj는 죄가 없어! ë‚´ê°€ 바보임 ã… ã… 

�연수 I 기대한 값 실제 값
524288 1048576 524288
786432 1048577 524289
917504 1048579 524291
983040 1048583 524295

  • 제가 처ì�Œì—� 하다하다 안ë�˜ì„œ 10진수를 2진수로 표기하려고 했다가 그게 매우 어렵다는걸 깨달았죠...; 배열로 하는 방법ë�„ 있었군요. -김태진

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:22:27
Processing time 0.1815 sec