Chri_K

负进制转换(noip2000)

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
void change(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;
else m+='0';
change(n/r,r);
printf("%c",m);
return ;
}
int main()
{
int n,r;
cin>>n>>r;
cout<<n<<"=";
change(n,r);
printf("(base%d)",r);
return 0;
}

 

//std代码

#include<cstdio>
const char key[18]={
'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G'
};
int a[1000];
int main()
{
freopen("t1.in","r",stdin);
freopen("t1.out","w",stdout);
int n,k,i;
while (scanf("%d%d",&n,&k)!=EOF)
{
printf("%d=",n);
i=0;
while (n<0 || n>=-k)
{
a[i]=n%k;
n/=k;
if (a[i]<0)
{
a[i]-=k;
n++;
}
i++;
}
a[i]=n;
for (;i>=0;i--)
printf("%c",key[a[i]]);
printf("(base%d)",k);
}
return 0;
}

posted on 2020-09-30 23:01  Chri_K  阅读(93)  评论(0编辑  收藏  举报