36 lines
741 B
C++
36 lines
741 B
C++
|
|
#include <bits/stdc++.h>
|
|||
|
|
|
|||
|
|
using namespace std;
|
|||
|
|
const int N = 110;
|
|||
|
|
int n, r;
|
|||
|
|
int a[N], idx;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
测试数据:
|
|||
|
|
-15 -2
|
|||
|
|
|
|||
|
|
|
|||
|
|
结果 110001
|
|||
|
|
*/
|
|||
|
|
int main() {
|
|||
|
|
cin >> n >> r;
|
|||
|
|
cout << n << "=";
|
|||
|
|
//数位分离
|
|||
|
|
while (n) {
|
|||
|
|
a[idx] = n % r; //余数
|
|||
|
|
n /= r; //商
|
|||
|
|
if (a[idx] < 0) { //余数不能是负的,如果是负的,需要借位
|
|||
|
|
a[idx] += (-r); //借位增加(-r)
|
|||
|
|
n++; //商++
|
|||
|
|
}
|
|||
|
|
idx++;
|
|||
|
|
}
|
|||
|
|
//倒序
|
|||
|
|
for (int i = idx - 1; i >= 0; i--)
|
|||
|
|
if (a[i] >= 10)
|
|||
|
|
printf("%c", (char) ('A' + a[i] - 10));
|
|||
|
|
else printf("%d", a[i]);
|
|||
|
|
|
|||
|
|
cout << "(base" << r << ")";
|
|||
|
|
return 0;
|
|||
|
|
}
|