Re: [algogeeks] Combinations with numbers

2010-10-06 Thread Wladimir Tavares
#include #define MAXN 100 int sum[MAXN]; void comb(int s,int n, int p, int t){ int j; if(p < n){ for(j=0;j<=s;j++){ sum[p] = j; t = t+j; comb(s,n,p+1,t); t = t-j; } }else{ if(t==s){ printf("%d",sum[0

[algogeeks] Combinations with numbers

2010-10-06 Thread MAC
Given a sum s, and an integer n as an input, find all possible combinations that sum up to s. For example: If s = 5, and n = 2, then output should be 0+5, 1+4, 2+3, 3+2, 4+1, 5+0 If s = 5, and n = 3, then output should be 0+0+5, 0+1+4, 0+2+3, 0+3+2, 0+4+1, 0+5+0, 1+0+4, 1+1+3, 1+2+2...2+0+3, 2+1+2.