ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 백준 1373,1212,2089 - 진법변환문제
    자료구조와 알고리즘/문제풀기 2023. 4. 10. 23:19

     

    1373

    2진법 - 8진법 

    2진법을 3자리씩 끊어서 읽으면 8진법 

    public class Main{
    
        public static void main(String[] args) throws Exception {
           BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
           BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
           Stack<Integer> s = new Stack();
           String input = br.readLine();
           
            //숫자가 너무 커서 자료형에 저장 불가 -->문자로 표현해야함
            int size = input.length()%3 == 0 ? input.length()/3 : input.length()/3+1;
            char[] oresult= new char[size];//char arr크기 2진수 3당 8진수 1개 표현
           
            int result = 0; //계산결과 담기
            StringBuilder sb = new StringBuilder();//출력용
            
            //바이너리 값 스택에 저장
            for(char a : input.toCharArray()){
              s.push(Integer.parseInt(a+""));
            }
    
            //8진수로 고치기
            for(int i=0; i<input.length();i++){
                if(i % 3 ==0 )result =0;
                int temp = ((int)Math.pow(2,i%3))*s.pop();
                result += temp;
                oresult[i/3] = (char)(result+'0');
            }
    
            sb.append(oresult);
            sb.reverse();//결과 뒤집기
    
    
           bw.write(sb.toString());
           bw.flush();
           bw.close();
        }
    
    
    }

    1212

    8진법 -> 2진법

    public class Main {
    
        public static void main(String[] args) throws Exception {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
            StringBuilder sb = new StringBuilder();
    
            String input = br.readLine();
            int[] oinput = new int[input.length()];
    
            //입력변환
            for(int i=0; i<input.length();i++){
                oinput[i] = Integer.valueOf(input.charAt(i))-'0';
            }
            //변환
            int n=0;
            for(int i=0; i<oinput.length;i++){
                n = oinput[i];
                sb.append(getBinery(n));
            }
            if(!sb.toString().equals("000")) {
                while (sb.charAt(0) == '0') {
                    sb.deleteCharAt(0);
                }
            }
            else {
                System.out.println("0");
                return;
            }
            System.out.println(sb);
    
        }
    
        private static String getBinery(int n) {
            StringBuilder sb = new StringBuilder();
            Stack<Character> s = new Stack();
            if(2>n)sb.append("00");
            else if (4>n) sb.append("0");
            while(n != 0 && n!= 1){
                s.push((char)(n%2+'0'));
                n /=2;
            }
            if(n ==1) s.push('1');
            else s.push('0');
            while(!s.empty())
            sb.insert(sb.length(),s.pop());
            return sb.toString();
        }
    
    }

    2089

    -2진법 

    public class Main{
        
        public static void main(String[] args){
            Scanner sc = new Scanner(System.in);
            int input = sc.nextInt();
            StringBuilder sb = new StringBuilder();
            if(input == 0) {System.out.println("0");}
            else{
                while(input != 0){
                    sb.append(Math.abs(input%-2));
                    input = (int)Math.ceil((double)input/-2);
                }
               
                sb.reverse();
                System.out.println(sb);
        }
        
    }
    }

     

Designed by Tistory.