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

 

1373번: 2진수 8진수

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

www.acmicpc.net

 

 

 

 

문제

 

2진수가 주어졌을 때, 8진수로 변환하는 프로그램을 작성하시오.

 

 

 

입력

 

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

 

 

 

출력

 

첫째 줄에 주어진 수를 8진수로 변환하여 출력한다.

 

 

 

 

코드

 

#include <iostream>
#include <string>
using namespace std;


int main(){
	string s;
	cin>>s;

	int len=s.length();

	if(len%3==2) cout<<(s[0]-'0')*2+(s[1]-'0');
	if(len%3==1) cout<<s[0]-'0';
	for(int i=len%3; i<len; i+=3)
		cout<<(s[i]-'0')*4+(s[i+1]-'0')*2+(s[i+2]-'0');
}
728x90

'알고리즘 > 백준' 카테고리의 다른 글

백준 2089번: -2진수  (0) 2020.05.18
백준 1212번: 8진수 2진수  (0) 2020.05.18
백준 2734번: 진법 변환  (0) 2020.05.18
백준 11005번: 진법 변환 2  (0) 2020.05.18
백준 9613번: GCD 합  (0) 2020.05.18