#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int num1[201], num2[201];
int lastDigit1, i, numE;
for (int k = 2; k > 0; k--) {
char c = getchar();
// delete zero at the beginning
while (c == '0') {
i = 0;
c = getchar();
}
i = 0;
while (c != '\n') {
numE = c - '0';
if (k == 2) {
num1[i] = numE;
} else {
num2[i] = numE;
}
i++;
c = getchar();
}
if (k == 2) {
lastDigit1 = i - 1; // keep last i of first n
}
i = i - 1;
}
int maxIndex = max(i,lastDigit1);
int result[maxIndex];
int temp = 0;
for ( ;max(i, lastDigit1) >= 0 ; lastDigit1 --, i --) {
if(i < 0){
result[lastDigit1] = num1[lastDigit1] + temp;
temp = 0;
}else if(lastDigit1 < 0){
result[i] = num2[i] + temp;
temp = 0;
}else{
result[max(i, lastDigit1)] = (num1[lastDigit1] + num2[i]) % 10 + temp;
temp = (num1[lastDigit1] + num2[i]) / 10;
}
}
for(int m = 0; m < maxIndex; m ++){
cout << result[m];
}
}
本地运行正常,线上报错segmentfault。求大佬指点!