#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
string a, b, res;
cin >> a >> b >> res;
int n = a.size();
long long result = 0;
int consec = 0;
int carry = 0;
for(int i=n-1; i>=0; i--) {
int cur = (a[i]-'0') + (b[i]-'0') - (res[i]-'0');
if(cur + carry == 10) //ex: '6 + 9 (+0) = 5' or '6 + 9 (+1) = 6', where (+1) means carry and (+0) means no carry
carry = 1;
else if(cur == 10) { //remove previous carry
carry = 1;
consec = 0;
}
else if(cur + carry == 0) { //ex: '4 + 2 (+0) = 6' or '4 + 2 (+1) = 7'
carry = 0;
consec += 1;
result += consec;
}
else if(cur == 0) { //remove previous carry
carry = 0;
consec = 1;
result += consec;
}
else { //failure
consec = 0;
carry = 0;
}
}
cout << result << "\n";
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); string a, b, res; cin >> a >> b >> res; int n = a.size(); long long result = 0; int consec = 0; int carry = 0; for(int i=n-1; i>=0; i--) { int cur = (a[i]-'0') + (b[i]-'0') - (res[i]-'0'); if(cur + carry == 10) //ex: '6 + 9 (+0) = 5' or '6 + 9 (+1) = 6', where (+1) means carry and (+0) means no carry carry = 1; else if(cur == 10) { //remove previous carry carry = 1; consec = 0; } else if(cur + carry == 0) { //ex: '4 + 2 (+0) = 6' or '4 + 2 (+1) = 7' carry = 0; consec += 1; result += consec; } else if(cur == 0) { //remove previous carry carry = 0; consec = 1; result += consec; } else { //failure consec = 0; carry = 0; } } cout << result << "\n"; return 0; } |
English