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;
}

 

转载于:https://www.cnblogs.com/2018zxy/p/9824680.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值