题目描述
输入一个整数,将其转换成八进制数输出。
输入描述:
输入包括一个整数N(0<=N<=100000)。
输出描述:
可能有多组测试数据,对于每组数据,
输出N的八进制表示数。
示例1
输入
7
8
9
输出
7
10
11
代码:
#include<iostream>
using namespace std;
int main(){
int n;
while(cin>>n){
int result[10];
int i=0;
while(n>=8){
result[i++]=n%8;
n=n/8;
}
result[i]=n;
for(int j=i;j>=0;j--){
cout<<result[j];
}
cout<<endl;
}
}
小结:上面是我自己写的代码,后来看了别人的代码,才知道,这题可以直接用cout的格式化输出
#include<iostream>
using namespace std;
int main()
{
int N;
while(cin>>N)
{
cout<<oct<<N<<endl;
}
return 0;
}
既然这样子打打代码回顾一下,cout的格式化输出
//8进制和16进制输出
cout<<oct<<66<<endl;
cout<<hex<<99<<endl;
//设置宽度用0填充
cout.fill('0');
cout.width(4);
cout<<20<<endl;
以上是做题所遇到的格式