题解 | #数制转换#

数制转换

https://www.nowcoder.com/practice/8ef02ef8571b417d8c311a87861f7a03?tpId=40&tqId=21387&rp=1&ru=/ta/kaoyan&qru=/ta/kaoyan&difficulty=&judgeStatus=&tags=/question-ranking

#include <stdio.h>
#include <string.h>
#include <math.h>

//下述数字其实就是对应位置的ascii码
int main() {
    char arr[64];
    long a, b;
    while (scanf("%ld %s %ld", &a, arr, &b) != EOF) {
        int len = strlen(arr);
        long num=0;
        //先转化为十进制
        for (int i=0;i<len;i++){
            int temp = arr[i];
            int h;
            if (temp>=48 && temp<=57){
                h = temp-48;
            }else if(temp >=65 && temp <=70){
                h = temp-55;
            }else {
                h = temp-87;
            }
            long n1 = pow(a, len-i-1)*h;
            num+=n1;
        }
        //再转化为对应进制,取余数的思想
        int end=0;
        int ret[64]={0};
        while (num/b) {
            ret[end++] = (num%b);
            num/=b;
        }
        if (num%b!=0){
            ret[end++]=num%b;
        }
        //打印,无营养,不用看
        for (int i=end-1;i>=0;i--){
            if (ret[i]<10){
                printf("%d",ret[i]);
            }else {
                char cc=ret[i]+55;
                printf("%c", cc);
            }
        }
    }
    return 0;
}

#c##c语言#
全部评论

相关推荐

但听说转正率很低,我现在有在实习了,好纠结要不要去
熬夜脱发码农:转正率低归低,但是实习的经历你可以拿着,又不是说秋招不准备了
点赞 评论 收藏
分享
Twilight_m...:经典我朋友XXXX起手,这是那种经典的不知道目前行情搁那儿胡编乱造瞎指导的中年人,不用理这种**
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务