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.
{ 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.
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这道题,怎么说呢,如果懂得如何转换进制的话其实是不难的,水一些高精度就能过了。
其实就是将一个十分大的数去除以要转话的进制数,余数作为解的每个位数,商作为下一次的计算的数。然后一直除,直到商为0为止,然后将前面获得的每一个余数倒序输出就是结果,但是要注意0的特例。(以后可以尝试先处理掉特例,不然错得不明不白)
挺water 的一道题
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <sstream>
#define INF 1e9
using namespace std;
FILE *fp;
typedef long long ll;
int b,p,ss[100005],q,k,ans[100005];
char s[100005],cans[100005];
void turn()
{
int i = 0;
while (i<=strlen(s))
{
if(s[i]>='A'&&s[i]<='Z')
{
ss[i]=s[i]-'A'+10;
}
else if(s[i]>='a'&&s[i]<='z')
{
ss[i]=s[i]-'a'+36;
}
else
{
ss[i]=s[i]-'0';
}
i++;
}
}//将获得的字符串转化为数组储存
int turn2()
{
for (int i = 0;i < k;i++)
{
if(ans[i]>=0&&ans[i]<=9)
{
cans[i]=ans[i]+'0';
}
else if(ans[i]>=10&&ans[i]<=35)
{
cans[i]=ans[i]-10+'A';
}
else if(ans[i]>=36&&ans[i]<=61)
{
cans[i]=ans[i]-36+'a';
}
}
}//将计算得到的余数转化为字符串储存
int main(void)
{
int t;
scanf("%d",&t);
while (t--)
{
scanf("%d %d",&b,&p);
scanf("%s",s);
turn();
cout<<b<<" "<<s<<endl;
cout<<p<<" ";
int bis = 0;
for (int i = 0;i < strlen(s);i++)
{
if(ss[i]!=0)
{
bis = 1;
break;
}
}
if(!bis)
{
cout<<0<<endl<<endl;
continue;
}//判0
k = 0;
while (1)
{
int flag = 0;
for ( q = 0;q < strlen(s);q++)
{
if(ss[q]!=0)
{
flag = 1;
break;
}
}
if(!flag)
{
break;
}
int i;
for (i = q;i+1 < strlen(s);i++)
{
ss[i+1]+=(ss[i]%p)*b;
ss[i] = ss[i]/p;
//cout<<ss[i]<<" ";
}
ans[k++]=ss[i]%p;
ss[i]=ss[i]/p;
}
turn2();
/*cout<<b<<" "<<s<<endl;
cout<<p<<" ";*/
int j;
for (j = k-1;j >= 0;j--)
{
if(ans[j]!=0)
{
break;
}
}
for (int i = j;i >= 0;i--)
{
cout<<cans[i];
}
cout<<endl<<endl;
}
return 0;
}