poj 1546

      进制转换问题,很重要,尤其是搜索中用于状态的表示。这题把目标进制弄成十进制再用栈来转换成目标进制就好了。

      以下是代码:

 

  1. #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<cmath>
    using namespace std;
    char dic[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
    char digit[10];
    int chang;
    int stack[100],top;
    int n,m;
  2. void change1()
    {
     int len=strlen(digit);
     chang=0;
     for(int i=0;i<len;i++)
      if(digit[i]!=' ' && digit[i]!='0')
      {
       int temp;
       if(digit[i]>='1' && digit[i]<='9') temp=digit[i]-'0';
       else if(digit[i]=='A') temp=10;
       else if(digit[i]=='B') temp=11;
       else if(digit[i]=='C') temp=12;
       else if(digit[i]=='D') temp=13;
       else if(digit[i]=='E') temp=14;
       else if(digit[i]=='F') temp=15;
       chang+=pow(n,len-i-1)*temp;
      }
    }
  3. void change2()
    {
     top=0;
     int i;
     while(chang)
     {
      int temp=chang%m;
      stack[top++]=temp;
      chang/=m;
     }
    }
  4. int main()
    {
    // freopen("in.txt","r",stdin);
     while(scanf("%s",digit)==1)
     {
      scanf("%d%d",&n,&m);
      change1();
      change2();
      if(top>7)
       printf("  ERROR");
      else
      {
       int i;
       for(i=0;i<7-top;i++)
        printf(" ");
       while(top)
       {
        printf("%c",dic[stack[top-1]]);
        top--;
       }
      }
      printf("/n");
     }
     return 0;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值