进制转换,主要学习setw规范位数函数和setfill填充函数
几个函数的用法如下:
- reverse(res.begin(),res.end()),将res字符串进行转置。
- setw(2),规定两位的输出结果。
- setfill(‘0’)和setfill一起用的结果是:如果只有一位,则前面自动补0,达到两位。
代码如下:
#include<bits/stdc++.h>
using namespace std;
char mars[13] = {'0','1','2','3','4','5','6','7','8','9','A','B','C'};
string getRedix(int num){
string res;
while(num){
res += mars[num%13];
num /= 13;
}
reverse(res.begin(),res.end());
return res;
}
int main(){
int num;
putchar('#');
for(int i=0;i<3;i++){
cin>>num;
cout<<setw(2)<<setfill('0')<<getRedix(num);
}
return 0;
}