SPOJ #429 Simple Numbers Conversion

时间:2023-03-09 07:56:27
SPOJ #429 Simple Numbers Conversion

This is simply a human work simulation - exactly reproducing how you do it by hand. Nothing special. You'd better put each step on a paper to make everything clear. Reference: http://blog.csdn.net/rappy/article/details/1737671

My code passed all 6 test cases. Yay..

//

#include <iostream>
#include <cstring>
#include <vector>
#include <cmath>
using namespace std; char GetDigitChar(int val)
{
if(val >= && val <= ) return val + '';
else if(val >= && val < ) return val - + 'A';
return '\0';
} int GetDigitVal(char c)
{
c = toupper(c);
if( c >= '' && c <= '') return c - '';
else if(c >= 'A' && c <= 'Z') return c - 'A' + ;
return ;
} void num_conv(string &str, int r, int s)
{
int strLen = str.length(); // Convert string to val first
vector<int> origVal;
origVal.reserve(strLen);
for(int i = ; i < strLen; i ++)
{
origVal.push_back(GetDigitVal(str[i]));
} // Go
vector<char> ret; int currStartInx = ;
bool bAllDone = false;
while(currStartInx < strLen && !bAllDone)
{
// cout << "Start from " << currStartInx << endl;
for(int i = currStartInx; i < strLen;i++)
{
// cout << "\t Curr Digit: " << origVal[i] << endl;
int quo = origVal[i] / s;
int rem = origVal[i] % s;
// cout << "\t Quo: " << quo << " Rem: " << rem << endl; origVal[i] = quo;
// The digit to record
if(i == strLen - )
{
ret.push_back(GetDigitChar(rem));
// cout << "!" << GetDigitChar(rem) << endl;
bAllDone = (currStartInx == (strLen - ) && quo == )? true: false;
break;
} // Add remainer to next digit
if(rem > )
{
// cout << "\tAdding rem " << r * rem << " = ";
origVal[i+] += r * rem;
// cout << origVal[i+1] << endl;
} if(i == currStartInx)
{
currStartInx += quo == ? : ;
}
}// for
}// while // Output
for(int i = ret.size() - ; i >=; i --)
{
cout << ret[i];
}
cout << endl;
} int main()
{
int runcnt = ;
cin >> runcnt;
while(runcnt--)
{
string strnum;
int r, s;
cin >> strnum >> r >> s;
num_conv(strnum, r, s);
}
return ;
}