题解 | #进制转换#

进制转换

https://www.nowcoder.com/practice/deb19498bc644f53a6a99905ef5ee01d

#include <stdio.h>
#include <math.h>
// 计算字符串长度:
int Length(char a[]) {
    int n = 0;
    for (int i = 0; a[i] != '\0'; i++) {
        n++;
    }
    return n;
}
int main() {
    char a[50] = "";
    while((scanf("%s", a))!=EOF)
    {
        int len = Length(a);
    int sum = 0;
    int n = 0;
    for (int i = len - 1; i > 1; i--) {
        if (a[i] >= 'A') {
            int num = a[i] - 'A' + 10;
            sum += num * pow(16, n);
            n++;
        } else {
            int num = a[i] - '0';
            sum += num * pow(16, n);
            n++;
        }
    }
    printf("%d\n", sum);
    }
    
    return 0;
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务