CodeMaster has just returned from shopping. He scanned the check of the items he bought and gave the resulting string to Ratiorg to figure out the total number of purchased items. Since Ratiorg is a bot he is definitely going to automate it, so he needs a program that sums up all the numbers which appear in the given input. Help Ratiorg by writing a function that returns the sum of numbers that appear in the given inputString. Example For inputString = "2 apples, 12 oranges", the output should be sumUpNumbers(inputString) = 14. |
code>>
int sumUpNumbers(std::string inputString) {
std::regex re("([0-9]+)");
std::smatch m;
int sum = 0;
while (std::regex_search(inputString, m, re))
{
sum += std::atoi(m[0].str().c_str());
inputString = m.suffix();
}
return sum;
}
'알고리즘 > codefights' 카테고리의 다른 글
56>digitsProduct (0) | 2018.04.12 |
---|---|
55>differentSquares (0) | 2018.03.28 |
53>validTime (0) | 2018.02.10 |
52>longestWord (0) | 2018.02.04 |
51>deleteDigit (0) | 2018.02.01 |