# 排列组合的算法

1102人阅读 评论(1)

{
{1,2,3,4}，
{1,2,3,4}，
{1,2,3,4}，
{1,2,3,4}，
}从每组数据中取出一个，组合成一个新的数据，

----------------------------------------------------------------------------------------------

#include <iostream>
using namespace std;

int main()
{
int x[4][4]=
{
{1,2,3,4},
{5,6,7,8},
{1,2,3,4},
{1,2,3,4},
};
for(int a=0; a<4; ++a)
for(int b=0; b<4; ++b)
for(int c=0; c<4; ++c)
for(int d=0; d<4; ++d)
cout<<"{"<<x[0][a]<<x[1][b]<<x[2][c]<<x[3][d]<<"}"<<endl;
return 0;
}

----------------------------------------------------------------------------------------------------

#include <iostream>
using namespace std;

const int count=3;
int a[count][count];
int p[count];
int n;

void f()
{
int i;
if (n<0)
{
return;
}
for (int i=0; i<count; i++)
{
cout << a[i][p[i]];
}
cout <<endl;

p[n]++;
if (p[n]>=count)
while (p[n]>=count)
{
p[n]=0;
n--;
p[n]++;
if (n<0)
{
return;
}
}
n = count-1;
f();
}

main()
{
for (int i=0; i<count; i++)
for (int j=0; j<count; j++)
a[i][j] = j+1;
n = count-1;
f();
}

-------------------------------------------------------------------------------------------------------------------

#include <IOSTREAM>
#include <VECTOR>

using namespace std;

typedef
unsigned int
Counter;
typedef
vector<Counter>
CounterArrayType;
typedef
void (*DoThing)(CounterArrayType &);

Counter P(Counter Depth, const CounterArrayType & Endians, DoThing ThingToDo);

void ATestThing(CounterArrayType &);

int main(int argc, char* argv[])
{
int n = 3;
CounterArrayType Endian(size_t(n), 4);
cout << P(n, Endian, ATestThing);
cin >> n;
return 0;
}

Counter P(Counter Depth, const CounterArrayType & Endians, DoThing ThingToDo) {
Counter atDepth = 0, runTimes = 0;
bool Finished = false;
vector<Counter> LoopVar( Depth, 0 );
while (!Finished) {
atDepth = 0;
(*ThingToDo)(LoopVar);
++runTimes;
while (++LoopVar[atDepth] == Endians[atDepth]) {
LoopVar[atDepth++] = 0;
if (atDepth == Depth) {
Finished = true;
break;
}
}
}
return runTimes;
}

void ATestThing(CounterArrayType & vTest) {
static CounterArrayType::iterator itvTest;
cout << '[' << vTest.size() << ']';
for(itvTest = vTest.begin(); itvTest != vTest.end(); ++itvTest )
cout << ' ' << *itvTest;
cout << endl;
}

Counter P(Counter Depth, const CounterArrayType & Endians, DoThing ThingToDo)

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：19980次
• 积分：343
• 等级：
• 排名：千里之外
• 原创：13篇
• 转载：6篇
• 译文：0篇
• 评论：2条
文章存档