To make my question more clear, I'll give an example. Say I want to implement a recursive function that sums up the range of numbers from [1-10] and [15-20], skipping (10-15). I would want to add up [15-20] and skip the function calls on stack of (10-15), and continue with [1-10]. How can I do this?
int summation(int x, int y) {
if(x == y) return x;
// Catch here and return sum of 15-20 back to calls of 1-10
if(x == 10)
catch(int n)
return n;
int sum = x + summation(x+1,y);
// Skip function calls 10-15
if(x==15) throw sum;
return sum;
}
summation(1,20) // >> 160 or [1-10] = 55, [15,20] = 105
I know how to solve the above example with a different approach, but this example gives an idea of what I'm trying to do.
Aucun commentaire:
Enregistrer un commentaire