赞
踩
加法:(字符串模拟小学加法)
string add(string s1, string s2) {
int len1 = s1.length(), len2 = s2.length();
int maxlen = max(len1, len2) + ;
string res(maxlen, '');
int flag = ;
int i = len1 - , j = len2 - , k = maxlen - ;
for (; i >= && j >= ; i--, j--) {
int temp = (s1[i] - '') + (s2[j] - '') + flag;
flag = temp / ;
res[k--]= temp % + '';
}
while (i >= ) {
int temp = s1[i--] - '' + flag;
flag = temp / ;
res[k--] = temp % + '';
}
while (j >= ) {
int temp = s2[j--] - '' + flag;
flag = temp / ;
res[k--] = temp % + '';
}
res[k] = flag
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。