# P1017 M-ary conversion (negative M-ary & Number Theory)

2021-08-10 08:03:52

## P1017 Hexadecimal conversion （ Negative base & number theory ）

Subject portal

The question ： Give an integer and cardinality , Require conversion to the base number of the base number .

Ideas ： The main difficulty of the topic is ： The remainder may be negative , Because when it is converted to this hexadecimal representation, it is in the reverse order of the remainder , Because the divisor ( It's a negative number ) The absolute value of is greater than the absolute value of the remainder , So subtract a divisor from the remainder , Quotient plus a divisor ensures that the remainder is positive , And the result remains the same .

``````#include<bits/stdc++.h>
using namespace std;
void fun(int n,int r){
if(n==0) return;
int m=n%r;
if(m<0) m-=r,n+=r;
if(m>=10) m='A'+m-10;// here m yes ASCII code
else  m=m+'0';
fun(n/r,r);
printf("%c",m);
}
int main(){
int n,r;
cin>>n>>r;
printf("%d=",n);
fun(n,r);
printf("(base%d)\n",r);
return 0;
}
```

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
```

https://chowdera.com/2021/08/20210810080205511U.html