NUMBER BASE CONVERSION(进制转化)
2014-07-31 23:25
148 查看
Description
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.
Input
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).
Output
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.
Sample Input
Sample Output
解题思路:
题目大意是给一个a进制的数,让你转化成b进制,给的数会很大。无非就是不断求余,用高精度求固然可以,但十分麻烦。让被除数上的每一位先加上之前的余数乘以进制数再去除以除数得出的最后余数即是求出的余数。比如5进制数1234转化成2进制:第一位是1,1%2 = 1,1 / 2 = 0,则该位变为0,下一位为2,加上之前的余数乘以进制数,即2 + 1 * 5 = 7,7 % 2 = 1,7 / 2 = 3,则第二位变为3,依次类推,求出余数后,对已经改变的该进制数重新进行求余直至为0。
AC代码:
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.
Input
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).
Output
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.
Sample Input
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
Sample Output
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
解题思路:
题目大意是给一个a进制的数,让你转化成b进制,给的数会很大。无非就是不断求余,用高精度求固然可以,但十分麻烦。让被除数上的每一位先加上之前的余数乘以进制数再去除以除数得出的最后余数即是求出的余数。比如5进制数1234转化成2进制:第一位是1,1%2 = 1,1 / 2 = 0,则该位变为0,下一位为2,加上之前的余数乘以进制数,即2 + 1 * 5 = 7,7 % 2 = 1,7 / 2 = 3,则第二位变为3,依次类推,求出余数后,对已经改变的该进制数重新进行求余直至为0。
AC代码:
#include <iostream> #include <cstdio> #include <cstring> using namespace std; const int maxn = 10000; char num[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; int main() { int t, a, b, len; char str[maxn], str_2[maxn]; scanf("%d", &t); while(t--) { int k, res, j; scanf("%d%d", &a, &b); scanf("%s", str); printf("%d %s\n", a, str); len = strlen(str); bool flag = true; int n=10; j = 0; while(flag) { flag = false; res = 0; for(int i = 0; i < len; i++) { if(str[i] >= '0' && str[i] <= '9') k = str[i] - '0'; if(str[i] >= 'A' && str[i] <= 'Z') k = str[i] - 'A' + 10; if(str[i] >= 'a' && str[i] <= 'z') k = str[i] - 'a' + 36; k = res * a + k; // 加上之前的余数乘以进制数 res = k % b; // 取余 str[i] = num[k / b]; if(str[i] != '0') // 判断是否全为0 flag = true; } str_2[j++] = num[res]; // 存储余数 } printf("%d ", b); for(int i = j - 1; i >= 0; i--) // 倒序输出余数 printf("%c", str_2[i]); printf("\n\n"); } }
相关文章推荐
- 1220 NUMBER BASE CONVERSION【高精度进制转化】
- (高精度运算4.7.26)POJ 1220 NUMBER BASE CONVERSION(高精度数的任意进制的转换——方法:ba1----->10进制----->ba2)
- (高精度运算4.7.26)POJ 1220 NUMBER BASE CONVERSION(高精度数的任意进制的转换——方法:ba1----->10进制----->ba2)
- POJ 1220-NUMBER BASE CONVERSION 任意进制数转换
- 11.Excel Sheet Column Number(26进制转化为10进制)
- poj 1220 NUMBER BASE CONVERSION(字符串处理经典)
- poj 1220 NUMBER BASE CONVERSION
- NUMBER BASE CONVERSION(进制转换)
- NUMBER BASE CONVERSION(高精度)
- 高精度进制转换 poj 1220 NUMBER BASE CONVERSION
- POJ 1220 NUMBER BASE CONVERSION(较复杂的进制转换)
- poj 1220 NUMBER BASE CONVERSION 高精度进制转换
- POJ 1220 Number Base Conversion
- NUMBER BASE CONVERSION POJ - 1220 进制转换/模拟除法
- POJ 1220 NUMBER BASE CONVERSION(高精度--进制转换)
- poj1220 NUMBER BASE CONVERSION
- POJ 1220 NUMBER BASE CONVERSION
- POJ 1220 NUMBER BASE CONVERSION 笔记
- POJ 1220 NUMBER BASE CONVERSION
- leetcode解题之171# Excel Sheet Column Number Java版 (二十六进制转化为十进制)