Blue Bridge Cup Chapter II - recursion

Blue Bridge Cup Chapter II - recursive algorithm

Recursive algorithm program which is important to a process, then such a large complex problem by recursively decomposed into many small sub-problems simple, so easy to understand.

1. Classic example Feibolaqi series

Directly to the code.

#include <stdio.h>
int fib(int n);
int main()
{
int m = 0, total = 0;
printf("请输入一个数字,然后会输出相应的在该年份的费波拉契数列对应的数字:");
printf("\n");
printf("year = ");
scanf("%d",&m);
total = fib(m);
printf("第%d年是%d\n",m,total);
 }
int fib(int n)
{
if(n == 1 || n == 0)
return 1;
return fib(n-1)+fib(n-2);
}

2. seeking the greatest common divisor ( using Euclidean division)

Using the Euclidean algorithm for obtaining the greatest common divisor of two numbers recursive form.

#include <stdio.h>
int maxnum(int m, int n);
int main()
{
	int m = 0, n = 0;
	int max = 0;
	do
	{

		printf("请输入两个数m和n,然后会输出这两个数的最大公约数\n");
		printf("m = ");
		scanf("%d",&m);
		printf("n = ");
		scanf("%d",&n);
		max = maxnum(m,n);
		printf("%d 和 %d的最大公约数 = %d\n",m,n,max);
	}
	while(m!=0 && n!=0);
}
int maxnum(int m, int n)
{
	if(m % n == 0)
		return n;
	else
		return maxnum(n,m%n);
}

3. recursive insertion sort

The insertion sort rewrite recursive form.

 

#include <stdio.h>
#include <iostream>
using namespace std;
void insertSort(int a[], int k);
int main()
{
	int a[10]={1,2,3,4,5,6};
	int k = 10;
	insertSort(a,k);
	for(int i = 0; i < 10; i++)
	cout << a[i] << endl;
	return 0;
 } 
void insertSort(int a[], int k)
{
	cout <<"使用了此函数"<< endl;
	if( k == 0)
	return ;
	insertSort(a, k - 1);
	int x = a[k];
	int index = k - 1;
	while( x < a[index])
	{
		a[index+1] = a[index];
		index--;
	}
	a[index] = x;
}

4. Tower of Hanoi problem

This has not written.

 The binary search recursively

Like all ideas become just rewrite recursive form. (Note that the applicable conditions limited to an ordered array)

 

#include <iostream>
#include <algorithm>
using namespace std;
int erfen(int a[], int low, int high, int key);
int main()
{
	//利用sort排好序然后再用递归二分找出key元素所在位置 
	int a[10] = {0,1,2,3,4,5,8,7,6,9};
	sort(a+0,a+9);
	int key = 8;
	int low = 0, high = 9;
	int location = erfen(a,low,high,key); 
	cout << "第"<<location+1 <<"个元素是"<<key<< endl;
	return 0;
}
//使用二分查找的递归模式 
int erfen(int a[], int low, int high, int key)
{
	int mid = ( low + high )/2;
	if(low > high)
	return -1;
	if(key < a[mid])
	return erfen(a, mid+1, high, key);
	else if(key > a[mid])
	return erfen(a, mid+1, high, key);
	else
	return mid;
}

6. recursive ordering Hill

(I.e., insertion sort incremental change)

 

#include <iostream>
using namespace std;
int shellsort(int arr[]);
int arr[10] = {4,3,2,1,0,5,6,7,8,9};
int main()
{
	int i = 0;
	cout << "希尔排序前" << endl;
	for(i = 0; i < 10; i++)
		cout << arr[i] << " ";
	cout << endl;
	shellsort(arr);
	cout << "希尔排序后" << endl;
	for(i = 0; i < 10; i++)
		cout << arr[i] <<" ";
	return 0;
}
int shellsort(int arr[])
{
	int interval = 0;
	int len = 10, i = 0, j = 0;
	int target = 0;
	//使用增量法,间隔增量 - 1个的数字之间进行插入排序。
	for(interval = len / 2; interval > 0; interval /= 2)
		for(i = interval; i < len; i++)
		{
			target = arr[i];
			j = i - interval;
			while(j >-1 && target < arr[j])
			{
				arr[j + interval] = arr[j];
				j -= interval;
			}
			arr[j + interval] = target;
		}
}

