# 생각
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;
}
}
https://codeup.kr/problem.php?id=1031
[기초-출력변환] 10진 정수 1개 입력받아 8진수로 출력하기(설명)
C언어기초100제v1.2 : @컴퓨터과학사랑, 전국 정보(컴퓨터)교사 커뮤니티/연구회 - 학교 정보(컴퓨터)선생님들과 함께 수업/방과후학습/동아리활동 등을 통해 재미있게 배워보세요. - 모든 내용
codeup.kr
'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 |