# 생각
C에서는
%d로 입력받고 %o를 사용해
출력하면 8진수로 간단하게 변환이 가능하다
#include <cstdio>
#pragma warning(disable:4996) //disable scanf warning
int main()
{
int n;
scanf("%d", &n);
printf("%o", n);
}
CPP에서도 I/O manipulator를 사용하여
간단하게 변환이 가능하다
std::dec std::hex std::oct
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> dec >> n;
cout << oct << n;
}
또한 vector를 이용하여 작성할 수 있다
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int dec;
cin >> dec;
vector<int> v;
while (dec)
{
v.push_back(dec % 8);
dec /= 8;
}
reverse(v.begin(), v.end()); // 뒤로 순차적으로 삽입 되기 때문에 reverse
for (auto i : v)
{
cout << i;
}
}
'Algorithm > CodeUp' 카테고리의 다른 글
[CodeUp] 1082 : [기초-종합] 16진수 구구단? with cpp (0) | 2022.01.22 |
---|---|
[CodeUp] 1067 : [기초-조건/선택실행구조] 정수 1개 입력받아 분석하기(설명) with cpp (0) | 2022.01.21 |
[CodeUp] 1029 : [기초-데이터형] 실수 1개 입력받아 그대로 출력하기2(설명) with cpp (0) | 2022.01.18 |
[CodeUp] 1027 : [기초-입출력] 년월일 입력 받아 형식 바꿔 출력하기(설명) with cpp (0) | 2022.01.18 |
[CodeUp] 1025 : [기초-입출력] 정수 1개 입력받아 나누어 출력하기(설명) with cpp (0) | 2022.01.18 |