全配列アルゴリズムの簡単な実現を求めます
1025 ワード
( )
, , :
:allsort(a b c); a+allsort(b c); b+allsort(a c), c+allsort(a b);
: allsort(b c) , b+allsort(c) c+allsort(b);
, , , ;
c++ :
#include <iostream>
using namespace std;
void swap(int &a,int &b)//
{
int tem;
tem = a;
a = b;
b = tem;
}
void cal(int *a,int first,int length)
{
if(first == length)// ,
{
for(int i = 0; i <= length; i++)
cout<<a[i]<<" ";
cout<<endl;
}
else
{
for(int i = first; i <= length; i++)
{//
swap(a[first],a[i]);//
cal(a,first+1,length);// , , 。
swap(a[first],a[i]);//
}
}
}
int main()
{
int a[3] = {1,2,3};
cal(a,0,2);
return 0;
}