이진수 8진수 변환 by 백준 1373

less than 1 minute read

🔗 2진수 8진수 변환 백준 1373문제
package math;

import java.util.Scanner;

public class BinaryToHex {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        String num = sc.nextLine();

        if (num.length() % 3 == 1) {
            System.out.print(num.charAt(0));
        }
        else if (num.length() % 3 == 2) {

            System.out.print((num.charAt(0)-'0')* 2 + (num.charAt(1)-'0') );
        }

        for (int i = num.length()%3; i < num.length(); i+=3) {
            System.out.print((num.charAt(i)-'0')*4 + (num.charAt(i+1)-'0') * 2 + (num.charAt(i+2)-'0'));
        }
    }
}

💎결과

image-20220131122719716

Categories:

Updated:

Comments