Algorithm/Mathematics
Baekjoon 1212 8진수 2진수
GirlFriend_Yerin
2019. 3. 31. 23:24
Link https://www.acmicpc.net/problem/1212
소스결과 2312 KB / 32 ms
출처 Baekjoon
언어 C++ 17
분류 진법, 수학
설명
주어진 8진수를 2진수로 출력하자.
문제는 크게 어렵지 않다. 8진수와 2진수의 관계만 안다면.
2진수를 3자리씩 묶어 10진수로 변환하면 8진수가 된다. 위 문제는 반대로 만들면 된다.
알고리즘
1. 8진수를 입력 받는다.
2. 첫자리인경우 1,2,3 에 한정해 직접 출력한다.
3. 나머지 자리는 8진수를 2진수로 변환한 결과를 출력한다.
소스코드
#include <iostream>
#include <cstring>
using namespace std;
const int MAX = 333334;
char input[MAX+1];
void oct2Dex(char o)
{
if (o == '0')
cout << "000";
else if (o == '1')
cout << "001";
else if (o == '2')
cout << "010";
else if (o == '3')
cout << "011";
else if (o == '4')
cout << "100";
else if (o == '5')
cout << "101";
else if (o == '6')
cout << "110";
else if (o == '7')
cout << "111";
}
int main()
{
cin >> input;
int len = strlen(input);
if (input[0] != '0')
{
if (input[0] == '1')
cout << 1;
else if (input[0] == '2')
cout << 10;
else if (input[0] == '3')
cout << 11;
else
oct2Dex(input[0]);
for (int i = 1; i < len; i++)
oct2Dex(input[i]);
}
else
cout << 0;
return 0;
}