HDU-2031-进制转换

来源:互联网 发布:冒险岛2捏脸数据库 编辑:程序博客网 时间:2024/06/02 06:22
#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以内的进制的转换

0 0
原创粉丝点击