首页 > 试题广场 >

数制转换

[编程题]数制转换
  • 热度指数:12772 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
    求任意两个不同进制非负整数的转换(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)。
示例1

输入

15 Aab3 7

输出

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

int changeXY(int d[],int len,int ans[],int x,int y){
    int k=0;
    for(int i=0;i<len;){
        int r=0;
        for(int j=0;j<len;j++){
            int t=(d[j]+r*x)%y;
            d[j]=(d[j]+r*x)/y;
            r=t;
        }
        ans[k++]=r;
        while(d[i]==0) i++;
    }
    return k;
}

int main(){
    int a,b;
    char str[100];
    while(scanf("%d %s %d",&a,&str,&b)!=EOF){
        int d[10000],ans[10000];
        int len=strlen(str);
        for(int i=0;i<len;i++){
            if(str[i]>='a'&&str[i]<='z'){
                str[i]=str[i]-'a'+'A';
            }
            if(str[i]>='A'&&str[i]<='Z'){
                d[i]=str[i]-'A'+10;
            }else{
                d[i]=str[i]-'0';
            }
        }
        int k=changeXY(d,len,ans,a,b);
        for(int i=k-1;i>=0;--i){
            if(ans[i]>9){
                printf("%c",ans[i]-10+'A');
            }else{
                printf("%d",ans[i]);
            }
        }
        printf("\n");
    }

    return 0;
}

发表于 2019-03-05 14:36:58 回复(0)
//记得用do{  }while();这道题的测试数据没有0,所以用while也能通过测试。
//但是在电脑上0无法输出
#include<stdio.h>
#include<string1.h>
int main() {
    int a, b;
    char str[40];
    while (scanf("%d%s%d", &a, str, &b) != EOF) {
        long temp = 0; 
        int size = strlen(str);
        for (int i = 0; i < size; i++) {
            if (str[i] <= 'f'&&str[i] >= 'a') {
                temp = (str[i] - 'a') + 10 + temp * a;
            }
            else if (str[i] <= 'F'&&str[i] >= 'A') {
                temp = (str[i] - 'A') + 10 + temp * a;
            }
            else temp = (str[i]-'0') + temp * a;
        }
        char str2[40]; int p = 0;
         do{
            if (temp%b >= 10) {
                str2[p++] = temp % b - 10 + 'A';
            }
            else str2[p++] = temp % b + '0';
            temp /= b;
         } while (temp);
        for (int i = --p; i >= 0; i--) {
            printf("%c", str2[i]);
        }
        printf("\n");
    }
    return 0;
}

编辑于 2018-03-04 21:05:32 回复(2)
#include<stdio.h>//本道题的难点主要解决abcd这些值
#include<string.h>//思想:善于利用数组的下标代表abcd这些值
#include<math.h>
int main()
{
    char a[17]="0123456789ABCDEF";
    char b[100],c[100];int m,n,i,j,sum,num;
    scanf("%d%s%d",&m,b,&n);
    for(i=strlen(b)-1;i>=0;i--)//0.小写字母转换成大写字母
		if(b[i]>='a'&&b[i]<='z')
			b[i]=b[i]-32;
    num=0;sum=0;//1.m进制转换成十进制
    for(i=strlen(b)-1;i>=0;i--)
        for(j=0;j<17;j++)
            if(a[j]==b[i])//找下标做实际值
            {sum+=j*pow(m,num++);break;}
	num=0;//2.sum转换成n进制
	while(sum)
	{
		i=sum%n;//得到的余数做a的下标
		c[num]=a[i];//根据下标找c[]的值
		sum/=n;num++;
	}
	for(i=num-1;i>=0;i--)//倒序输出
		printf("%c",c[i]);
	printf("\n");
}

