Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 2470 | Accepted: 1055 |
Description
Input
Output
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
/* 进制之间的任意转换 其实就是循环做除法,每趟循环除法得到的余数作为结果的当前最高位 */ #include <iostream> #include <string> using namespace std; string input, ouput; char index[62] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; int getV(char c) { if(c >= '0' && c <= '9') return c - '0'; else if(c >= 'A' && c <= 'Z') return c - 'A' + 10; else return c - 'a' + 36; } string change(int f, int t, string input) { string res = ""; int last = 0, quo, cur, residual, pos = 0; string quotient; while(input.length() != 0) { last = 0; cur = 0; for(pos = 0; pos < input.length(); pos++) { if((cur = last * f + getV(input[pos])) < t) { quotient = quotient + '0'; last = cur; continue; } else { quo = cur / t; quotient = quotient + index[quo]; last = cur % t; } } res = index[last] + res; while(quotient.length() >= 1 && quotient[0] == '0') quotient = quotient.substr(1, quotient.length() - 1); input = quotient; quotient = ""; } return res; } int main() { int caseNum, c, from, to; cin>>caseNum; for(c = 1; c <= caseNum; c++) { cin>>from>>to; cin>>input; cout<<from<<" "<<input<<endl; cout<<to<<" "<<change(from, to, input)<<endl<<endl; } return 0; }