//从末尾开始依次相加,当作你在用笔做加法运算一个道理 //设置一个进位标识,两数相加大于等于10时,进位标识转为ture // 第一个循环跳出之后开始处理未处理完的字符串 string solve(string s, string t) { int i = s.length() - 1, j = t.length() - 1; bool process = false; //进位标识 char temp; while (i >= 0 && j >= 0) { temp = s[i] + t[j] - 48 + (process ? 1 : 0); process = false; if (temp >= 58) { temp -= 10; process = true; } s[i] = temp; t[j] = temp; i--; j--; } string r; if (i < 0 && j < 0) { if (process) { s = '1' + s; } return s; } if (i >= 0) { while (i >= 0) { s[i] += (process ? 1 : 0); process = false; if (s[i] >= 58) { s[i] -= 10; process = true; } i--; } if (process) { s = '1' + s; } r = s; } else { while (j >= 0) { t[j] += (process ? 1 : 0); process = false; if (t[j] >= 58) { t[j] -= 10; process = true; } j--; } if (process) { t = '1' + t; } r = t; } return r; }