zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    promethus监控JVM jar包
    ubuntu中文乱码解决办法
    IT焦躁中的赤子青年
    ftp neo4j http kafka搭建
    查看python脚本执行过程
    解决coredns-7f9c544f75-2sjs2一直处于ContainerCreating
    番茄工作法
    数据库的性能优化
    MyBatis
    CentOS下安装JDK,Tomcat,Redis,Mysql,及项目发布
  • 原文地址:https://www.cnblogs.com/tonix/p/3538752.html
Copyright © 2011-2022 走看看