判断是否聚合数
by 伍雪颖
#include <iostream> #include <sstream> #include <string> #include <cstdlib> using namespace std; bool match(int i,int j,string text) { string first = text.substr(0,i); string second = text.substr(i,j); string str = ""; str += first + second; while(str.length() < text.length()) { long long th = atoll(first.c_str())+atoll(second.c_str()); stringstream ss; string third = ""; ss << th; ss >> third; str += third; first = second; second = third; } return str.length()==text.length() && str == text; } bool isAggregatedNum(string text) { int len = text.length()/2; for(int i=1; i<=len; ++i) for(int j=1; j<=len; ++j) if(match(i,j,text)) return true; return false; } int main() { string s = "1111111111111111111111111112222222222222222222222222223333333333333333333333"; cout << isAggregatedNum(s) << endl; return 0; }match方法里的运算没搞懂!- -