编辑于 2020-04-02 14:31:08 回复(0)
#include<bits/stdc++.h>
char n[65];
int main(){
    long s=0;
    int a,b;
    while(scanf("%d %s %d ",&a,n,&b)!=EOF){
        int l=strlen(n);
        for(int i=l-1;i>=0;i--)
            if(n[i]>='0' && n[i]<='9')
                s=s+(n[i]-'0')*pow(a,l-i-1);
            else if(n[i]>='a' && n[i]<='f')
                s=s+(n[i]-'a'+10)*pow(a,l-i-1);
            else s=s+(n[i]-'A'+10)*pow(a,l-i-1);
        for(int i=0;s;i++){
            n[i]=s%b; s/=b; l=i;
        }
        for(int i=l;i>=0;i--)
            if(n[i]>=10) printf("%c",n[i]-10+'A');
            else printf("%d",n[i]);
        printf("\n");
    }
}
发表于 2019-03-14 16:04:50 回复(0)

python3十行解法。


def baseN(num, b):
    return ((num == 0) and "0") or (baseN(num // b, b).lstrip("0") + "0123456789ABCDEFGHIGKLMNOPQRSTUVWXYZ"[num % b])

while True:
    try:
        a,b,c=input().split()
        val=int(b,int(a))
        print(baseN(val,int(c)))
    except:
        break

通过测试。

发表于 2017-10-16 16:28:55 回复(1)
#include<bits/stdc++.h>
using namespace std;

int ChartoInt(char ch)
{
    if(ch>='0'&&ch<='9')
        return ch-'0';
    else if(ch>='a'&&ch<='b')
        return ch-'a'+10;
    else
        return ch-'A'+10;
}
char InttoChar(int num)
{
    if(num>=0&&num<=9)
        return num+'0';
    else
        return num-10+'A';
}
int main()
{
    int a,b;//a进制转化为b进制
    string num;
    while(cin>>a>>num>>b)
    {
        long number=0;
        stack<char>result;
        for(int i=0;i<num.size();i++)//将a进制转换为十进制
            number=number*a+ChartoInt(num[i]);
        while(number!=0)//将十进制转换为b进制
        {
            result.push(InttoChar(number%b));
            number/=b;
        }
        while(!result.empty())
        {
            cout<<result.top();
            result.pop();
        }
        cout<<endl;
    }
    return 0;
}

发表于 2022-03-07 13:36:50 回复(0)
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
using namespace std;

vector<char> myVector;

int toInt(char c)
{
    if (c >= '0' && c <= '9')
    {
        return c - '0';
    }
    else if (c >= 'a' && c <= 'z')
    {
        return c - 'a' + 10;
    }
    else
    {
        return c - 'A' + 10;
    }
}

long convertToTen(string str, int a)
{
    long res = 0;
    for (int i = 0; i < str.size(); i++)
    {
        res *= a;
        res += toInt(str[i]);
    }
    return res;
}

char toChar(int c)
{
    if (c < 10)
    {
        return c + '0';
    }
    else
    {
        return c - 10 + 'A';
    }
}

void convertToN(long ret, int b)
{
    if (ret == 0)
    {
        myVector.push_back('0');
    }
    while (ret)
    {
        myVector.push_back(toChar(ret % b));
        ret /= b;
    }
    for (int i = myVector.size() - 1; i >= 0; i--)
    {
        printf("%c", myVector[i]);
    }
    cout << endl;
}

int main()
{
    int a, b;
    string str;
    while (cin >> a >> str >> b)
    {
        long ret = convertToTen(str, a);
        convertToN(ret, b);
        myVector.clear();
    }
    return EXIT_SUCCESS;
}

发表于 2021-02-18 18:12:39 回复(0)
#include <stdio.h>
#include <string.h>

char IntToChar(int x)
{
    if(x<10)
        return x+'0';
    else
        return x-10+'A';
}

int CharToInt(char c){
    if(c>='0'&&c<='9')
        return c-'0';
    else if(c>='A'&&c<='Z')
        return c-'A'+10;
    else
        return c-'a'+10;
}

int main()
{
    char s[100],ans[100];
    int m,n;
    int len,i;
    long long num;
    while(scanf("%d %s %d",&m,&s,&n)!=EOF)
    {
        len=strlen(s);
        for(num=0,i=0;i<len;i++)
        {
            num*=m;
            num+=CharToInt(s[i]);
        }
        i=0;
        while(num>0)
        {
            ans[i++]=IntToChar(num%n);
            num/=n;
        }
        while(i>0) printf("%c",ans[--i]);
        printf("\n");
    }
}

发表于 2021-02-17 22:43:11 回复(1)

比较简单,注意大小写即可。

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int a,b;
    string n;
    while(cin>>a>>n>>b)
    {
        long num=0;
        string result="";
        while(n[0]=='0')
            n=n.substr(1);
        int k=1;
        for(int i=n.length()-1;i>=0;--i)
        {
            if(n[i]>='0'&&n[i]<='9')
                num+=(n[i]-'0')*k;
            else if(n[i]>='a'&&n[i]<='z')
                num+=(n[i]-'a'+10)*k;
            else if(n[i]>='A'&&n[i]<='Z')
                num+=(n[i]-'A'+10)*k;
            k*=a;
        }
        while(num!=0)
        {
            if(num%b<=9)
                result=to_string(num%b)+result;
            else
                result=string(1,num%b-10+'A')+result;
            num/=b;
        }
        cout<<result<<endl;
    }
    return 0;
}
发表于 2021-01-30 23:01:45 回复(0)
//p -= !n是为了兼容0,在这里去掉也是对的
#include <iostream>
using namespace std;
int main() {
    long long a, b, n;
    char s[0x71], *p;
    while (cin >> a >> (p = s + 0x30) >> b) {
        for (n = 0; *p; ++p)
            n = n * a + *p - (*p < 'A' ? '0' : *p < 'a' ? 'A' - 10: 'a' - 10);
        for (p -= !n; n; n /= b)
            *p += (*--p = n % b) > 9 ? 'A' - 10 : '0';
        cout << p << endl;
    }
    return 0;
}

编辑于 2021-01-27 04:09:53 回复(0)
#include<stdio.h>
(737)#include<string.h>

char s[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
void help(char* ret, int a, char* ans, int b)
{
    int len=strlen(ret), cnt = 0, k = 0, t;
    for(int i = 0; i < len; )
    {
        k = 0;
        for(int j = i; j < len; j++)
        {
            if(ret[j] >= 'A' && ret[j] <= 'F')
            {
                t = (k*a+ret[j]-'A'+10);
                ret[j] = s[t/b];
                k = t%b;
            }
            else
            {
                t = (k*a+ret[j]-'0');
                ret[j] = s[t/b];
                k = t%b;                
            }
        }
        ans[cnt++] = s[k];
        while(ret[i] == '0') i++;        
    }
    ans[cnt]='\0';
    return ;
    
}
int main()
{
    int a, b,len;
    char ret[1000], ans[1000];
    
    while(scanf("%d %s %d", &a, ret, &b)!=EOF)
    {
        len = strlen(ret);
        for(int i = 0; i < len; i++)
            if(ret[i] >= 'a' && ret[i] <= 'z') ret[i] -= 32;
        help(ret,a,ans,b);
        len = strlen(ans);
        for(int i = len-1; i > -1; i--)
            printf("%c", ans[i]);
        printf("\n");
    }
    
    return 0;
}

发表于 2020-03-30 21:45:43 回复(0)
#include<iostream>
#
include<string>
#include<vector>
using namespace std;

long CharToLong(char c){//数字转字符
    long n;
    if(c<='9'&&c>='0'){
        n=c-'0';
    }
    else if(c>='A'&&c<='F'){
        n=c-'A'+10;
    }
    else{
        n=c-'a'+10;
    }
    return n;
}

char LongToChar(long x){    //字符转数字
    if(x<10){
        return x+'0';
    }else{
        return x-10+'A';
    }
}

int main(){
    long  a,b;
    string n;
    while(cin>>a>>n>>b){
        long num=0;
        for(int i=0;i<n.size();i++){    //a进制转十进制
            num*=a;
            num+=CharToLong(n[i]);
        }
        vector<char> answer;
        while(num!=0){                   //十进制转b进制
            answer.push_back(LongToChar(num%b));
            num/=b;
        }
        for(int i=answer.size()-1;i>=0;i--){    //逆序输出
            cout<<answer[i];
        }
        cout<<endl;
    }

发表于 2020-03-15 23:30:49 回复(0)

关于A~F的转化,提供两种思路:一个是直接ascii码进行运算(如:‘A’的ascii为65,要转化为10,只需‘A’-55,即可)另一个是用map容器,如下代码,但是不推荐此做法,太笨了。。。
#include <bits/stdc++.h>
using namespace std;
map<char,int>m;
vector<char>result;
int sum=0;
int x_to_ten(int a,string n){
	int j=0;
	for(auto i=n.rbegin() ;i!=n.rend();i++){
		if(*i!=0){
			if(*i>='A') sum+=m[*i]*pow(a,j);
			else  sum+=(*i-'0')*pow(a,j);
		}
		j++;
	}
	return sum;

}
void ten_to_x(int b,int n){
	while(n!=0){
		if(n%b<10)
		result.push_back(n%b+'0');
		else	result.push_back((char)(n%b+55));

		n/=b;
	}
	for(int i=result.size()-1;i>=0;i--){
		 cout<<result[i];
	}
}
int main(){
	string n;
	int a,b;
	m['A']=10;m['a']=10;
	m['B']=11;m['b']=11;
	m['C']=12;m['c']=12;
	m['D']=13;m['d']=13;
	m['E']=14;m['e']=14;
	m['F']=15;m['f']=15;
	cin>>a>>n>>b;
	ten_to_x(b,x_to_ten(a,n));
	//cout<<x_to_ten(a,n);
	return 0;
}


发表于 2020-03-03 23:37:48 回复(0)

又臭又长

#include<string>
using namespace std;

string change(int a, string n, int b){
    long dec = 0;
    long temp = 1;
    for(int i = n.length() - 1; i >= 0; i--){
        if(n[i] >= 'A' && n[i] <= 'F'){
            dec += temp * (n[i] - 'A' + 10);
        }
        else if(n[i] >= 'a' && n[i] <= 'f'){
            dec += temp * (n[i] - 'a' + 10);
        }
        else{
            dec += temp * (n[i] - '0');
        }
        temp *= a;
    }

    string ret = "";
    while(dec){
        temp = dec % b;
        if(temp > 9){
            ret += (char)(temp - 10 + 'A');
        }
        else{
            ret += (char)(temp + '0');
        }
        dec /= b;
    }
    return ret;
}

int main(){
    int a, b;
    string n;
    while(cin >> a >> n >> b){
        string ret = change(a, n, b);
        for(int i = ret.length() - 1; i >= 0; i--){
            cout << ret[i];
        }
        cout << endl;
    }
    return 0;
}
发表于 2019-03-13 19:46:14 回复(0)
#include <cstdio>
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
int to;
vector<int> sto;
int k;
long toTen(string s){
    long sum = 0;
    for(int i =s.length()-1,j=0;i >= 0;i--,j++){
        if(s[i]>='0'&&s[i]<='9')
            sum+=(s[i]-'0')*pow(k,j);
        else if(s[i]>='A'&&s[i]<='F')
            sum+=(s[i]-'A'+10)*pow(k,j);
        else if(s[i]>='a'&&s[i]<='f')
            sum+=(s[i]-'a'+10)*pow(k,j);
    }
    return sum;
}
void toStr(long num){

    while(num!=0){
        sto.push_back(num%to);
        num/=to;
    }
}
int main(){
    cin>>k;
    string s;
    cin>>s;
    long numTen = toTen(s);
    //cout<<numTen;
    cin>>to;

    toStr(numTen);
    for(int i = sto.size()-1;i >= 0 ;i--){
        if(sto[i]>9)
            cout<<(char)(sto[i]-10+'A');
        else
            cout<<sto[i];
    }

    return 0;
}

发表于 2019-03-01 13:47:03 回复(0)
这题难度也不是很大,提交的时候有两个出错的地方没有注意,一个是,由于存储的时候用数字存储,所以没有转化为A-F,这里注意审题,题目有相关的要求,用一下强制类型转化。其他就没什么了
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;
int main(){
    int a,b;
    string str;
    char ch[30];
    int num[30];
    while(cin>>a>>str>>b){
        long total=0;
        strcpy(ch,str.c_str());
        for(int i=0;i<str.length();i++){
            if(ch[strlen(ch)-1-i]>='a'&&ch[strlen(ch)-1-i]<='f')
                total+=(ch[strlen(ch)-1-i]-'a'+10)*pow(a,i);
            else if(ch[strlen(ch)-1-i]>='A'&&ch[strlen(ch)-1-i]<='F')
                total+=(ch[strlen(ch)-1-i]-'A'+10)*pow(a,i);
            else if(ch[strlen(ch)-1-i]>='0'&&ch[strlen(ch)-1-i]<='9')
                total+=(ch[strlen(ch)-1-i]-'0')*pow(a,i);
        }
        int temp=0;
        while(total>0){
            num[temp++]=total%b;
            total/=b;
        }
        for(int i=temp-1;i>=0;i--){
            if(num[i]>=10)
                cout<<(char)(num[i]-10+'A');
            else
                cout<<num[i];
        }
        cout<<endl;
    }
}

发表于 2019-02-08 17:27:55 回复(1)
voc=['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F']
def f(n,b):
    t=[]
    while n != 0:
        t.append(voc[n%b])
        n = n//b
    return ''.join(t)[::-1]
while 1:
    try:
        s=input().split()
        a,b=int(s[0]),int(s[2])
        n=int(s[1],a)
        print(f(n,b))
    except:
        break

发表于 2017-09-05 10:07:16 回复(0)
#include<iostream>
#include<math.h>
#include<algorithm>
#include<string>
using namespace std;
int main(){
    
    int charTonumber[256];
    
    for(int i='0';i<='9';i++){
        charTonumber[i]=i-'0';
    }
    for(int i='a';i<='f';i++){
        charTonumber[i]=i-'a'+10;
    }
    for(int i='A';i<='F';i++){
        charTonumber[i]=i-'A'+10;
    }
	
	
	char numberToChar[20];
	for(int i=0;i<=9;i++){
		numberToChar[i]=i+'0';
	}
	for(int i=10;i<16;i++){
		numberToChar[i]=i-10+'A';
	}
	int beforeChange;
    string number;
    int afterChange;
	while(cin>>beforeChange>>number>>afterChange){
		int changeNumber=0;
		
		for(int i=0;i<number.size();i++){
			changeNumber+=charTonumber[number.at(i)]*(int)pow(beforeChange+0.0,number.size()-1.0-i);
		}

		string result;
		while(changeNumber>0){
			int temp=changeNumber%afterChange;
			result+=numberToChar[temp];
			changeNumber/=afterChange;
		}
		reverse(result.begin(),result.end()); 
		cout<<result<<endl;
	}
    return 0;
}

发表于 2016-10-26 13:54:27 回复(0)
while True:
    try:
        baseChar = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        numInput = input().split()
        a = int(numInput[0])
        b = int(numInput[2])
        base10 = 0
        temp = numInput[1].upper()
        index = 0
        while temp:               #先转成10进制
            base10 += baseChar.index(temp[-1])*a**index
            index += 1
            temp = temp[:-1]
        result = [] 
        while base10 > 0:         #再转成b进制
            base10, index = divmod(base10, b)
            result.append(baseChar[index])
        print("".join(result[::-1]))
    except Exception:
        break
编辑于 2018-10-03 00:10:52 回复(0)
#include <stdio.h>
int main()
{
    long a,b;
    char c[40];
    while(scanf("%ld%s%ld",&a,c,&b)!=EOF)
    {
        int x,sum=0,d=1;
        for(int i=0;c[i]!=0;++i){
            if(c[i]>='0'&&c[i]<='9')
                x=c[i]-'0';
            else if(c[i]>='a'&&c[i]<='f')
                x=c[i]-'a'+10;
            else if(c[i]>='A'&&c[i]<='F')
                x=c[i]-'A'+10;
            sum=a*sum+x;
        }
        char ans[40];
        int size=0;
        do{
            int x=sum%b;
            ans[size++]=(x>=10)?x-10+'A':x+'0';
            sum/=b;
        }while(sum);
        for(int i=size-1;i>=0;--i)
            printf("%c",ans[i]);
        printf("\n");
    }
    return 0;
}

运行时间:3ms

占用内存:384k

发表于 2018-01-17 22:14:14 回复(0)

问题信息

难度:
115条回答 9762浏览

热门推荐

通过挑战的用户

查看代码