10进制转8进制【华中科技大学考研机试题】
十进制转八进制
点击查看代码
输入一个整数 N
,将其转换成八进制数输出。
输入格式
输入包含多组测试数据。
每组数据占一行,包含一个整数 N
。
输出格式
每组数据输出占一行,输出 N
的八进制表示数。
数据范围
每个输入最多包含 100
组数据。
\(0≤N≤10^5\)
输入样例:
7
8
9
输出样例:
7
10
11
代码
点击查看代码
#include<iostream>
using namespace std;
const char nl = '\n';
const int N = 1e5 + 10;
int st[N];
int main(){
int n;
while(cin >> n){
if(!n)cout << 0;
else{
int top = 0;
while(n){ //获取每一位
st[++top] = n % 8;
n /= 8;
}
while(top){
cout << st[top--];
}
}
cout << nl;
}
}
``