Untitled

From Mature Crow, 5 Years ago, written in Plain Text, viewed 295 times.
URL https://code.nat.moe/view/647d6f52 Embed
Download Paste or View Raw
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>
  4. #include <set>
  5.  
  6. using namespace std;
  7.  
  8. // brute-force searching
  9. // enumerate every possibility and verify them
  10. struct Solution {
  11.     int a, b;
  12. };
  13.  
  14. bool operator<(const Solution& a, const Solution& b) {
  15.     if (a.a == b.a)
  16.         return a.b < b.b;
  17.     return a.a < b.a;
  18. }
  19.  
  20. set<Solution> solutions;
  21.  
  22. void solve(const string&, const string&, const string&);
  23.  
  24. inline
  25. void enumerate_a(const string& a, const string& b, const string& dest, size_t idx) {
  26.     string ca = a;
  27.     for (size_t i = 0; i <= 9; ++i) {
  28.         ca[idx] = '0' + i;
  29.         solve(ca, b, dest);
  30.     }
  31. }
  32.  
  33. inline
  34. void enumerate_b(const string& a, const string& b, const string& dest, size_t idx) {
  35.     string cb = b;
  36.     for (size_t i = 0; i <= 9; ++i) {
  37.         cb[idx] = '0' + i;
  38.         solve(a, cb, dest);
  39.     }
  40. }
  41.  
  42. void solve(const string& a, const string& b, const string& dest) {
  43.     bool ahas = false, bhas = false;
  44.     for (int i = 0; i < 3; ++i) {
  45.         if (i < a.size() && a[i] == 'X') {
  46.             enumerate_a(a, b, dest, i);
  47.             ahas = true;
  48.         }
  49.         if (i < b.size() && b[i] == 'X') {
  50.             enumerate_b(a, b, dest, i);
  51.             bhas = true;
  52.         }
  53.     }
  54.    
  55.     if (ahas || bhas) {
  56.         return;
  57.     }
  58.  
  59.     auto _a = ::atoi(a.c_str());
  60.     auto _b = ::atoi(b.c_str());
  61.     auto _c = ::atoi(dest.c_str());
  62.     if (_a != 0 && _b != 0 && _a + _b == _c) {
  63.         Solution c;
  64.         c.a = _a;
  65.         c.b = _b;
  66.         // cout << _a << "," << _b << endl;
  67.         solutions.insert(c);
  68.     }
  69. }
  70.  
  71. int main() {
  72.     int n;
  73.     cin >> n;
  74.     while (n-- > 0) {
  75.         string a, b, dest;
  76.         cin >> a >> b >> dest;
  77.        
  78.         solutions.clear();
  79.         solve(a, b, dest);
  80.         cout << solutions.size() << endl;
  81.     }
  82. }

Reply to "Untitled"

Here you can reply to the paste above

captcha