zoukankan      html  css  js  c++  java
  • hdu-1877(大数+进制转换)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1877

    思路:注意考虑0,0的情况。

    #include<iostream>
    #include<cstdio>
    #include<string>
    #include<algorithm>
    using namespace std;
    int m,a,b;
    string s1,s2;
    string add(string s1,string s2)
    {
        if(s1.length()<s2.length()) 
        {
            string tp=s1;s1=s2;s2=tp;
        }
        int i,j,l1=s1.length(),l2=s2.length();
        for(i=l1-1,j=l2-1;i>=0;i--,j--)
        {
            s1[i]=(char)(s1[i]+(j>=0?s2[j]-'0':0));
            if(s1[i]-'0'>=m)
            {
                s1[i]=(char)((s1[i]-'0')%m+'0');
                if(i) s1[i-1]++;
                else s1="1"+s1;
            }
        }
        return s1;
    }
    string f(int x)
    {
        string s2="";
        int i,j,tp;
        while(x)
        {
            tp=x%m;
            x/=m;
            s2+=(char)(tp+'0');
        }
        reverse(s2.begin(),s2.end());
        //cout<<s2<<endl;
        return s2;
    }
    int main(void)
    {
        while(cin>>m&&m)
        {
            cin>>a>>b;
            if(a+b==0)
            {
                cout<<0<<endl;continue;
            }
            s1=f(a);
            s2=f(b);
            cout<<add(s1,s2)<<endl;
        }
        return 0;
    }
  • 相关阅读:
    MathML
    Redux counterpart rematch dva
    flow
    pauseable 库
    a simple machine learning system demo, for ML study.
    react图工具集成
    SQLite
    Celery
    RabbitMQ installation
    Thunk
  • 原文地址:https://www.cnblogs.com/2018zxy/p/9824680.html
Copyright © 2011-2022 走看看