i have a question for recursion in C++,i read a lot of posts,but i still can't get the logic in this exam i have.If you please help me what is the exact logic and if it possible to become easier i would be gratefull. If i enter N=3 and R=2 the answer is 12,13,21,23,31,32. But what is the logic?
#include <iostream>
using namespace std;
#define MAXN 20
int n, r;
char used[MAXN] = { 0 };
int mp[MAXN];
void permute(int i);
void print();
int main()
{
int i=0;
/*cout << "enter r";
cin >> r;
cout << "enter n";
cin >> n;*/
cout << used[0];
n = 3;
r = 2;
permute(i);
}
void permute(int i)
{
int k;
if (i >= r)
{
print();
return;
}
for (k = 0; k<n; k++)
{
if (!used[k])
{
used[k] = 1;
mp[i] = k;
permute(i + 1);
used[k] = 0;
}
}
}
void print()
{
int i;
for (i = 0; i<r; i++)
{
cout << mp[i] + 1;
}
cout << endl;
}
Aucun commentaire:
Enregistrer un commentaire