【題解】GreenJudge h176: A.A+B Problem

【題目敘述】http://tcgs.tc.edu.tw:1218/ShowProblem?problemid=h176

#include <iostream>  
#include <string.h>  
#include <queue>  
#include <vector>  
using namespace std;  
  
long long tonum(string s){  
    long long num = 0;  
    for (int i = 0; i < s.length(); i++){  
        num *= 10;  
        num += s[i] - '0';  
    }  
    return num;  
}  
  
int main(){  
    int num;  
    string str;  
    while (cin >> num){  
        for (int iter = 0; iter < num; iter++){  
            cin >> str;  
            int ans = 0;  
            vector <vector<string> > v;  
            vector <string> v2;  
            string s1, s2, s3;  
            for (int i = 1; i < min(16, (int)str.length()); i++){  
                for (int j = i+1; j < min(i+16, (int)str.length()); j++){  
                    s1 = str.substr(0, i);  
                    s2 = str.substr(i, j-i);  
                    s3 = str.substr(j);  
                    if(max(max(s1.size(),s2.size()),s3.size())>=17)continue;  
                    if (tonum(s1) + tonum(s2) == tonum(s3)){  
                        if ((s1 == "0" || s1[0] != '0') && (s2 == "0" || s2[0] != '0') && (s3 == "0" || s3[0] != '0')){  
                            ans += 1;  
                            v2.clear();  
                            v2.push_back(s1);  
                            v2.push_back(s2);  
                            v2.push_back(s3);  
                            v.push_back(v2);  
                        }  
                    }  
                }  
            }  
            cout << ans << "\n";  
            for (int i = 0; i < v.size(); i++){  
                cout << v[i][0] << " + " << v[i][1] << " = " << v[i][2] << "\n";  
            }  
        }  
    }  
}  
分享本文 Share with friends