题解 | #八进制#
八进制
http://www.nowcoder.com/practice/eda051c1effc4dffa630bc8507f0c5f7
#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
using namespace std;
string str;
char inttochar(int x){ //数字转字符
if(x<10){
return x+'0';
}
else{
return x-10+'a';
}
}
int chartoint(char c){ //字符转数字
if(c>='0'&&c<='9'){
return c-'0';
}
else{
return c-'A'+10;
}
}
int main(){
while(cin>>str){
long long number=0;
vector<char>answer;
for(int i=0;i<str.size();++i){ //把输入转成十进制
number*=10;
number+=chartoint(str[i]);
}
while(number!=0){
answer.push_back(inttochar(number%8)); //十进制转八进制,并写入向量中
number/=8;
}
for(int i=answer.size()-1;i>=0;--i){ //逆序输出答案
printf("%c",answer[i]);
}
printf("\n");
}
return 0;
}