stl - Inserting values into a global C++ set from a recursion -
first time questioner. realized morning knew how perform task of interest simple recursive call. trouble global set not changed of calls s.insert(n) put in. expect there straightforward way rewrite fill set numbers found. so, question is, how that?
here function wrote it, fragment calls later, , relevant part of output. entirely happy lines cerr command, matter of getting values (first , third in each line) stl set. first guess on how part not enough.
..............................................................
void insert_primitive_reps(unsigned int a, unsigned int h, unsigned int b, unsigned int m, set<unsigned int> s) { cerr << setw(12) << << setw(12) << h << setw(12) << b << " insert_primitive_reps" << endl; if ( <= m ) { s.insert(a); if ( b <= m ) { s.insert(b); if ( <= m - b && h <= m - - b) { if( <= m - - h ) insert_primitive_reps(a, h + 2 * a, + b + h, m, s); if( b <= m - b - h ) insert_primitive_reps(a + b + h, h + 2 * b,b, m, s); // comment: once a+b+h <= m, min(2a+h, 2b+h) <= m } // if + b + h } // if b } // if } // end insert_primitive_rep
.....................................
set<unsigned int> s; insert_primitive_reps(1,3,1,100, s); cout << s.size() << endl; set<unsigned int>::iterator iteru; for(iteru = s.begin() ; iteru != s.end() ; ++iteru) { unsigned int p = *iteru; cout << setw(12) << p << factored(p) << endl; }
......................................
june 7 2014 1 3 1 insert_primitive_reps 1 5 5 insert_primitive_reps 1 7 11 insert_primitive_reps 1 9 19 insert_primitive_reps 1 11 29 insert_primitive_reps 1 13 41 insert_primitive_reps 1 15 55 insert_primitive_reps 1 17 71 insert_primitive_reps 1 19 89 insert_primitive_reps 55 95 41 insert_primitive_reps 41 69 29 insert_primitive_reps 29 47 19 insert_primitive_reps 95 85 19 insert_primitive_reps 19 29 11 insert_primitive_reps 19 67 59 insert_primitive_reps 59 51 11 insert_primitive_reps 11 15 5 insert_primitive_reps 11 37 31 insert_primitive_reps 11 59 79 insert_primitive_reps 79 99 31 insert_primitive_reps 31 25 5 insert_primitive_reps 31 87 61 insert_primitive_reps 61 35 5 insert_primitive_reps 5 5 1 insert_primitive_reps 5 15 11 insert_primitive_reps 5 25 31 insert_primitive_reps 5 35 61 insert_primitive_reps 61 87 31 insert_primitive_reps 31 37 11 insert_primitive_reps 31 99 79 insert_primitive_reps 79 59 11 insert_primitive_reps 11 7 1 insert_primitive_reps 11 29 19 insert_primitive_reps 11 51 59 insert_primitive_reps 59 67 19 insert_primitive_reps 19 9 1 insert_primitive_reps 19 47 29 insert_primitive_reps 19 85 95 insert_primitive_reps 29 11 1 insert_primitive_reps 29 69 41 insert_primitive_reps 41 13 1 insert_primitive_reps 41 95 55 insert_primitive_reps 55 15 1 insert_primitive_reps 71 17 1 insert_primitive_reps 89 19 1 insert_primitive_reps s size 0 jagy@phobeusjunior:~$
.........................................
help friend, plus hints looking "dereference" in book deitel , deitel:
....................................
void insert_primitive_reps(unsigned int a, unsigned int h, unsigned int b, unsigned int m, set<unsigned int> *s) { cerr << setw(12) << << setw(12) << h << setw(12) << b << " insert_primitive_reps" << endl; if ( <= m ) { (*s).insert(a); if ( b <= m ) { (*s).insert(b); if ( <= m - b && h <= m - - b) { if( <= m - - h ) insert_primitive_reps(a, h + 2 * a, + b + h, m, s); if( b <= m - b - h ) insert_primitive_reps(a + b + h, h + 2 * b,b, m, s); // comment: once a+b+h <= m, min(2a+h, 2b+h) <= m } // if + b + h } // if b } // if } // end insert_primitive_rep
...............................
cout << endl << " june 7 2014 " << endl << endl; set<unsigned int> s; insert_primitive_reps(1,3,1,100, &s); cout << " s size " << s.size() << endl; set<unsigned int>::iterator iteru; for(iteru = s.begin() ; iteru != s.end() ; ++iteru) { unsigned int p = *iteru; cout << setw(12) << p << factored(p) << endl; }
.................................
june 7 2014 1 3 1 insert_primitive_reps 1 5 5 insert_primitive_reps 1 7 11 insert_primitive_reps 1 9 19 insert_primitive_reps 1 11 29 insert_primitive_reps 1 13 41 insert_primitive_reps 1 15 55 insert_primitive_reps 1 17 71 insert_primitive_reps 1 19 89 insert_primitive_reps 55 95 41 insert_primitive_reps 41 69 29 insert_primitive_reps 29 47 19 insert_primitive_reps 95 85 19 insert_primitive_reps 19 29 11 insert_primitive_reps 19 67 59 insert_primitive_reps 59 51 11 insert_primitive_reps 11 15 5 insert_primitive_reps 11 37 31 insert_primitive_reps 11 59 79 insert_primitive_reps 79 99 31 insert_primitive_reps 31 25 5 insert_primitive_reps 31 87 61 insert_primitive_reps 61 35 5 insert_primitive_reps 5 5 1 insert_primitive_reps 5 15 11 insert_primitive_reps 5 25 31 insert_primitive_reps 5 35 61 insert_primitive_reps 61 87 31 insert_primitive_reps 31 37 11 insert_primitive_reps 31 99 79 insert_primitive_reps 79 59 11 insert_primitive_reps 11 7 1 insert_primitive_reps 11 29 19 insert_primitive_reps 11 51 59 insert_primitive_reps 59 67 19 insert_primitive_reps 19 9 1 insert_primitive_reps 19 47 29 insert_primitive_reps 19 85 95 insert_primitive_reps 29 11 1 insert_primitive_reps 29 69 41 insert_primitive_reps 41 13 1 insert_primitive_reps 41 95 55 insert_primitive_reps 55 15 1 insert_primitive_reps 71 17 1 insert_primitive_reps 89 19 1 insert_primitive_reps s size 14 1 = 1 5 = 5 11 = 11 19 = 19 29 = 29 31 = 31 41 = 41 55 = 5 * 11 59 = 59 61 = 61 71 = 71 79 = 79 89 = 89 95 = 5 * 19 jagy@phobeusjunior:~$
.................................
Comments
Post a Comment