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.
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
Source
题目类型:高精度进制转换
算法分析:直接使用大整数除法模拟实现即可,详细可以参考高精度进制转换用法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
#include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; const int INF = 0x7FFFFFFF; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int MOD = 10000007; const int maxn = 100000 + 66; char str[maxn];//输入字符串 int start[maxn], ans[maxn], res[maxn];//被除数,商,余数 int oldBase, newBase;//转换前后的进制 int GetNum (char c)//这里进制字符是先数字,后大写字母,后小写字母的 { if(c >= '0' && c <= '9') return c - '0';//数字 if(c >= 'A' && c <= 'Z') return c - 'A' + 10;//大写字母 return c - 'a' + 36;//小写字母 } //数字得到字符 char GetChar (int i) { if (i >= 0 && i <= 9) return i + '0'; if (i >= 10 && i <= 35) return i - 10 + 'A'; return i - 36 + 'a'; } void Change ()//把输入的字符串的各个数位还原为数字形式 { int i; start[0] = strlen (str);//数组的0位存的是数组长度 for(i = 1; i <= start[0]; i++) start[i] = GetNum (str[i-1]); } void Solve () { memset (res, 0, sizeof (res));//余数初始化为空 int y, i, j; //模n取余法,(总体规律是先余为低位,后余为高位) while (start[0] >= 1) {//只要被除数仍然大于等于1,那就继续“模2取余” y = 0; i = 1; ans[0] = start[0]; // while (i <= start[0]) { y = y * oldBase + start[i]; ans[i++] = y / newBase; y %= newBase; } res[++res[0]] = y;//这一轮运算得到的余数 i = 1; //找到下一轮商的起始处 while ((i <= ans[0]) && (ans[i] == 0)) i++; //清除这一轮使用的被除数 memset (start, 0, sizeof(start)); //本轮得到的商变为下一轮的被除数 for(j = i;j <= ans[0]; j++) start[++start[0]] = ans[j]; memset (ans, 0, sizeof(ans)); //清除这一轮的商,为下一轮运算做准备 } } void Output ()//从高位到低位逆序输出 { int i; for (i = res[0]; i >= 1; i--) printf ("%c", GetChar (res[i])); printf ("\n"); } int main() { // ifstream cin ("aaa.txt"); int t; cin >> t; while (t--) { cin >> oldBase >> newBase >> str; cout << oldBase << " " << str << endl; Change (); Solve (); cout << newBase << " "; Output (); cout << endl; } return 0; } |
- « 上一篇:poj1218
- poj1251:下一篇 »