C++课后题-递归

C++课后题:

1.采用递归逆序打印一个整数中所有的数字:(简单)

#include <iostream>
#include <string>

using namespace std;

void reverstNumber(int num)
{
	if(num>0)
	{
        cout << (num%10);
	    reverstNumber(num/10);
	}
	//cout << endl;
}


int main(int argc, char *argv[])
{
        int number;
	cout << "Enter a number:  ";
	cin >> number;
	reverstNumber(number);
	cout << endl; 
	return 0;   //  
}

2. 采用递归的方法实现字符串逆序输出:

#include <iostream>
#include <string>

using namespace std;

void reverstNumber(int num)
{
	if(num>0)
	{
        cout << (num%10);
	    reverstNumber(num/10);
	}
	//cout << endl;
}

// 这里考虑写一个recursive helper function 
void reverseString(const string& ss)
{
	if(ss.size()>0)    // 停止条件 
	{
		cout << ss[ss.size()-1];   //输出字符串最后一个字符
		reverseString(ss.substr(0, ss.size()-1)); 
	}
}

int main(int argc, char *argv[])
{
        string s;
	cout << "Enter a string:  ";
	getline(cin, s);
	reverseString(s);
	cout << endl; 
	return 0;     
}

运行结果:

3.使用递归实现级数求和m(i)=1/3 + 2/5 + 3/7 + ......+ i/(2i+1)

该问题可以分解为:

m(i)=i/(2i+1)+m(i-1)

停止条件为i>0

代码:

#include <iostream>
#include <string>

using namespace std;

void reverstNumber(int num)
{
	if(num>0)
	{
        cout << (num%10);
	    reverstNumber(num/10);
	}
	//cout << endl;
}

// 这里考虑写一个recursive helper function 
void reverseString(const string& ss)
{
	if(ss.size()>0)    // 停止条件 
	{
		cout << ss[ss.size()-1];   //输出字符串最后一个字符
		reverseString(ss.substr(0, ss.size()-1)); 
	}
}

double sumCascade(int n)   // 求级数的前n项和
{
	if(n>0)    // 停止条件 
	   return static_cast<double>(n)/static_cast<double>(2*n+1) + sumCascade(n-1);
} 

int main(int argc, char *argv[])
{
        int number;
	cout << "Enter a number:  ";
	cin >> number; 
	cout << "The sum of 1/3+2/5+3/7+....+" << number << "/" << (2*number+1) << " = " << sumCascade(number) << endl; 
	return 0;     
}

运行结果:

2018-12-23

-----------------------------------分割线----------------------------------------

又来更新啦

用递归的方式实现选择排序:
代码:

#include <iostream>
#include <string>

using namespace std;

// 递归的方法实现选择排序
template<typename T>
void selectSort(T list[], int n)
{
	if(n>1)
	{
		int max_index = 0;
		int max_number = list[0];
		for(int i=1; i<n; i++)
		{
			if(list[i]>=max_number)
			{
				max_number = list[i];
				max_index = i;
			}
		}
		// 找到最大值与最大值的index
		list[max_index] = list[n-1];
		list[n-1] = max_number;
		selectSort(list, n-1);		 
	}
}  


int main(int argc, char *argv[])
{
    cout << "Hello" << endl;
    int a[] = {1,5,2,9,5,6,4,3};
    selectSort(a, 8);
    for(int i=0; i<8; i++)
    {
    	cout << a[i] << " ";
    }
    cout << endl;
	return 0;     
}

运行结果:

用递归的方式写冒泡排序:
一次冒泡的过程,数列的最大元素一定会移动到数列的尾部,这与选择排序中寻找序列最大值并将其放到队尾的操作一致,接着对前n-1个元素进行相同的操作,所以可以用递归的方式实现这一过程。

#include <iostream>
#include <string>

using namespace std;

template<typename T>
void bulbSort(T list[], int n)
{
    if(n>1)
    {
    	for(int i=0; i<n-1; i++)
    	{
	    	if(list[i]>list[i+1])
	    	{
	    		T temp = list[i+1];
	    		list[i+1] = list[i];
	    		list[i] = temp;
	    	}
	    }
	    bulbSort(list, n-1);
    }
}


int main(int argc, char *argv[])
{
    cout << "Hello" << endl;
    int a[] = {1,5,2,9,5,6,4,3};
    // selectSort(a, 8);
    bulbSort(a, 8); 
    for(int i=0; i<8; i++)
    {
    	cout << a[i] << " ";
    }
    cout << endl;
	return 0;     
}

实现插入排序的非递归版本:

#include <iostream>
#include <string>
#include <time.h>

using namespace std;

template<typename T>
void insertionSort(T list[], int n)    // 注意选择排序的思路 
{
	for(int i=1; i<n; i++)    // 从a[i](i=1;i<n)开始,向前遍历 
	{
		T temp = list[i];   // a[i]插入a[0: i-1] 
		int j;
		for(j=i-1; j>=0&&temp<list[j]; j--)    // 数组元素后移 
		{
			list[j+1] = list[j];
		}
		list[j+1] = temp;   // list[j+1] 
	}
}


int main(int argc, char *argv[])
{
    int a[] = {5, 6, 2, 1, 4, 9, 0};
	insertionSort(a, 7);
	for(int i=0; i<7; i++)
	{
		cout << a[i] << " ";
	} 
	cout << endl;
	return 0;     
}

猜你喜欢

转载自blog.csdn.net/zj1131190425/article/details/85223499
今日推荐