//Solution by Mikołaj Kołek
#include "bits/stdc++.h"
#define intin *istream_iterator<int>(cin)
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
string a, b, c;
cin >> a >> b >> c;
int n = a.length();
long long matching = 0, withCarry = 0, res = 0;
for(int i = n - 1; i >= 0; i--) {
int sum = (a[i] - '0') + (b[i] - '0'), target = c[i] - '0';
if(sum == target) {
matching++;
withCarry = 0;
res += matching;
}
else if(sum == (target - 1)) {
matching = withCarry;
withCarry = 0;
res += matching;
}
else if(sum == (target + 10)) {
withCarry = matching + 1;
matching = 0;
}
else if(sum == (target + 9))
matching = 0;
else
matching = withCarry = 0;
}
cout << res << "\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 43 | //Solution by Mikołaj Kołek #include "bits/stdc++.h" #define intin *istream_iterator<int>(cin) using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); string a, b, c; cin >> a >> b >> c; int n = a.length(); long long matching = 0, withCarry = 0, res = 0; for(int i = n - 1; i >= 0; i--) { int sum = (a[i] - '0') + (b[i] - '0'), target = c[i] - '0'; if(sum == target) { matching++; withCarry = 0; res += matching; } else if(sum == (target - 1)) { matching = withCarry; withCarry = 0; res += matching; } else if(sum == (target + 10)) { withCarry = matching + 1; matching = 0; } else if(sum == (target + 9)) matching = 0; else matching = withCarry = 0; } cout << res << "\n"; return 0; } |
English