洛谷 P1017 [NOIP 2000 提高组] 进制转换
思路:
原本不会负进制,去查了一下,学习了一下负进制运算,然后直接模拟短除法就行了。
AcCode:
#include<iostream>
#include<stack>
#include<map>
using namespace std;
stack<char> s;
map<int, char> itoc;
int main(){
for(int i = 0; i <= 9; i++){
itoc[i] = (char)(i + '0');
}
char llll = 'A';
for(int i = 10; i <= 20; i++){
itoc[i] = llll++;
}
int N, R;
cin >> N >> R;
cout << N << "=";
while(N){
int n = N / R;
int y = N % R;
if(y < 0){
s.push(itoc[y + (-R)]);
n += 1;
}else{
s.push(itoc[y]);
}
N = n;
}
while(!s.empty()){
cout << s.top();
s.pop();
}
cout << "(base" << R << ")";
return 0;
}