// // Task 2: Split Same Average // // C version. // #include #include #include #include #include #include "args.h" #include "parseints.h" #include "printarray.h" #define EPSILON 0.00001 // // void subset_select( comb, nval, val[], &nsel, sel[], &nnotsel, notsel[] ); // Consider comb as an nsel-bit binary unsigned number. // Select all the values from val[] whose corresponding bit in comb is 1, // store them into sel[nsel] (setting nsel), and store all the unselected // values into notsel[nnotsel], setting nnotsel. // void subset_select( int comb, int nval, int *val, int *nsel, int *sel, int *nnotsel, int *notsel ) { *nsel = *nnotsel = 0; for( int i=0; i>= 1; } } // // bool found = count_val_avg( goalavg, nval, val[], &nsel, sel[], &nnotsel, notsel[] ); // Attempt to find a subset of val[nval] whose average is goalavg. // If we find one, copy the number of subset elements into nsel, // and all subset elements into sel[nsel], and all other elements into // notsel[], setting notnsel to nval - nsel, and return true. // If we can't find one, return false. // Do it by iterating over all D-bit binary numbers from 1..2**nval-2 // subset-summing each selected combination // bool count_val_avg( double goalavg, int nval, int *val, int *nsel, int *sel, int *nnotsel, int *notsel ) { // d = 2**nval - 2 int d = 1; for( int i=0; i