进制转换
2026-07-21 09:41:36
发布于:浙江
22阅读
0回复
0点赞
这一道题有一个大坑, 作者在这个坑里栽了6次
在NOIP2000 原题里, 需要标准的输出格式。
但是在ACGO里, 这题只用输出负进制数字串就行
因此在这里, 作者郑重提醒广大朋友一定要看清输出格式
像这道题: 在这里只用:
cout << ans;
而在NOIP的原题里, 就要用:
cout << temp << "=" << s << "(base" << r << ")" << endl;
代码如下:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int n, base, num, rem;
string ans;
char getChar(int x){
if (x < 10){
return '0' + x;
}
return 'A' + x - 10;
}
int main(){
cin >> n >> base;
num = n;
if (num == 0){
ans = "0";
}
else{
while (num != 0){
rem = num % base;
num /= base;
if (rem < 0){
rem -= base;
num += 1;
}
ans += getChar(rem);
}
reverse(ans.begin(), ans.end());
}
cout << ans;
return 0;
}
这里空空如也






有帮助,赞一个