반응형

https://www.acmicpc.net/problem/1373

 

1373번: 2진수 8진수

첫째 줄에 2진수가 주어진다. 주어지는 수의 길이는 1,000,000을 넘지 않는다.

www.acmicpc.net

 

뒤에서부터 세 자리씩 슬라이싱하며, 대응되는 8진수를 구해주었습니다.

 

#include <iostream>
#include <algorithm>
#include <map>
using namespace std;

string s, ans = "";
map<string, char> m = { {"000", '0'}, {"001", '1'}, {"010", '2'},
	{"011", '3'}, {"100", '4'}, {"101", '5'}, {"110", '6'}, {"111", '7'},
	{"0", '0'}, {"1", '1'}, {"10", '2'}, {"11", '3'} };

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

	cin >> s;
	int i = s.size() - 3;
	for (; i >= 0; i -= 3) ans.push_back(m[s.substr(i, 3)]);
	if(i != -3) ans.push_back(m[s.substr(0, i + 3)]);
	reverse(ans.begin(), ans.end());
	cout << ans;
}
반응형

'Algorithm' 카테고리의 다른 글

백준 21869 : Maximum Bishop  (0) 2021.11.20
백준 1247 : 부호  (0) 2021.11.20
백준 1212 : 8진수 2진수  (0) 2021.11.20
백준 10798 : 세로읽기  (0) 2021.11.20
백준 10610 : 30  (0) 2021.11.20

+ Recent posts