Errrr... I've been thinking about this program for so long I can't think straight enough to see if a for loop is not needed. Here is the code, basically its a program that given a string such as "123" and an integer such as 10, it prints out the number of combinations of those three numbers adding and subtracting that equal or are less than 10. For example, the answer here is 6:

1+2+3

1-2-3

1+2-3

1-2+3

1-23

12-3

The only rule is that the first number must be positive, so "-123" or "-1+2-3" do not count .

Here is my code that works, but if I pass by value then something like string="927572819283748" and max value=123456789 goes past the 10 second limit.

Code:

#include<iostream>
#include<vector>
#include<string>
using namespace std;
class NumCombine
{
private:
int counter;
public:
NumCombine() : counter(0) {};
double numParse(string& s, int beg, int end)
{
double num=0;
while (isdigit(s[beg]) && beg<=end)
{
num=num*10+int(s[beg]-'0');
beg++;
}
return num;
}
void recursion(string& s, double total, int max)
{
total+=numParse(s, 0, s.length()-1);
if (total<=max)
counter++;
total-=numParse(s, 0, s.length()-1);
for (int x=1; x<s.length(); x++)
{
total+=(numParse(s, 0, s.length()-x-1));
recursion(s.substr(s.length()-x,x),total, max);
total-=(numParse(s, 0, s.length()-x-1));
}
total-=numParse(s, 0, s.length()-1);
if (total<=max)
counter++;
total+=numParse(s, 0, s.length()-1);
for (int xx=1; xx<s.length(); xx++)
{
total-=(numParse(s, 0, s.length()-xx-1));
recursion(s.substr(s.length()-xx,xx),total, max);
total+=(numParse(s, 0, s.length()-xx-1));
}
}
int numCombos(string& s, int max)
{
double total=0;
total+=numParse(s, 0, s.length()-1);
if (total<=max)
counter++;
total-=numParse(s, 0, s.length()-1);
for (int x=1; x<s.length(); x++)
{
total+=(numParse(s, 0, s.length()-x-1));
recursion(s.substr(s.length()-x,x),total, max);
total-=(numParse(s, 0, s.length()-x-1));
}
return counter;
}
};

The user only uses numCombos. I don't have time at the moment to place commentw, but I'll do it if it is hard to understand.