7. White stairs

This problem namely the use of white last three moves to recursively obtained are finally left front 1,2,3 stepwise manner and can be obtained by adding the final method of stairs.

Note that the number of recursive condition secret staircase to 0 when it should be recursive return 1 instead of 0

 

#include <stdio.h>
int f(int n);
int main()
{
	int k = 1;
	int n = 0;
	int ans = 0;
	while(k != 0)
	{
		scanf("%d",&k);
		ans = f(k);
		printf("%d\n",ans);
	}
}
int f(int n)
{
	if(n == 0)
		return 1;
	else if(n == 1)
		return 1;
	else if(n == 2)
		return 2;
	return f(n - 1)+f(n - 2)+f(n-3);
}

8. Array rotation problems.

That transformation dichotomy problem, use an ordered array side, while the disorder is characterized by always crossed half the elements to find when the last only two elements of this program is the end state. Is the second smallest number.

For example after the source array is 01234567 rotation of a two-digit 23456701 (rotation starting characteristic is a number that partition, ordered left and right disorder, this feature can be calculated using rotation number)

As another example 00011111 3 after rotation but this time starting from the start of the rotation numbers 11111000, on its left number order, but the order from the right side of this figure also this feature is less than the preceding number this number may be ascertained.

There is no rotation but also consider the situation.

 

#include <stdio.h>
#include <iostream>
int min(int a[]);
using namespace std;
//利用二分法求解旋转数组的最小数字
int main()
{
	int a[]= {3,4,5,6,7,8,9,10,1,2};
	//int a[] = {1,0,1,1,1,1,1,1};
	int ans = 0;
	ans = min(a);
	cout << "最小数字是 " << ans << endl;
	return 0;
}
int min(int a[])
{
	int len = 0;
	bool flag = false;
	int begin = 0, end = 0;
	int mid = 0;
	while(a[len] != '\0')
		len++;
	cout << "len = " << len << endl;
	end = len - 1;
	//无旋转情况
	if(a[begin] < a[end])
		return a[begin];
	while(begin + 1 < end)
	{
		mid = ((end + begin)>>1);
		if(a[mid] == a[begin] && a[mid] == a[end])
		{
			flag = true;
			break;
		}
		if(a[mid] > a[begin])
			begin = mid;
		else
			end = mid;
	}
	if(flag)
	{
		int ans = a[0];
		for(int i = 1; i < len; i++)
			if(a[i] < ans)
				ans = a[i];
				return ans;
	}
	return a[end];
}

9. Find string

Not hahhh

10. The seek array longest increasing sequence.

The establishment of two pointers scratch pointer i, j pointer i from the start, once the drop is terminated, i + 1, j i point.

And then the sequence number of the original set up is relatively large then the switch.

 

#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
	int i = 0, j = 0, begin = 0, end = 0;
	int maxlen = 0, arrlen = 0, len = 1;
	int a[] = {1,9,6,2,8,3,4,5,7,0};
	arrlen = sizeof(a)/sizeof(a[0]);
	//cout << sizeof(a)/sizeof(a[0]) << endl;
	for(i = 0; i < arrlen - 1; i++)
		{
			len = 1;
		for(j = i; j < arrlen - 1 -1; j++)
		{
			if(a[j+1] >a[j])
			len++;
			else
			break;
		}
		if(len > maxlen)
		{
			maxlen = len;
			begin = i;
			end = j;
		}
	}
	cout <<"最长递增子序列为的长度为" << maxlen
	<<"分别是"<< endl; 
	for(i = begin; i <= end; i++)
	cout << "a["<<i<<"] = "<< a[i]<<" ";
	cout<<endl;
}

11. Efficient power algorithm

Or not, 55555 ~~~

Guess you like

Origin blog.csdn.net/CSDNsabo/article/details/91500457