实验目的
1、 熟悉开发工具的使用。
2、 掌握递归的实现思想。
实验内容
1、 输入2-20个大于0的正整数(1、2、3或者100、200、300),输入0作为结束,0不参与排列。
2、 输出这几个整数的全排列,每个数之间用半角“,”隔开,中间不要有空格,每个排列单独一行。
//Author:horsetaill
#include <iostream>
using namespace std;
void swap(int a[20],int index1,int index2){
int temp=a[index1];
a[index1]=a[index2];
a[index2]=temp;
};
void permutation(int a[20],int start, int end){
if(start==end){
for(int i=0;i<end-1;i++)
cout << a[i] << ",";//这个地方不能和java一样用+连接 (气死)
cout << a[end-1]<< endl;
}
else
{
for(int j=start;j<end;j++){
swap(a,start,j);
permutation(a,start+1,end);
swap(a,start,j);
}
}
}
int main()
{ cout << "Input"<< endl;
int a;int array[20];int count=0;
cin >> a;
while(a!=0){
array[count++]=a;
cin >> a;
}
cout << "Output"<< endl;
permutation(array,0,count);//这个地方如果用sizeof获得的是20,所以必须用计数器
cout << "End0" << endl;
system("pause");
return 0;
}