wtf

From Trivial Lion, 5 Years ago, written in C++, viewed 294 times.
URL https://code.nat.moe/view/4d8ac3e3 Embed
Download Paste or View Raw
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. // brute-force searching
  7. // enumerate every possibility and verify them
  8.  
  9.  
  10. int solve(const string&, const string&, const string&);
  11.  
  12. inline
  13. int enumerate_a(const string& a, const string& b, const string& dest, size_t idx) {
  14.     string ca = a;
  15.     for (size_t i = 0; i <= 9; ++i) {
  16.         ca[idx] = '0' + i;
  17.     }
  18.     return solve(ca, b, dest);
  19. }
  20.  
  21. inline
  22. int enumerate_b(const string& a, const string& b, const string& dest, size_t idx) {
  23.     string cb = b;
  24.     for (size_t i = 0; i <= 9; ++i) {
  25.         cb[idx] = '0' + i;
  26.     }
  27.     return solve(a, cb, dest);
  28. }
  29.  
  30. int solve(const string& a, const string& b, const string& dest) {
  31.     bool ahas = false, bhas = false;
  32.     int result = 0;
  33.     for (int i = 0; i < 3; ++i) {
  34.         if (a[i] == 'X')
  35.             result += enumerate_a(a, b, dest, i);
  36.         if (b[i] == 'X')
  37.             result += enumerate_b(a, b, dest, i);
  38.     }
  39.    
  40.     if (ahas || bhas)
  41.         return 0;
  42.     return ::atoi(a.c_str()) + ::atoi(b.c_str()) == ::atoi(dest.c_str()) ? 1 : 0;
  43. }
  44.  
  45. int main() {
  46.     int n;
  47.     cin >> n;
  48.     while (n-- > 0) {
  49.         string a, b, dest;
  50.         cin >> a >> b >> dest;
  51.         // pad
  52.         if (a.size() == 1) a = "00" + a;
  53.         if (a.size() == 2) a = "0" + a;
  54.         if (b.size() == 1) b = "00" + b;
  55.         if (b.size() == 2) b = "0" + b;
  56.        
  57.         cout << solve(a, b, dest) << endl;
  58.     }
  59. }

Reply to "wtf"

Here you can reply to the paste above

captcha