You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
741 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#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;
}