poj 1220 NUMBER BASE CONVERSION

NUMBER BASE CONVERSION
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 5976   Accepted: 2738

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

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

 

  1 #include<cstdio>
  2 #include<iostream>
  3 #include<cstring>
  4 #include<algorithm>
  5 #include<cmath>
  6 //#include<vector>
  7 //#include<queue>
  8 //#include<set>
  9 #define INF 0x3f3f3f3f
 10 #define N 100005
 11 #define re register
 12 #define Ii inline int
 13 #define Il inline long long
 14 #define Iv inline void
 15 #define Ib inline bool
 16 #define Id inline double
 17 #define ll long long
 18 #define Fill(a,b) memset(a,b,sizeof(a))
 19 #define R(a,b,c) for(register int a=b;a<=c;++a)
 20 #define nR(a,b,c) for(register int a=b;a>=c;--a)
 21 #define Min(a,b) ((a)<(b)?(a):(b))
 22 #define Max(a,b) ((a)>(b)?(a):(b))
 23 #define Cmin(a,b) ((a)=(a)<(b)?(a):(b))
 24 #define Cmax(a,b) ((a)=(a)>(b)?(a):(b))
 25 #define D_e(x) printf("\n&__ %d __&\n",x)
 26 #define D_e_Line printf("-----------------\n")
 27 #define D_e_Matrix for(re int i=1;i<=n;++i){for(re int j=1;j<=m;++j)printf("%d ",g[i][j]);putchar('\n');}
 28 using namespace std;
 29 //The Code Below Is Bingoyes's Function Forest.
 30 Ii read(){
 31     int s=0,f=1;char c;
 32     for(c=getchar();c>'9'||c<'0';c=getchar())if(c=='-')f=-1;
 33     while(c>='0'&&c<='9')s=s*10+(c^'0'),c=getchar();
 34     return s*f;
 35 }
 36 Iv print(int x){
 37     if(x<0)putchar('-'),x=-x;
 38     if(x>9)print(x/10);
 39     putchar(x%10^'0');
 40 }
 41 /*
 42 Iv Floyd(){
 43     R(k,1,n)
 44         R(i,1,n)
 45             if(i!=k&&dis[i][k]!=INF)
 46                 R(j,1,n)
 47                     if(j!=k&&j!=i&&dis[k][j]!=INF)
 48                         Cmin(dis[i][j],dis[i][k]+dis[k][j]);
 49 }
 50 Iv Dijkstra(int st){
 51     priority_queue<int>q;
 52     R(i,1,n)dis[i]=INF;
 53     dis[st]=0,q.push((nod){st,0});
 54     while(!q.empty()){
 55         int u=q.top().x,w=q.top().w;q.pop();
 56         if(w!=dis[u])continue;
 57         for(re int i=head[u];i;i=e[i].nxt){
 58             int v=e[i].pre;
 59             if(dis[v]>dis[u]+e[i].w)
 60                 dis[v]=dis[u]+e[i].w,q.push((nod){v,dis[v]});
 61         }
 62     }
 63 }
 64 Iv Count_Sort(int arr[]){
 65     int k=0;
 66     R(i,1,n)
 67         ++tot[arr[i]],Cmax(mx,a[i]);
 68     R(j,0,mx)
 69         while(tot[j])
 70             arr[++k]=j,--tot[j];
 71 }
 72 Iv Merge_Sort(int arr[],int left,int right,int &sum){
 73     if(left>=right)return;
 74     int mid=left+right>>1;
 75     Merge_Sort(arr,left,mid,sum),Merge_Sort(arr,mid+1,right,sum);
 76     int i=left,j=mid+1,k=left;
 77     while(i<=mid&&j<=right)
 78         (arr[i]<=arr[j])?
 79             tmp[k++]=arr[i++]:
 80             (tmp[k++]=arr[j++],sum+=mid-i+1);//Sum Is Used To Count The Reverse Alignment
 81     while(i<=mid)tmp[k++]=arr[i++];
 82     while(j<=right)tmp[k++]=arr[j++];
 83     R(i,left,right)arr[i]=tmp[i];
 84 }
 85 Iv Bucket_Sort(int a[],int left,int right){
 86     int mx=0;
 87     R(i,left,right)
 88         Cmax(mx,a[i]),++tot[a[i]];
 89     ++mx;
 90     while(mx--)
 91         while(tot[mx]--)
 92             a[right--]=mx;
 93 }
 94 */
 95 char number[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",str[N],str_new[N];
 96 int main(){
 97     int T=read();
 98     while(T--){
 99         int a=read(),b=read();
100         scanf("%s",str),
101         printf("%d %s\n",a,str);
102         int len=strlen(str),flag=1,digit=0;
103         while(flag){
104             flag=0;
105             int res=0;
106             R(i,0,len-1){
107                 int num;
108                 if(str[i]>='0'&&str[i]<='9')num=str[i]^'0';
109                 if(str[i]>='A'&&str[i]<='Z')num=str[i]-'A'+10;
110                 if(str[i]>='a'&&str[i]<='z')num=str[i]-'a'+36;
111                 num+=res*a,res=num%b,str[i]=number[num/b];
112                 if(str[i]!='0')
113                     flag=1;
114             }
115             str_new[++digit]=number[res];
116         }
117         printf("%d ", b);
118         nR(i,digit,1)
119             printf("%c",str_new[i]);
120         printf("\n\n");
121     }
122     return 0;
123 }
124 /*
125     Note:
126         There is always a truth: High precision is the descendants of Satan.
127 */
View Code

 

posted @ 2018-12-27 19:25  邱涵的秘密基地  阅读(354)  评论(0编辑  收藏  举报