algorithm

BOJ 1373

cheesecrust1008 2021. 12. 23. 16:31
#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <vector>
 
using namespace std;
 
int main() {
    string s = "";
    // string으로 입력
    getline(cin, s);
    
    while (s.size() % 3 != 0) {
    s = '0' + s;
    }
    string ans = "";
    for (int i = 0; i < s.size(); i+=3) {
    int tmp = 0;
    // 3자리씩 8진수로 변환하기
    for (int j = i; j < i+3; j++) {
    tmp += (s[j]-'0')*pow(2,2-(j%3));
    }
    ans += to_string(tmp);
    }
    for (int i = 0; i < ans.size(); i++) {
    printf("%c", ans[i]);
    }
}
 

이때에 뒤에서 부터 하는 것 보다는 위에 처럼 자릿수를 맞추어 놓은 후에 1의 자리가 아닌 큰 자릿수 부터 처리하는 것이 더욱 효과 적이다.

'algorithm' 카테고리의 다른 글

스택  (0) 2021.12.30
링크드 리스트 공부  (0) 2021.12.30
BOJ 1105  (0) 2021.12.22
BOJ 1699번  (0) 2021.12.20
BOJ 11053번  (0) 2021.12.20