题目链接
大数求和
class Solution {
public:
string addStrings(string num1, string num2) {
int cf = 0;//进位
int temp;
string str;
int n;
int len_num1 = num1.size();
int len_num2 = num2.size();
int len = abs(len_num1 - len_num2);
//长度对齐
if(len_num1 < len_num2)
{
for(int i=0;i<len;i++)
{
num1 = "0"+num1;
}
}
else
{
for(int i=0;i<len;i++)
{
num2 = "0"+num2;
}
}
len_num1 = num1.size();
//字符串相加
for(int i=len_num1-1;i>=0;i--)
{
n = num1[i] - '0'+ num2[i] - '0' + cf;
cf = n/10;
temp = n%10;
str = to_string(temp) + str;
}
if(cf != 0)
str = to_string(cf)+str;
return str;
}
};