Experiment "data structure" a Shandong University: Recursive practice

Experiment a recursive practice

First, the purpose of the experiment

1, using familiar development tools. 2, grasp the idea of ​​recursive implementation.

Second, the experimental content

1, input a positive integer from 2 to 20 (2, 3 or 200, 300) is greater than 0, the input end of a 0, 0 is not involved in the arrangement.
2, the output of these full array of integers, each with a number of half-angle between "," separated, without spaces, each arranged in a separate row.
3, the program must have a Input, Output, End message, but there are other tips do not appear in the format, the same as the following experiment.

code show as below:

#include<iostream>
#include<cstdio>
#include<stdlib.h>
#include<string.h> 
using namespace std;
int a[25];
void perm(int k,int n){
	if(k==n){
		for(int i=0;i<n-1;i++)cout<<a[i]<<",";
		cout<<a[n-1]<<endl;
	}
	else{
		for(int i=k;i<n;i++){
			int t;
			t=a[i];a[i]=a[k];a[k]=t;
			perm(k+1,n);
			t=a[i];a[i]=a[k];a[k]=t;
		}
	}
}
int main(){
	int n;
	cout<<"Input"<<endl;
	for(n=0;cin>>a[n]&&a[n]!=0;n++);	
	cout<<"Output"<<endl;
	perm(0,n);
	cout<<"End"<<endl;	
	return 0;
} 

Conclusion Analysis and experience:

做递归的题目需要首先确定递归结束的条件,也是初始条件,然后找出函数的等价关系式,明确递归调用的顺序。
Published 11 original articles · won praise 0 · Views 38

Guess you like

Origin blog.csdn.net/weixin_43959421/article/details/103973486