Wednesday, November 16, 2016

Uva 355 - The Bases are Loaded

/*
                                                      CODEMAN
                                              ARIF KHAN NIHAR
                                       uva 355 - The Bases are Loaded

*/
#include<bits/stdc++.h>
using namespace std;

#define sf scanf
#define pf printf

char N[] = "0123456789ABCDEF";
int main()
{
   long long int d,n1,n,i,j,l;
   int b1,b2;
    string re = "";
    char s[101];
    while( sf("%d %d %s",&b1,&b2,&s) == 3) {
        string rt = s;
    if(rt != "0"){
    n = strlen(s);
      n1 =  n - 1, l = d = 0;
      for(i = 0 ; n1 >= 0 ; i++ , n1-- ) {
        if( !isalpha(s[i])  &&  (((s[i]-48) >=  0)  &&  (( s[i] - 48)  <  b1)) ) {
        l += ((s[i]-'0') * (powl(b1,n1))) ;
        }
        else if(isalpha(s[i]) &&  ((( s[i] - 65 + 10) >=  0)  && ( s[i] - 65 + 10 ) < b1)) {
        l += (10 + (s[i] - 65)) * powl(b1,n1) ;
        }else{
            l = 0;
            break;
        }
       }
      if( l ){
      while( l )
        re += (N[l%b2]) , l /= b2 ;
    }
}else{
    re +=  "0";
}
    if( !re.empty() ){
      reverse(re.begin(),re.end());
      cout << s << " base " << b1 << " = " << re << " base " << b2 << endl;
    }else{
        cout << s << " is an illegal base " << b1 << " number" << endl;
    }

      re.clear();
    }

    return 0;
}


No comments: