01:数制转换

本文介绍了一种实现不同进制(2进制到16进制)非负整数转换的方法,通过具体示例展示了从一种进制到另一种进制转换的过程,并提供了完整的C++实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

01:数制转换


总时间限制: 
1000ms 
内存限制: 
65536kB
描述

求任意两个不同进制非负整数的转换(2进制~16进制),所给整数在long所能表达的范围之内。
不同进制的表示符号为(0,1,...,9,a,b,...,f)或者(0,1,...,9,A,B,...,F)。

输入
输入只有一行,包含三个整数a,n,b。a表示其后的n 是a进制整数,b表示欲将a进制整数n转换成b进制整数。
a,b是十进制整数,2 =< a,b <= 16。
输出
输出包含一行,该行有一个整数为转换后的b进制数。输出时字母符号全部用大写表示,即(0,1,...,9,A,B,...,F)。
样例输入
15 Aab3 7
样例输出
210306
来源
2005~2006医学部计算概论期末考试

分析: 水题。。。。。水。。。。。

#include <stdio.h>
#include <iostream>
#include <stack>
#include <string.h>
#include <queue>
#include <cmath>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <string>
using namespace std;
typedef long long LL;
#define MAX 1001
//int a[MAX][MAX];
//int b[MAX][MAX];
//int res[MAX][MAX];
//int n, m;
char c[MAX];
void fun(int a_Before, string str_Before, int b_After)
{
    for(int i = 0; i < str_Before.size(); i++){
        if(str_Before[i] >= 'A' && str_Before[i] <= 'Z'){
            str_Before[i] += 32;
        }
    }
    LL sum = 0;
    int j = str_Before.size();
    for(int i = 0; i < str_Before.size(); i++){
        if(str_Before[i] >= 'a' && str_Before[i] <= 'z'){
            sum += (str_Before[i] - 'a' + 10) * pow(a_Before, --j);
        }else {
            sum += (str_Before[i] - '0') * pow(a_Before, --j);
        }
    }
    if(sum == 0){
        cout << "0" << endl;
        return;
    }
    //cout << sum << endl;
    j = 0;
    do{
        LL yushu = sum % b_After;
        if(yushu <= 9){
            c[j++] = yushu + '0';
        }else {
            switch(yushu){
            case 10:
                c[j++] = 'A';
                break;
            case 11:
                c[j++] = 'B';
                break;
            case 12:
                c[j++] = 'C';
                break;
            case 13:
                c[j++] = 'D';
                break;
            case 14:
                c[j++] = 'E';
                break;
            case 15:
                c[j++] = 'F';
                break;
            }
        }
        sum /= b_After;

    }while(sum != 0);

    int len = strlen(c);
    for(int i = len - 1; i >= 0; i--){
        cout << c[i];
    }
    cout << endl;
}


int main() {
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    int a_Before;
    string str_Before;
    int b_After;
    cin >> a_Before >> str_Before >> b_After;
    fun(a_Before, str_Before, b_After);
    return 0;

}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值