Thread: permutation blues

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    22

    permutation blues

    suppose you have a number 1234 how to code solution in c so as to find all the possible combnations of this number. eg : 1423, 1324,3214 etc....

    TIA
    ~codomaniac

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Here's how to do it using STL containers/algorithms:
    Code:
    #include <algorithm>
    #include <iostream>
    #include <set>
    
    int main()
    {
    	int Numbers[] = {1, 2, 3, 4};
    	std::set<int> S(Numbers, Numbers + (sizeof(Numbers) / sizeof(int)));
    	do
    	{
    		std::copy(S.begin(), S.end(), std::ostream_iterator<int>(std::cout, " "));
    		std::cout << std::endl;
    	}
    	while(std::next_permutation(S.begin(), S.end()));
    	return 0;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Permute 1
    Dead easy

    Permute 12
    Pretty easy, 12 or 21

    Permute 123
    That's just
    1 with permutations of 23
    2 with permutations of 13
    3 with permutations of 12
    Since you already know how to permute a pair of digits, it shouldn't be hard

    Permute 1234
    1 with permutations of 234
    ....

    Get the picture?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    22
    no sirs ! I am bit confused as to how get permutations ! . How to program it in C. It would be nice if you tell me the logic involved in simple steps.

    TIA
    ~codomaniac

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    I thought that was pretty simple

    The only way it's going to get simpler is by handing over the source code for your homework
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bit level permutation function
    By zxcv in forum C Programming
    Replies: 2
    Last Post: 07-27-2008, 01:26 PM
  2. Problem creating a recursive string permutation function
    By indigo0086 in forum C++ Programming
    Replies: 4
    Last Post: 10-10-2006, 10:09 AM
  3. Sorting: Getting permutation index array
    By flyvholm in forum C Programming
    Replies: 2
    Last Post: 09-20-2006, 07:07 PM
  4. Permutation algorithm??
    By lris2005 in forum C++ Programming
    Replies: 1
    Last Post: 04-01-2006, 06:51 AM
  5. Permutation Calculation
    By Eric Hansen in forum C++ Programming
    Replies: 21
    Last Post: 06-11-2003, 04:03 PM