?
查看原帖
?
613957
M_I_H_O_Y_O楼主2023/4/7 16:52
#include <iostream>
#include <cstring>
using namespace std;
string a,b,S;
string f(string a){
	string b;
	for(int i=a.length()-1;i>=0;i--){
		b+=a[i];
	}
	return b;
}
string add(string a,string b){
	int c=0;
	string s="";
	/*a=f(a);
	b=f(b);
	for(int i=0;i<max(a.length(),b.length());i++){
		int temp,temp1=a[i]-'0',temp2=b[i]-'0';
		temp=(temp1+temp2+c)%10+'0';
		c=(temp1+temp2+c)/10;
		s+=temp;
		//cout<<temp1<<' '<<temp2<<' ';
	}
	if(c)s+=c+'0';
	//cout<<c<<endl;
	s=f(s);*/
	if(a.length()<b.length()){
		for(int i=0;i<b.length()-a.length();i++){
			a='0'+a;
		}
	}else{
		for(int i=0;i<a.length()-b.length();i++){
			b='0'+b;
		}
	}
	for(int i=max(a.length(),b.length())-1;i>=0;i--){
		int temp,temp1=a[i]-'0',temp2=b[i]-'0';
		temp=(temp1+temp2+c)%10;
		c=(temp1+temp2+c)/10;
		s=char(temp+'0')+s;
	}
	if(c)s=char(c+'0')+s;
	return s;
}
int main(){
	cin>>a>>b;
	S=add(a,b);
	cout<<S;
	return 0;
} 
2023/4/7 16:52
加载中...