Skip to content

Instantly share code, notes, and snippets.

@tywong
Created June 8, 2016 07:25
Show Gist options
  • Save tywong/6c13f3f6a8a3d8803bf467c82f27902a to your computer and use it in GitHub Desktop.
Save tywong/6c13f3f6a8a3d8803bf467c82f27902a to your computer and use it in GitHub Desktop.
#include <stdio.h>
int count = 0;
void subset_sum(int k, int n, int r) {
if(n < 0)
return;
if(r == 0 && n == 0) {
count++;
return;
}
if(r == 0 && n != 0) {
return;
}
subset_sum(k+1, n-k, r-1);
subset_sum(k+1, n, r-1);
}
int main(void) {
int N = 10;
int n = 10;
int remain = n;
scanf("%d", &N);
subset_sum(1, N, remain);
printf("%d\n", count);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment