#include <stdio.h>#include <string.h>#include <assert.h>void print_seqs( char * seqs, int sum, int max ){ assert( max > 0 ); assert( sum > 0 ); char * end = seqs + strlen( seqs ); for( int i = max; i > 0; --i ) { sprintf( end, "%d, ", i ); if( sum == i ) { printf( "%s\n", seqs ); } else if( sum > i ) { print_seqs( seqs, sum-i, i ); } }}int main() { int q = 5; char seqs[100] = ""; print_seqs( seqs, q, q ); return 0;}
what is the name of this problem (if it exists)
Quote:Original post by hplus0603
...
Yes but your calculating the solution to some sub problems more than once! OMG^(&^&
well im working in Java so I wrote a java solution. Not sure if it works the same way yours does, but I will give Fruny a glance. Thanks
Hum I am guessing Fruny is a forum member?
Hum I am guessing Fruny is a forum member?
It may look like trivial recursion, but it seems it isn't. Anyway I didn't find the trivial stuff about it, but I manage to write some Haskell code that solves the problem. Maybe I missed some vital trick that makes it much easier, I dunno.
(zeroes not included, it didn't make any sense)
f 0 = [[]]f n = concat [[x : z | z <- f (n - x), z == [] || head z <= x] | x <- [n, n - 1 .. 1]]stylish = putStr . unlines . map (init . tail . show) . fMain> stylish 554,13,23,1,12,2,12,1,1,11,1,1,1,1
(zeroes not included, it didn't make any sense)
Quote:Original post by Kommi10
homework it is not. Thanks I will give a recursive solution a try
But it looks like homework, which is a violation of forum policy. A simple statement that it is not is really insufficient. Please read the Forum FAQ and in the future note that you should provide some justification for this type of academic problem.
You need a computer to solve problem like this, basically what you want to do is
lets say you want to decompose n
then find the solutions of
x1+x2 = n ( C(2+n-1,n), x1<=5,x2<=n)
x1+x2+x3 = n ( C(3+n-1,n), x1<=5,x2<=n,x3<=n)
...
....
....
X1+x2+x3+.....xn = n ( C(n+n-1,n), x1<=5,x2<=n,x3<=n....xn<n)
then add the results together and voila
lets say you want to decompose n
then find the solutions of
x1+x2 = n ( C(2+n-1,n), x1<=5,x2<=n)
x1+x2+x3 = n ( C(3+n-1,n), x1<=5,x2<=n,x3<=n)
...
....
....
X1+x2+x3+.....xn = n ( C(n+n-1,n), x1<=5,x2<=n,x3<=n....xn<n)
then add the results together and voila
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement