高精度进位制转换,Poj(1220)
转自ACdream。
#include <stdio.h> #include <string.h> #include <stdlib.h> #define MAXSIZE 60000 char in[MAXSIZE]; int oldbase,newbase; int a[MAXSIZE],b[MAXSIZE],r[MAXSIZE]; 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 chToNum() { a[0] = strlen(in); for(int i=1;i<=a[0];i++) { a[i] = getNum(in[i-1]); } } void alter() { while(a[0] >= 1) { int i,t = 0; for(i=1;i<=a[0];i++) { t = t * oldbase + a[i]; b[ ++b[0] ] = t / newbase; t %= newbase; } r[ ++r[0] ] = t; b[0] = a[0]; for(i = 1; i <= b[0] && !b[i]; i++) ; for(a[0] = 0; i <= b[0]; i++) a[ ++a[0] ] = b[i]; memset(b,0,sizeof(b)); } } void print() { printf("%d %s\n%d ",oldbase,in,newbase); while(r[0]>=1) { printf("%c",getChar(r[ r[0]-- ])); } printf("\n\n"); } int main() { int n; while(scanf("%d",&n)!=EOF) { while(n--) { scanf("%d %d %s",&oldbase,&newbase,in); chToNum(); alter(); print(); } } return 0; }