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 87 88 89 |
#include <iostream> #include <string> #include <cstring> #include <string.h> using namespace std; unsigned long long decOf(string s,int a) { unsigned long long d; unsigned long long _pow=1; unsigned long long r=0; if((s[s.size()-1]-'0')>=0 && (s[s.size()-1]-'0')<=9) d=(s[s.size()-1]-'0'); else d=(s[s.size()-1]-55); if(d>=a) { return -1; } r+=(d*1); for(int i=s.size()-2;i>=0;i--) { _pow*=a; if((s[i]-'0')>=0 && (s[i]-'0')<=9) d=(s[i]-'0'); else d=(s[i]-55); if(d>=a) { return -1; } r+=(d*_pow); } return r; } int main() { string str,str1; unsigned long long dec,dec1; int m,n; //cout<<'Z'-55<<"\n"; //cout<<decOf("Z",36)<<"\n"; //cout<<decOf("35",10)<<"\n"; while(cin>>str) { cin>>str1; m=0; n=0; for(int i=2;i<=36;i++) { dec=decOf(str,i); if(dec==-1) continue; for(int j=2;j<=36;j++) { dec1 = decOf(str1,j); //cout<<dec << " "<<dec1<<" = "<<i<<" "<<j<<"\n"; if(dec1==-1) continue; if(dec==dec1 && dec!=-1) { m=i; n=j; //cout<<m<<" "<<n<<"\n"; break; } } if(m!=0 && n!=0) { //cout<<"enter\n"; break; } } //cout<<m<<" "<<n<<" ds\n"; if(m!=0 && n!=0) { cout<<str<<" (base "<<m<<") = "<<str1<<" (base "<<n<<")\n"; } else cout<<str<<" is not equal to "<<str1<<" in any base 2..36\n"; } return 0; } |