UVA 389 - Basically Speaking Solution
UVA 389 - Basically Speaking Solution
#include<iostream>
#include<string>
using namespace std;
string input;
int m,n;
int convertToDes()
{
int i,j,k,len,sum,temp,val,mul;
len=input.length();
char ch;
bool flag=false;
j=0;
sum=0;
mul=1;
for(i=len-1;i>=0;i--)
{
ch=input[i];
if(ch>='0' && ch<='9')
val=ch-48;
else
val=ch-55;
if(flag)
mul=mul*m;
flag=true;
sum=sum+val*mul;
j++;
}
return sum;
}
string convertToBase(int num)
{
string res="";
char ch;
int rem;
while(num>0)
{
rem=num%n;
if(rem>=0 && rem<=9)
ch=rem+48;
else
ch=rem+55;
res.insert(0,1,ch);
num=num/n;
}
return res;
}
string removeTrailZero(string str)
{
string temp;
int i;
for(i=0;i<str.length() && str[i]=='0';i++);
if(i==0)
temp=str;
else
temp=str.substr(i,str.length()-i);
return temp;
}
int main()
{
freopen("input.txt","r",stdin);
string res;
int len;
while(cin>>input>>m>>n)
{
input=removeTrailZero(input);
if(m==n)
{
len=input.length();
if(len<=7)
{
if(len==0)
{
input="0";
len=1;
}
input.insert(0,7-input.length(),' ');
cout<<input<<endl;
}
else
cout<<" ERROR"<<endl;
continue;
}
res=convertToBase(convertToDes());
res=removeTrailZero(res);
len=res.length();
if(len==0)
{
res="0";
len=1;
}
if(len<7)
{
len=7-len;
res.insert(0,len,' ');
}
if(len>7)
cout<<" ERROR"<<endl;
else
cout<<res<<endl;
}
return 0;
}
Comments
Post a Comment