Original title link: https: //www.luogu.com.cn/problemnew/show/P1017
negative and positive binary hex conversion of almost
all short division
The turnover number is assumed as r n-ary (r <0)
to put a positive number r as
the first count remainder
mod = n% r
then count commercially
t = n / r
However, the remainder should be 0 ~ (-r-1), the remainder of the above formula seek out possible negative
then add determines
if mod is negative:
mod = mod-R & lt (R & lt radix)
T ++ (supplier also plus 1 )
Code:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
string op="0123456789ABCDEFGHIJ";
string st;
int main()
{
int n,r,cnt=0;
cin>>n>>r;
cout<<n<<"=";
while(n!=0)
{
int k=n%r;
int c=n/r;
if(k<0)
{k-=r;
c++;}
n=c;
st[cnt++]=op[k];
}
for(int i=cnt-1;i>=0;i--)
{cout<<st[i];}
cout<<"(base"<<r<<")"<<endl;
return 0;
}