1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
#include <iostream> #include <string> #include <stdio.h> #include <sstream> using namespace std; int fBase,tBase; string str,str1; char ch[7]; int convert() { unsigned long long d; int k=7; unsigned long long r=0; unsigned long long _pow=1; if(str[str.size()-1]>'9') { d=str[str.size()-1]-55; } else d=str[str.size()-1]-'0'; //cout<<_pow<<" "<<d<<"\n"; r+=d; for(int i=str.size()-2;i>=0;i--) { _pow*=fBase; if(str[i]>'9') { d=str[i]-55; } else d=str[i]-'0'; //cout<<_pow<<" "<<d<<"\n"; r+=(d*_pow); } //cout<<"r = "<<r<<"\n"; k=7; if(r==0) { k--; ch[k]='0'; } while(r) { k--; if((r%tBase)>9) ch[k]=(((r%tBase)+55)+'\0'); else ch[k]=((r%tBase)+'0'); r/=tBase; if(k==0) { if(r) return -1; else break; } } for(int i=k-1;i>=0;i--) ch[i]=' '; return 0; } int main() { while(cin>>str) { cin>>fBase>>tBase; str1=""; //cout<<str<<"\n"; if(convert()==-1) cout<<" ERROR\n"; else { printf("%s\n",ch); } } return 0; } |