鱼C论坛

 找回密码
 立即注册
查看: 2090|回复: 1

[技术交流] 001:NUMBER BASE CONVERSION

[复制链接]
发表于 2018-2-4 14:35:14 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 Messj 于 2018-6-8 23:33 编辑

总时间限制: 1000ms 内存限制: 65536kB

描述
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
  2. 62 2 abcdefghiz
  3. 10 16 1234567890123456789012345678901234567890
  4. 16 35 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2
  5. 35 23 333YMHOUE8JPLT7OX6K9FYCQ8A
  6. 23 49 946B9AA02MI37E3D3MMJ4G7BL2F05
  7. 49 61 1VbDkSIMJL3JjRgAdlUfcaWj
  8. 61 5 dl9MDSWqwHjDnToKcsWE1S
  9. 5 10 42104444441001414401221302402201233340311104212022133030
复制代码


样例输出
  1. 62 abcdefghiz
  2. 2 11011100000100010111110010010110011111001001100011010010001

  3. 10 1234567890123456789012345678901234567890
  4. 16 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2

  5. 16 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2
  6. 35 333YMHOUE8JPLT7OX6K9FYCQ8A

  7. 35 333YMHOUE8JPLT7OX6K9FYCQ8A
  8. 23 946B9AA02MI37E3D3MMJ4G7BL2F05

  9. 23 946B9AA02MI37E3D3MMJ4G7BL2F05
  10. 49 1VbDkSIMJL3JjRgAdlUfcaWj

  11. 49 1VbDkSIMJL3JjRgAdlUfcaWj
  12. 61 dl9MDSWqwHjDnToKcsWE1S

  13. 61 dl9MDSWqwHjDnToKcsWE1S
  14. 5 42104444441001414401221302402201233340311104212022133030

  15. 5 42104444441001414401221302402201233340311104212022133030
  16. 10 1234567890123456789012345678901234567890
复制代码

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-2-4 15:05:15 | 显示全部楼层
  1. #include<cstdio>
  2. #include<iostream>
  3. #include<cstring>

  4. const int maxn=1000;
  5. using namespace std;

  6. int n,m;
  7. char str1[maxn],str2[maxn];
  8. int t[maxn],A[maxn];

  9. void solve(char *str1,char *str2,int n,int m)
  10. {
  11.         int len,i,k;
  12.         len = strlen(str1);
  13.         for(i=len;i>=0;i--)
  14.         {
  15.                 t[len-1-i]=str1[i]-(str1[i]<58?48:str1[i]<91?55:61);    //ascii '0'-48 'A'-65 'a'-97
  16.         }
  17.         for(i=0;len;)
  18.         {
  19.                 for(i=len;i>0;i--)
  20.                 {
  21.                         t[i-1]=t[i]%m*n;
  22.                         t[i]/=m;
  23.                 }
  24.                 A[k++]=t[0]%m;
  25.                 t[0]/=m;
  26.                 while(len>0&&!t[len-1])
  27.                 {
  28.                         len--;
  29.                 }
  30.         }
  31.         str2[k]=NULL;
  32.         for(i=0;i<k;i++)
  33.         {
  34.                 str2[k-1-i] = A[i]+(A[i]<10?48:A[i]<36?55:61);
  35.         }
  36. }
  37. int main()
  38. {
  39.         int i,T;
  40.         cin>>T;
  41.         for(i=0;i<T;i++)
  42.         {
  43.                 cin>>n>>m>>str1;
  44.                 solve(str1,str2,n,m);
  45.                 cout<<n<<" "<<str1<<endl;
  46.                 cout<<m<<" "<<str2<<endl;
  47.                 if(i!=T-1)
  48.                         cout<<endl;
  49.         }
  50. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-7 13:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表