Permutations with specified sample space

I have 10 objects and I want to choose 5 from these objects and repetition is allowed. The permutation equation for that is 10!/(10-5)! which will give me all the possible permutations. I want to know if there is a C++ code that can do that. I give it an array of 10 objects and it prints out all the permutations of 5 selected objects at a time.

Thank You
If repetition is allowed then that's not a permutation.

The problem you describe is equivalent to having an alphabet of 10 elements and generating all strings of size 5 in that alphabet, which in turn is equivalent to generating the decimal representations of all integers between 0 and 10^5 - 1, zero-padded to 5 digits.
P(a,b)
c=a-1;
a=-1;
a=a*c;
if(c==1){
d=a-b;
e=d-1;
d=-1;
e=e*d;
ret a/(a-e);
}
What??
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream> 
#include <vector>
#include <string>
using namespace std;

const string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

vector<string> nPr( string options, int r )
{
   if ( r == 0 ) return vector<string>( 1, "" );
   vector<string> result;
   for ( int i = 0; i < options.size(); i++ )
   {
      string remainder = options;
      remainder.erase( i, 1 );
      vector<string> temp = nPr( remainder, r - 1 );  // recursive call
      for ( string s : temp ) result.push_back( options[i] + s );
   }
   return result;
}
   

int main()
{
   int n = 5;   //10
   int r = 3;   //5
   vector<string> permutations = nPr( ALPHABET.substr( 0, n ), r );
   for ( string p : permutations ) cout << p << '\n';
}

Topic archived. No new replies allowed.