题目链接: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
时间: 2024-10-15 06:06:20