P1017 题解
题目传送门
思路
在本文中,设被除数为
首先考虑
再考虑
当
AC CODE
#include<bits/stdc++.h>
using namespace std;
int read(){int x=0;char f=1,ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();return x*f;}
int a,b;
void dfs(int x){
if(!x)
return;
int q=x/b,r=x%b;
if(r<0)
++q,r-=b;
dfs(q);//倒序输出
if(r<10)
putchar(r+'0');
else putchar(r+'A'-10);
return;
}
int main(){
a=read(),b=read();
printf("%d=",a);
dfs(a);
printf("(base%d)\n",b);
return 0;
}