Aug 27, 2013 at 6:38pm UTC
i want to find all permuatations of sum of given numbers is there any function in cpp stl or any other way?
eg:-
say we have numbers - 1,2,3
then output will be like this - 1,2,3,3,4,5
it will be better if no repeated integers are shown!
Aug 27, 2013 at 6:50pm UTC
ie;
a,b,c inputs
output
a, b, c, a+b, a+c, b+c
Aug 27, 2013 at 6:52pm UTC
Sure, use:
http://www.cplusplus.com/reference/algorithm/next_permutation/
For a sum of any two numbers we can do this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream> // std::cout
#include <algorithm> // std::next_permutation, std::sort
int main ()
{
int myints[] = {1,2,3};
std::sort ( myints, myints + 3 );
do
{
std::cout << myints[0] + myints[1] << ',' ;
} while ( std::next_permutation(myints,myints+3) );
return 0;
}
3,4,3,5,4,5
Ok well I guess it isn;t that simple. This will return a sum of every two sets, regardless of order (so 2+3 and 3+2 are both counted).
Last edited on Aug 27, 2013 at 6:55pm UTC
Aug 27, 2013 at 6:58pm UTC
that will help but if you suggest any way that work for all sets that will be the best! ty!
Aug 27, 2013 at 9:01pm UTC
You could easily alter stewbond's example and place the results in a set so that you don't get duplicates.