玩栈啊...6.你等下我找个东西
by Gary_NotFound @ 2023-10-01 18:39:43
```cpp
#include <iostream>
#include <cstring>
using namespace std;
int main(){
char s1[1001],s2[1001];
int a[1001]={},b[1001]={},c[1001]={};
cin>>s1>>s2;
int lena=strlen(s1),lenb=strlen(s2);
for(int i=0;i<lena;i++){
a[lena-1-i]=s1[i]-'0';
}
for(int i=0;i<lenb;i++){
b[lenb-1-i]=s2[i]-'0';
}
int lenc=max(lena,lenb);
int x=0;
for(int i=0;i<lenc;i++){
c[i]=a[i]+b[i]+x;
x=c[i]/10;
c[i]%=10;
}
if(x){
c[lenc]=x;
lenc++;
}
for(int i=lenc-1;i>=0;i--){
cout<<c[i];
}
return 0;
}
```
下次用char数组吧。
有帮助关注下,谢谢喵
by Gary_NotFound @ 2023-10-01 18:41:55
**改了一下,做了前导零去除,5WA!!!**
```cpp
#include<bits/stdc++.h>
using namespace std;
stack<int> num2;
stack<int> num;
stack<int> ans;
char x;
int main() {
bool a = 0;
while (1) {
x = getchar();
if (x > '9' || x < '0')break;
if (x != '0') {
a = 1;
} else {
if (!a) {
continue;
}
}
int a = x - '0';
num.push(a);
}
a = 0;
while (1) {
x = getchar();
if (x > '9' || x < '0')break;
if (x != '0') {
a = 1;
} else {
if (!a) {
continue;
}
}
int a = x - '0';
num2.push(a);
}
bool j = false;
while (!num.empty() && !num2.empty()) {
int f = num.top() + num2.top();
num.pop();
num2.pop();
if (j)f++, j = 0;
if (f > 9) {
f -= 10;
j = 1;
}
ans.push(f);
}
while (!num.empty()) {
int f = num.top();
if (j) {
f++;
j = 0;
}
if (f > 9) {
f -= 10;
j = 1;
}
ans.push(f);
num.pop();
}
while (!num2.empty()) {
int f = num2.top();
if (j) {
f++;
j = 0;
}
if (f > 9) {
f -= 10;
j = 1;
}
ans.push(f);
num2.pop();
}
if (j)cout << "1";
while (!ans.empty()) {
cout << ans.top();
ans.pop();
}
}
```
by leiwenjin1234 @ 2023-10-05 13:59:22