-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCubic_Num.cpp
More file actions
38 lines (36 loc) · 1.04 KB
/
Copy pathCubic_Num.cpp
File metadata and controls
38 lines (36 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <string>
#include <vector>
#include <regex>
#include <algorithm>
class Cubes
{
public:
static std::string isSumOfCubes(std::string &str);
};
std::string Cubes::isSumOfCubes(std::string &str)
{
std::vector<std::string> cubics = {"0", "00", "000", "1", "01", "001", "153", "370", "371", "407"};
std::vector<std::string> nb = {};
std::regex exp("\\d{1,3}");
std::smatch res;
std::string::const_iterator searchStart(str.cbegin());
while (regex_search(searchStart, str.cend(), res, exp))
{
std::string r = res[0];
nb.push_back(r);
searchStart += res.position() + res.length();
}
int sum = 0; std::string result = "";
for (unsigned int i = 0; i < nb.size(); i++)
{
std::string k = nb[i];
if (std::find(cubics.begin(), cubics.end(), k) != cubics.end())
{
result += std::to_string(std::stoi(k)) + " ";
sum += std::stoi(k);
}
}
if (result == "")
return "Unlucky";
return result + std::to_string(sum) + " Lucky";
}