Files
python/TangDou/LuoGuBook/P1017_Prepare.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

36 lines
741 B
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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