Recursive small example

Palindrome judge

Press required input string and then determines the beginning and end of the intermediate, if not equal, the output occurs No, if it is a palindrome all equal, output Yes.

Source:

#include <iostream>
using namespace std;
void Huiwen(int n,char str[],int m);
int main(){
	int i=0;
	char str[100];
	cin>>str;
	while(str[i]!='\0')
		i++;
	Huiwen(0,str,i-1);
	return 0;
}

void Huiwen(int n,char str[],int m){
	if(str[n]!=str[m]){
		cout<<"No"<<endl;
		return ;
	}
	else{
		if(n<m)
		Huiwen(n+1,str,m-1);
		else
			cout<<"Yes"<<endl;
	}

}

Factor and the number of

Seeking the number of input factors recursive sum from his number to less than 1, and then compare the size of the original number, it is determined whether or not equal.

Source:

#include <iostream>
using namespace std;
int Sum(int n,int i);
int main(){
	int n;
	int x;
	cin>>n;
	x=Sum(n,n-1);
	if(n==x)
		cout<<"Yes"<<endl;
	else
		cout<<"No"<<endl;
	return 0;
}

int Sum(int n,int i){
	if(i>0){
	if(n%i==0)
		return Sum(n,i-1)+i;
	else
		return Sum(n,i-1);
	}
	else 
		return 0;
}

Power set collection

And the number of elements contained in the input set, according to the order of output power set to a given set of similar binary tree form, a recursive function into two, then two recursive solution, respectively.

Source:

#include <iostream>
#include <cstring>
using namespace std;
void Func(int i,char *Temp,int n);
char str[21];

int main()
{
	int n;
	char Temp[21]={0};
	cin>>n>>str;
	Func(0,Temp,n);
	return 0;
}

void Func(int i,char *Temp,int n)
{
	char reTemp[21]; 
	strcpy(reTemp,Temp);  
	if(i==n)
	{
		cout<<"("<<Temp<<")";
		return ;
	}
	else
	{
		Func(i+1,reTemp,n);
		strncat(reTemp,(str+i),1); 
		Func(i+1,reTemp,n);
	}
}

Polynomial

Algorithm Description:
requirements polynomials, and has been given the power of the number of coefficients, as long as the last item on the line is multiplied by the x, Wang Qianmian from a last cycle, with the result represents the total sum represented by the current number of power sum1 . Note: To open a bigger array, and can not use recursion and dual circulation, in order to avoid overtime.

Source:

#include <iostream>
using namespace std;
int main(){
	int i;
	int n,x;
	int a[100001];//开一个大一点的数组
	int sum=0,sum1=1;
	cin>>n>>x;
	for(i=0;i<=n;i++){//存各项系数
		cin>>a[i];
	}
	for(i=n;i>=0;i--){//从最后循环,求出多项式的解
		sum+=a[i]*sum1;
		sum1=sum1*x;
	}
	cout<<sum<<endl;
	return 0;
}

Stack problem

Algorithm Description:
recursive constantly push the stack until all elements are pushed onto the stack. We have to push the elements, advanced stack, and then recursively until all of them into the stack. Push the stack to be popped in there to get them out of the stack, these arrays into the final to be output, until all have popped output.

Source:

#include <iostream>
using namespace std;

int s[15];
int ss=0;
int a[15];
int length=0;

void push(int x){//进栈操作
	a[length++]=x;
}
int pop(){
	return a[--length];//出栈操作
}

void stack(int N[],int i,int in,int out){

	if(in==0&&out==0){//如果要进栈和要出栈的都没有输出栈内元素
		for(int j=0;j<i;j++){
			cout<<s[j];
		}
		cout<<endl;
	}

	else{

		if(in>0){//如果有要进栈的,进栈
			push(N[i]);
			stack(N,i+1,in-1,out);
			pop();
		}

		if(out>0&&out>in){//如果有要出栈的出栈
			s[ss++]=pop();
			stack(N,i,in,out-1);
			push(s[--ss]);
		}
	}
}

int main(){
	int n,i;
	cin>>n;
	int N[15];
	for(i=0;i<n;i++){//把1~n存到数组中,以便进出栈
		N[i]=i+1;
	}
	stack(N,0,n,n);
	return 0;
}

Guess you like

Origin blog.csdn.net/jihome/article/details/94962077