列表

详情


NC50957. NUMBER BASE CONVERSION

描述

Write a program to convert numbers in one base to numbers in a second base. There are 62 different digits: 
{ 0-9,A-Z,a-z } 
HINT: If you make a sequence of base conversions using the output of one conversion as the input to the next, when you get back to the original base, you should get the original number. 

输入描述

The first line of input contains a single positive integer. This is the number of lines that follow. Each of the following lines will have a (decimal) input base followed by a (decimal) output base followed by a number expressed in the input base. Both the input base and the output base will be in the range from 2 to 62. That is (in decimal) A = 10, B = 11, ..., Z = 35, a = 36, b = 37, ..., z = 61 (0-9 have their usual meanings).

输出描述

The output of the program should consist of three lines of output for each base conversion performed.

The first line should be the input base in decimal followed by a space then the input number (as given expressed in the input base).

The second output line should be the output base followed by a space then the input number (as expressed in the output base).

The third output line is blank.

示例1

输入:

8
62 2 abcdefghiz
10 16 1234567890123456789012345678901234567890
16 35 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2
35 23 333YMHOUE8JPLT7OX6K9FYCQ8A
23 49 946B9AA02MI37E3D3MMJ4G7BL2F05
49 61 1VbDkSIMJL3JjRgAdlUfcaWj
61 5 dl9MDSWqwHjDnToKcsWE1S
5 10 42104444441001414401221302402201233340311104212022133030

输出:

62 abcdefghiz
2 11011100000100010111110010010110011111001001100011010010001

10 1234567890123456789012345678901234567890
16 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2

16 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2
35 333YMHOUE8JPLT7OX6K9FYCQ8A

35 333YMHOUE8JPLT7OX6K9FYCQ8A
23 946B9AA02MI37E3D3MMJ4G7BL2F05

23 946B9AA02MI37E3D3MMJ4G7BL2F05
49 1VbDkSIMJL3JjRgAdlUfcaWj

49 1VbDkSIMJL3JjRgAdlUfcaWj
61 dl9MDSWqwHjDnToKcsWE1S

61 dl9MDSWqwHjDnToKcsWE1S
5 42104444441001414401221302402201233340311104212022133030

5 42104444441001414401221302402201233340311104212022133030
10 1234567890123456789012345678901234567890

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++(g++ 7.5.0) 解法, 执行用时: 4ms, 内存消耗: 324K, 提交时间: 2022-12-07 20:18:47

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 1006;
int f[200], ff[100];
char s[N], ans[N];

void NUMBER_BASE_CONVERSION() {
	int a, b;
	cin >> a >> b;
	scanf("%s", s);
	cout << a << " " << s << endl << b << " ";
	int len = strlen(s), t = strlen(s), i;
	for (i = 0; t; i++) {
		int k = 0;
		for (int j = len - t; j < len; j++) {
			k = k * a + f[(int)s[j]];
			s[j] = ff[k/b];
			k %= b;
		}
		ans[i] = ff[k];
		while (t > 0 && s[len-t] == '0') t--;
	}
	while (--i >= 0) cout << ans[i];
	cout << endl << endl;
}

int main() {
	int t = 0;
	for (int i = '0'; i <= '9'; i++) {
		f[i] = t;
		ff[t++] = i;
	}
	for (int i = 'A'; i <= 'Z'; i++) {
		f[i] = t;
		ff[t++] = i;
	}
	for (int i = 'a'; i <= 'z'; i++) {
		f[i] = t;
		ff[t++] = i;
	}
	cin >> t;
	while (t--) NUMBER_BASE_CONVERSION();
	return 0;
}

pypy3 解法, 执行用时: 48ms, 内存消耗: 29880K, 提交时间: 2021-06-25 10:59:55

def Get_num(s):
    if s >= 'a' and s <= 'z':
        return ord(s) - ord('a') + 36
    elif s >= 'A' and s <= 'Z':
        return ord(s) - ord('A') + 10
    else:
        return ord(s) - ord('0')
R_number = []
for i in range(10):
    R_number.append(str(i))
for i in range(ord('A'),ord('Z')):
    R_number.append(chr(i))
R_number.append('Z')
for i in range(ord('a'),ord('z')):
    R_number.append(chr(i))
R_number.append('z')

def R_num(x):
    return R_number[x]
n = int(input())
while n:
    n -= 1
    a,b,s = input().split()
    a = int(a)
    b = int(b)
    num = Get_num(s[0])
    #转化为10进制
    for i in s[1:]:
        num *= a
        x = Get_num(i)
        num += x
    ans = ""
    while(num):
        ans += R_num(num%b)
        num //= b
    if(s == "0"): ans = "0"
    print(a,s)
    print(b,ans[::-1])
    print("")
exit(0)

C++14(g++5.4) 解法, 执行用时: 4ms, 内存消耗: 356K, 提交时间: 2020-05-18 17:30:23

#include<bits/stdc++.h>
using namespace std;

int n,t,a,b;
int main()
{
	cin>>t;
	while(t--)
	{
	    string x,y;
		cin>>a>>b>>x;
		vector<int>num;
		for(auto i:x)
		{
			if(i>='0'&&i<='9')num.push_back(i-'0');
			else if(i>='A'&&i<='Z')num.push_back(i-'A'+10);
			else num.push_back(i-'a'+36); 
		}
		reverse(num.begin(),num.end());
		vector<int> res;
		while(num.size())
		{
			int r=0;
			for(int i=num.size()-1;i>=0;i--)
			{
				num[i]+=r*a;
				r=num[i]%b;
				num[i]/=b;
				
			}
			res.push_back(r);
			while(num.size()&&num.back()==0)num.pop_back();
		}
		reverse(res.begin(),res.end());
		for(auto x:res)
		{
			if(x>=0&&x<=9)y+=char(x+'0');
			else if(x>=10&&x<=35)y+=char(x-10+'A');
			else y+=char(x-36+'a');
		}
		cout<<a<<" "<<x<<endl;
		cout<<b<<" "<<y<<endl;
		cout<<endl;
	}
}

Python3 解法, 执行用时: 41ms, 内存消耗: 7076K, 提交时间: 2021-10-14 21:06:35

def change(s):
    if s<='9':
        return ord(s)-48
    elif s<='Z': 
        return ord(s)-55
    else:
        return ord(s)-61
def to(a):
    if a<=9:
        return chr(a+48)
    elif a<=35:
        return chr(a+55)
    else:
        return chr(a+61)
t=int(input())
for i in range(0,t):
    a,b,stra=input().split(" ")
    print(a+" "+stra)
    a=int(a)
    b=int(b)
    ans=0
    for j in stra:
        ans=ans*a+int(change(j))
    strb=""
    while ans>0:
        strb+=to(ans%b);
        ans=ans//b
    strb=strb[::-1]
    if strb=="":
        strb="0"
    b=str(b)
    print(b+" "+strb+"\n")

C++ 解法, 执行用时: 3ms, 内存消耗: 412K, 提交时间: 2021-11-23 20:52:17

#include<bits/stdc++.h>
using namespace std;
int n,x,y,i,l,k,t[1001],b[1001];
char s[1001],ans[1001];
int main(){
    cin>>n;
    while(n--){
      	cin>>x>>y>>s;
        for(i=k=strlen(s);0<i--;)t[k-1-i]=s[i]-(s[i]<58?48:s[i]<97?55:61);
        for(l=0;k;){
			for(i=k;1<i--;)t[i-1]+=t[i]%y*x,t[i]/=y;   
            b[l++]=t[0]%y;
            t[0]/=y;
            for(;0<k&&!t[k-1];k--);
        }
        for(ans[l]=i=0;i<l;i++)ans[l-1-i]=b[i]+(b[i]<10?48:b[i]<36?55:61);
        printf("%d %s\n%d %s\n\n",x,s,y,ans);
    }
    return 0;
}

上一题