[CodeUp] #4013-진법 변환(정올 1번 수준 문제)

chrmqgozj·2022년 1월 20일
0

CodeUp

목록 보기
3/48
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

long long bin=0;
long long octa=0;
string hexa;
char list[7] = {'A','B','C','D','E','F'};

void bi(int n){
    if(n/2==0){
        bin += 1;
        return;
    }
    else{
        bi(n/2);
        bin=bin*10+n%2;
        return;
    }
}
void oct(int n){
    if(n/8==0){
        octa += n%8;
        return;
    }
    else{
        oct(n/8);
        octa = octa*10+n%8;
        return;
    }
}
void hex(int n){
    if(n/16==0){
        int temp = n%16;
        if(temp>9){
            hexa = list[temp-10];
        }
        else{
            hexa = to_string(temp);
        }
    }

    else{
        hex(n/16);
        int temp = n%16;
        if(temp>9){
            hexa += list[temp-10];
        }
        else{
            hexa += to_string(temp);
        }

        return;
    }
}

int main(){
    int num;
    cin >> num;

    bi(num);
    oct(num);
    hex(num);
    

    cout << "2 " << bin << "\n";
    cout << "8 " << octa << "\n";
    cout << "16 " << hexa << "\n";

}

0개의 댓글

관련 채용 정보