【模拟】进制转换(负进制)

P1017
模拟的精髓就在其中(逃

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<stdio.h>
#include<math.h>
char num[30]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J'};
int main(){
int n,a,i,p=0,t,ed=0,ans[100]={0};
scanf("%d%d",&n,&a);
do{
ans[0]++;
for(p=0;p<=ed;p++){
if(ans[p]>=-a){
ans[p]=0;
ans[p+1]++;
}
}
if(ans[ed+1]!=0)ed++;
t=0;
for(i=0;i<=ed;i++){
t+=ans[i]*pow(a,i);
}
}while(t!=n);
printf("%d=",n);
for(i=ed;i>=0;i--)
printf("%c",num[ans[i]]);
printf("(base%d)",a);
return 0;
}