#include <iostream>
#include <string>
uint64_t add_digits(const char first, const char second) {
return uint64_t(first -'0') + uint64_t(second - '0');
}
uint64_t count_pairs(const std::string &first, const std::string &second, const std::string &third) {
if (first.size() != second.size() || first.size() != third.size()) {
return 0;
}
//std::cerr << first << "\n" << second << "\n" << third <<"\n";
uint64_t result = 0;
bool continuation = false;
bool exceed = false;
for (int32_t i = first.size() - 1; i >=0; --i) {
uint64_t added = add_digits(first[i], second[i]);
uint64_t third_val = uint64_t(third[i] - '0');
bool third_equal = (added % 10 == third_val);
bool third_equal_ex = (added % 10 == third_val - 1) && (continuation && exceed);
if (continuation && third_equal) {
result += 1;
// std::cerr << i << " first condition\n";
}
if (third_equal) {
result += 1;
// std::cerr << i << " second condition\n";
}
exceed = (added >= 10);
continuation = third_equal || third_equal_ex;
}
return result;
}
void prog_main(std::istream& in, std::ostream& out)
{
std::string first, second, third;
in >> first >> second >> third;
out << count_pairs(first, second, third) ;
}
#ifndef TEST
int main(int argc, char* argv[])
{
prog_main(std::cin, std::cout);
return 0;
}
#endif
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 44 45 46 47 48 49 50 51 52 53 | #include <iostream> #include <string> uint64_t add_digits(const char first, const char second) { return uint64_t(first -'0') + uint64_t(second - '0'); } uint64_t count_pairs(const std::string &first, const std::string &second, const std::string &third) { if (first.size() != second.size() || first.size() != third.size()) { return 0; } //std::cerr << first << "\n" << second << "\n" << third <<"\n"; uint64_t result = 0; bool continuation = false; bool exceed = false; for (int32_t i = first.size() - 1; i >=0; --i) { uint64_t added = add_digits(first[i], second[i]); uint64_t third_val = uint64_t(third[i] - '0'); bool third_equal = (added % 10 == third_val); bool third_equal_ex = (added % 10 == third_val - 1) && (continuation && exceed); if (continuation && third_equal) { result += 1; // std::cerr << i << " first condition\n"; } if (third_equal) { result += 1; // std::cerr << i << " second condition\n"; } exceed = (added >= 10); continuation = third_equal || third_equal_ex; } return result; } void prog_main(std::istream& in, std::ostream& out) { std::string first, second, third; in >> first >> second >> third; out << count_pairs(first, second, third) ; } #ifndef TEST int main(int argc, char* argv[]) { prog_main(std::cin, std::cout); return 0; } #endif |
English