请问这个问题有方法做出来吗
在区间中(0-11],(11-22],(22-33]中,分别取a,b,c个数(a,b,c由键盘输入),a+b+c=6,再由键盘输入一个数sum,输出所有满足a,b,c个数的数值之和为和数sum的数。(例如输入 1 2 3 (在0-11取 1个数,11-22取2个数,22-33取3个数)
101 (sum)
输出 1 12 27 28 33
......
#include <vector> #include <algorithm> #include <cstdio> int main( void ) { const std::vector<int> as = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; const std::vector<int> bs = { 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22 }; const std::vector<int> cs = { 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33 }; const size_t a = 1; const size_t b = 2; const size_t c = 3; const int sum = 101; { auto sumfun = []( const std::vector<int>& s, const std::vector<bool>& mask ) { int sum = 0; for( size_t i=0; i!=mask.size(); ++i ) if( mask[i] ) sum += s[i]; return sum; }; std::vector<bool> asp( as.size(), false ); for( size_t i=0; i!=a; ++i ) asp.at(as.size()-i-1)=true; do { int sum_a = sumfun( as, asp ); std::vector<bool> bsp( bs.size(), false ); for( size_t i=0; i!=b; ++i ) bsp.at(bs.size()-i-1)=true; do { int sum_b = sumfun( bs, bsp ); std::vector<bool> csp( cs.size(), false ); for( size_t i=0; i!=c; ++i ) csp.at(cs.size()-i-1)=true; do { int sum_c = sumfun( cs, csp ); if( sum_a+sum_b+sum_c == sum ) { for( size_t i=0; i!=asp.size(); ++i ) if(asp[i]) printf( "%d ", as[i] ); for( size_t i=0; i!=bsp.size(); ++i ) if(bsp[i]) printf( "%d ", bs[i] ); for( size_t i=0; i!=csp.size(); ++i ) if(csp[i]) printf( "%d ", cs[i] ); printf( "\n" ); } } while( std::next_permutation(csp.begin(),csp.end()) ); } while( std::next_permutation(bsp.begin(),bsp.end()) ); } while( std::next_permutation(asp.begin(),asp.end()) ); } return 0; }输出: