## Interview Question

<pre lang="" line="1" title="CodeMonkey26644" class="run-this">static vector<int> vectornum;

int findAllSumNum(int num, int n, int znum)

{

if (n <= 0)

return 0;

else if (1 == n)

{

int size = vectornum.size();

for (int i = 0;i < size-1;i++)

printf("%d + ",vectornum[i]);

printf("%d = %d\n",vectornum[size-1],znum);

}

else if (2 == n)

{

int end = num&1 ? (num>>1)+1 : (num>>1);

for (int i = 1;i < end;i++)

{

vectornum.push_back(i);

vectornum.push_back(num-i);

findAllSumNum(0,1,znum);

vectornum.pop_back();

vectornum.pop_back();

}

}

else

{

int begin = num&1 ? (num>>1)+1 : (num>>1);

for (int i = begin;i < num-2;i++)

{

vectornum.push_back(i);

findAllSumNum(num-i,n-1,znum);

vectornum.pop_back();

}

}

return 0;

}

</pre><pre title="CodeMonkey26644" input="yes">

</pre>

#include<iostream>

using namespace std;

void print(int arr[], int length)

{

for(int i = 0; i < length; i++)

cout << arr[i];

cout << endl;

}

void Recurse(int sum , int n, int ind, int arr[])

{

if(n == 1 && (sum > 0 && sum < 10))

{

arr[ind] = sum;

print(arr, ind + 1);

return;

} else if(n == 1)

return;

for(int i = 0; i < 10; i++)

{

arr[ind] = i;

Recurse(sum - i, n - 1, ind + 1, arr);

}

return;

}

int main(void)

{

int sum = 13;

int n = 2;

int arr[2];

Recurse(sum, n, 0, arr);

return 0;

}

Does the order matter? so 6+7 is different from 7+6?

- Anonymous September 05, 2011