zoukankan      html  css  js  c++  java
  • hdu 1335 Basically Speaking

    题目:

    之前也做过两个有关进制的转换的题,一个是不涉及到16#的,所以直接用的int来存的,最后结果保留在数组中,然后倒叙输出。

    一个是将各种进制分开的,这也是刚接触 C 语言的时候的想法。

    下面这种就很好了 先base#-->10# ,再 10# --> pbase#

    #include<stdio.h>
    
    void change(char str[],int base,int pbase)
    {
        //base# --> 10#
        int val=0;
        for(int i=0; str[i]!=''; i++)
        {
            if(str[i]>='0'&&str[i]<='9')
                val= val*base+str[i]-'0';
            else
                val=val*base+str[i]-55;//A的ASCII是65
        }
        
        //10# --> pbase#
        int result = val;
        char last[100],p=0;
        while(result)
        {
            int x = result%pbase;
            if(x>=0&&x<=9)
                last[p++]=x+'0';
            else
                last[p++]=x+55;
            result = result/pbase;
        }
    
        if(p>7) printf("  ERROR");
        else
        {
            int y = 7-p;
            while(y--){
                printf(" ");
    
            }
            for(int i=p-1; i>=0; i--)
                printf("%c",last[i]);
        }
        printf("
    ");
    
    }
    int main()
    {
        char str[10];
        int base,pbase;
        while(~scanf("%s%d%d",str,&base,&pbase))
            change(str,base,pbase);
        return 0;
    }
    

  • 相关阅读:
    2016去哪儿编程题:乘坐公交
    lintcode:交错正负数
    lintcode:Ugly Number I
    KMP算法
    适配器模式
    迭代器模式
    命令模式
    外观模式
    工厂方法模式
    代理模式
  • 原文地址:https://www.cnblogs.com/qie-wei/p/10160213.html
Copyright © 2011-2022 走看看