• 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..

    // 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 '';
    }
    
    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 << "	 Curr Digit: " << origVal[i] << endl;
                int quo =  origVal[i] / s;
                int rem =  origVal[i] % s;            
    //            cout << "	 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 << "	Adding 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;
    }
    View Code
  • 相关阅读:
    unity assert server 与 cache server
    Excel文件读写
    String与StringBuilder之间区别(转)
    c# 文件遍历
    C#整数三种强制类型转换int、Convert.ToInt32()、int.Parse()的区别
    2014年读过的书总结
    求职在年末
    被辞退于年末
    Unity Svn(转)
    公司的人员流动
  • 原文地址:https://www.cnblogs.com/tonix/p/3538752.html
Copyright © 2020-2023  润新知