Tuesday 17 July 2012

UVa 389 - Basically Speaking Solution

#include<iostream>
#include<list>
#include<string>
#include<cstring>
#include<sstream>
#include<cctype>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<fstream>
#include<cstdlib>
#include<vector>
#include<map>
#include<set>
#include<utility>
#include<iomanip>
#include<queue>

using namespace std;

#define INF (1<<29)
#define SET(a) memset(a,-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define CLR(a) memset(a,0,sizeof(a))
#define FILL(a,v) memset(a,v,sizeof(a))
#define PB push_back
#define FOR(i,n) for(int i = 0;i<n;i++)
#define PI acos(-1.0)
#define EPS 1e-9
#define MP(a,b) make_pair(a,b)
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define LL long long
#define MX 100000
#define MOD 1000000007

long long to_decimal(string s,int base)
{
    int cnt=0;
    long long res=0;
    for(int i=s.length()-1;i>=0;i--)
    {
        if ( s[i] > 47 && s[i] < 58 )
        res+= (pow(base,cnt)*(s[i]-'0'));
        else
        res+= (pow(base,cnt)*(s[i]-55));
        cnt++;
    }
    return res;
}

string dec_to(long long num, int base)
{
    string s="";
    while(num)
    {
        int tmp=num%base;
        if(tmp<10)
            s+=tmp+'0';
        else
            s+= char (tmp+55);
        num/=base;
    }
    if(s=="")
    return "0";
    return s;
}

int main()
{
   string s,sr,res;
   int from, to;
   while(cin>>s>>from>>to)
   {
       long long buf;
       buf=to_decimal(s,from);
       sr=dec_to(buf,to);
       res="";
       for(int i=sr.length()-1;i>=0;i--)
            res+=sr[i];
       if(res.length()>7)
            cout<<"  ERROR"<<endl;
       else
            cout<<setw(7)<<res<<endl;
       //cout<<setw(7)<<dec_to(buf,to)<<endl;
   }
return 0;
}

No comments:

Post a Comment