递归是指函数直接或间接调用自身的一种方法,它通常可把一个复杂的大型问题层层转化为与原问题相似但规模较小的问题来求解。
递归策略只需少量的程序就可描述解题过程所需的多次重复计算,因此大大减小了程序的代码量。
8.1 递归策略
构成递归需要两个条件:
- 子问题必须与原始问题相同,且规模更小。
- 不能无限制地调用本身,必须有一个递归出口。
例题8.1 n的阶乘
提交网址
递归方法:
#include <iostream>
using namespace std;
long long Factorial(int n){
if(n == 1){
return 1;
}
return n * Factorial(n - 1);
}
int main(){
int n;
while(scanf("%d", &n) != EOF){
printf("%lld\n", Factorial(n));
}
return 0;
}
循环方法:
#include <iostream>
using namespace std;
int main(){
int n;
while(scanf("%d", &n) != EOF){
long long factorial = 1;
while(n >= 1){
factorial *= n--;
}
printf("%lld\n", factorial);
}
return 0;
}
例题8.2 汉诺塔III
汉诺塔是经典的递归题目,这一题对题目做了一些改动,加深对递归的理解。
#include <iostream>
using namespace std;
int Hanoi(int n){
if(n == 1){
return 2;
}
return 3 * Hanoi(n - 1) + 2;
}
int main(){
int n;
while(scanf("%d", &n) != EOF){
printf("%lld\n", Hanoi(n));
}
return 0;
}
习题8.1 杨辉三角形
提交网址 这题明明更容易想到的是循环的方法。
离谱。牛客网这块儿的用例怎么这么离谱!也不是第一次示例和测试用例不同了吧。
要从第二行开始输出,第一行的1不用输出。
#include <iostream>
using namespace std;
long long a[1000][1000];
int main(){
int n;
while(scanf("%d", &n) != EOF){
for(int i=0; i<n; i++){
a[i][0] = a[i][i] = 1;
}
for(int i=2; i<n; i++){
for(int j=1; j<i; j++){
a[i][j] = a[i-1][j-1] + a[i-1][j];
}
}
for(int i=1; i<n; i++){
int j;
for(j=0; j<i; j++){
printf("%lld ", a[i][j]);
}
if(j == i){
printf("%lld\n", a[i][j]);
}
}
}
return 0;
}
习题8.2 全排列
提交网址
c++有next_permutation库,非常适合这一题。
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main(){
string str, tmp;
while(cin >> str){
sort(str.begin(), str.end());
do{
cout << str << endl;
}while(next_permutation(str.begin(), str.end()));
printf("\n");
}
return 0;
}
如果不用这个函数,就类似于深度优先搜索。如下:
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
bool used[30];
string out = "";
string str;
void dfs(int k, int n){
if(k == n){
cout << out << endl;
}else{
for(int i=0; i<n; i++){
if(!used[i]){
out += str[i];
used[i] = true;
dfs(k+1, n);
out = out.substr(0, out.size() - 1);
used[i] = false;
}
}
}
}
int main(){
while(cin >> str){
sort(str.begin(), str.end());
memset(used, false, sizeof(used));
dfs(0, str.length());
}
return 0;
}
8.2 分治法
分治法的步骤如下:
- 分:将问题分解为规模更小的子问题。
- 治:将这些规模更小的子问题逐个击破。
- 合:将已解决的子问题合并,最终得出“母”问题的解。
例题8.3 Fibonacci
#include <iostream>
using namespace std;
int main(){
int Fib[31];
Fib[0] = 0;
Fib[1] = 1;
for(int i=2; i<=30; i++){
Fib[i] = Fib[i-1] + Fib[i-2];
}
int n;
while(scanf("%d", &n) != EOF){
printf("%d\n", Fib[n]);
}
return 0;
}
例题8.4 二叉树
#include <iostream>
using namespace std;
int m, n, cnt;
void dfs(int k){
if(k > n) return;
cnt++;
dfs(2 * k);
dfs(2 * k + 1);
}
int main(){
while(scanf("%d%d", &m, &n) != EOF){
if(m == 0 && n == 0) break;
cnt = 0;
dfs(m);
printf("%d\n", cnt);
}
return 0;
}
习题8.3 2的幂次方
#include <iostream>
#include <string>
using namespace std;
string ToString(int num){
if(num == 1){
return "2(0)";
}else if(num == 2){
return "2";
}else{
string str = "";
while(num > 0){
int tmp = 1, i;
for(i=0; tmp <= num; i++){
tmp *= 2;
}
if(i == 1 || i == 2){
str += ToString(i) + "+";
}else{
str += "2(" + ToString(i - 1) + ")+";
}
num -= tmp / 2;
}
return str.substr(0, str.size() - 1);
}
}
int main(){
int n;
while(scanf("%d", &n) != EOF){
cout << ToString(n) << endl;
}
return 0;
}