https://www.luogu.com.cn/problem/P1017
题意:给一个十进制数,转换成负进制数。
思路:对于余数为负的情况,考虑借位。
#include<bits/stdc++.h>
using namespace std;
// clock_t start, end;
// start = clock();
// end = clock();
// cout << (double) (end - start) / CLOCKS_PER_SEC << endl;
//ios::sync_with_stdio(false);
#define int long long
#define rep(i, x, y) for(int i=(x);i<=(y);++i)
#define dep(i, x, y) for(int i=(x);i>=(y);--i)
#define gcd(a, b) __gcd(a,b)
const long long mod = 998244353;
const int maxn = 1e6 + 10;
int a[maxn], cnt;
signed main() {
int n, r;
cin >> n >> r;
int flag = 0;
int cas=n;
while (n) {
if (n % r < 0) {
flag = 1;
a[cnt++] = n % r - r;
n = n / r + 1;
} else {
a[cnt++] = n % r;
n/=r;
}
}
cout<<cas<<"=";
for(int i=cnt-1;i>=0;i--){
if(a[i]>9)cout<<(char)(a[i]+'A'-10);
else
cout<<a[i];
}
cout<<"(base"<<r<<")"<<endl;
return 0;
}