判断是否聚合数

前端之家收集整理的这篇文章主要介绍了判断是否聚合数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

判断是否聚合数

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方法里的运算没搞懂!- - 原文链接:https://www.f2er.com/javaschema/285437.html

猜你在找的设计模式相关文章