c++递归练习

实验目的
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; 
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值