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..
// 429 #include <iostream> #include <cstring> #include <vector> #include <cmath> using namespace std; char GetDigitChar(int val) { if(val >= 0 && val <= 9) return val + '0'; else if(val >= 10 && val < 36) return val - 10 + 'A'; return '\0'; } int GetDigitVal(char c) { c = toupper(c); if( c >= '0' && c <= '9') return c - '0'; else if(c >= 'A' && c <= 'Z') return c - 'A' + 10; return 0; } 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 = 0; i < strLen; i ++) { origVal.push_back(GetDigitVal(str[i])); } // Go vector<char> ret; int currStartInx = 0; 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 - 1) { ret.push_back(GetDigitChar(rem)); // cout << "!" << GetDigitChar(rem) << endl; bAllDone = (currStartInx == (strLen - 1) && quo == 0 )? true: false; break; } // Add remainer to next digit if(rem > 0) { // cout << "\tAdding rem " << r * rem << " = "; origVal[i+1] += r * rem; // cout << origVal[i+1] << endl; } if(i == currStartInx) { currStartInx += quo == 0? 1 : 0; } }// for }// while // Output for(int i = ret.size() - 1; i >=0; i --) { cout << ret[i]; } cout << endl; } int main() { int runcnt = 0; cin >> runcnt; while(runcnt--) { string strnum; int r, s; cin >> strnum >> r >> s; num_conv(strnum, r, s); } return 0; }

浙公网安备 33010602011771号