Letters比赛第六场1001 Basically Speaking解题报告
1001 Basically Speaking(POJ 1546)
基本思路:基本的进制转换,先转化成十进制再转化成目标进制即可。
代码如下:
1 #include <cstdlib> 2 #include <iostream> 3 #include <cmath> 4 5 using namespace std; 6 7 int a = 0; 8 int b = 0; 9 char aNum[10]; 10 char bNum[10]; 11 12 void solve() 13 { 14 int i = 7; 15 int j = 0; 16 int ans = 0; 17 int alen = strlen(aNum); 18 19 for(j=0; j<alen; j++) 20 { 21 if(aNum[j] >= 'A') 22 { 23 aNum[j] = aNum[j] - 'A' + 10; 24 } 25 else 26 { 27 aNum[j] = aNum[j] - '0'; 28 } 29 } 30 31 for(i=0; i<alen; i++) 32 { 33 ans = a * ans + aNum[i]; 34 } 35 36 if(ans >= (int)pow((double)b, 7.0)) 37 { 38 printf(" ERROR\n"); 39 } 40 else 41 { 42 for(j=0; ans && j<7; j++) 43 { 44 bNum[j] = ans%b; 45 ans = ans/b; 46 } 47 i = 7-j; 48 while(i--) 49 { 50 printf(" "); 51 } 52 for(i=j-1; i>=0; i--) 53 { 54 printf("%0X", bNum[i]); 55 } 56 printf("\n"); 57 } 58 } 59 60 int main() 61 { 62 #ifdef MYLOCAL 63 freopen("input.txt", "r", stdin); 64 freopen("output.txt", "w", stdout); 65 #endif 66 67 while(scanf("%s %d %d", aNum, &a, &b) != EOF) 68 { 69 solve(); 70 } 71 72 return 0; 73 }