您的位置:首页 > 其它

HDU-2031-进制转换

2016-02-05 18:05 316 查看
#include<iostream>
#include<cstdio>
#include<math.h>
using namespace std;
int shi(int m);
char pipei(int m);
int main()
{
int n,R;
while(scanf("%d%d",&n,&R)!=EOF)
{
int wei[33],i=1,yu,chu,zhuan=0;
char str[33];
if(n>0)
{
chu=n/R;
yu=n%R;
str[1]=pipei(yu);
while(chu!=0)
{
i=i+1;
str[i]=pipei(chu%R);
chu=chu/R;

}
int z;
for(z=i;z>=1;z--)
{
printf("%c",str[z]);
}
printf("\n");
/*   if(R<10)
{
for(z=1;z<=i;z++)
{
zhuan=wei[z]*shi(z)+zhuan;//10以内进制转换

}

printf("%d\n",zhuan);
}
else
{

}*/
}
else
{
n=n*(-1);
chu=n/R;
yu=n%R;
str[1]=pipei(yu);
while(chu!=0)
{
i=i+1;
str[i]=pipei(chu%R);
chu=chu/R;

}
int z;printf("-");
for(z=i;z>=1;z--)
{
printf("%c",str[z]);
}
printf("\n");
/* for(z=1;z<=i;z++)
{
zhuan=wei[z]*shi(z)+zhuan;//10以内进制转换
}

printf("-%d\n",zhuan);*/
}

}
return 0;
}
char pipei(int m)
{
if(m==0){return '0';}
if(m==1){return '1';}
if(m==2){return '2';}
if(m==3){return '3';}
if(m==4){return '4';}
if(m==5){return '5';}
if(m==6){return '6';}
if(m==7){return '7';}
if(m==8){return '8';}
if(m==9){return '9';}
if(m==10){return 'A';}
if(m==11){return 'B';}
if(m==12){return 'C';}
if(m==13){return 'D';}
if(m==14){return 'E';}
if(m==15){return 'F';}

}
int shi(int m)//计算位数的10*10*.....
{
int i,z=1;
for(i=1;i<m;i++)
{
z=z*10;
}
return z;
}
//其中注释掉的部分是对于10以内的进制的转换
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: