1. 程式人生 > >hdu-1877(大數+進制轉換)

hdu-1877(大數+進制轉換)

ble length names += tin bsp eve a+b 情況

題目鏈接: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; }

hdu-1877(大數+進制轉換)