目录
7-1 递归 递推
7-2 函数的递归调用
7-3 A010 递归练习1
7-4 A011 递归练习2
7-5 A012 递归练习3
7-6 PG009 循环与递归
7-7 计算Fibonacci数列—递归
7-8 整数转换为字符串
7-9 简单的归并
AC: 9 / 9
用时:1 h 4 min
递归专题。
7-1 递归 递推
#include <iostream> #include <algorithm> #include <cstring> using namespace std; const int N = 110; int n, f1, a, b; int func(int x) { if(x == 1) return f1; return a * func(x - 1) + b; } int main() { while(scanf("%d%d%d%d", &n, &f1, &a, &b) != EOF) { cout << func(n) << endl; } return 0; }
注意,输入内容中没有给出数据组数或者结束输入的标志。
7-2 函数的递归调用
#include <iostream> #include <algorithm> #include <cstring> using namespace std; const int N = 110; int n; int func(int x) { if(x == 0 || x == 1) return 1; return x * func(x - 1); } int main() { cin >> n; if(n < 0) printf("%d<0,no value!no value!", n); else printf("%d!=%d", n, func(n)); return 0; }
n < 0时那个逗号是全角符号,建议直接复制粘贴。
7-3 A010 递归练习1
#include <iostream> #include <algorithm> #include <cstring> using namespace std; const int N = 110; int n, m, k; int f(int x) { if(x == 1) return k; return f(x - 1) + m; } int main() { while(scanf("%d%d%d", &n, &m, &k) != EOF) { cout << f(n) << endl; } return 0; }
7-4 A011 递归练习2
#include <iostream> #include <algorithm> #include <cstring> using namespace std; const int N = 110; int n, m, t; int f(int x, int y) { if(y == 1) return x; if(x < 0 || y < 0 || x < y) return 0; return f(x - 1, y) + f(x - 1, y - 1); } int main() { cin >> t; while(t--) { cin >> n >> m; cout << f(n, m) << endl; } return 0; }
7-5 A012 递归练习3
#include <iostream> #include <algorithm> #include <cstring> using namespace std; const int N = 110; int t; struct Node { int a; int b; }node; struct Node f(int x) { if(x == 0) return {1, 0}; struct Node tem = f(x - 1); return {tem.b, tem.a * 3 + tem.b * 2}; } int main() { while(scanf("%d", &t) != EOF) { node = f(t); cout << node.a << ' ' << node.b << endl; } return 0; }
7-6 PG009 循环与递归
#include <iostream> #include <algorithm> #include <cstring> using namespace std; const int N = 110; int jie(int x) { if(x == 1 || x == 0) return 1; return x * jie(x - 1); } double f(int x) { if(x == 1) return 1; double sign = (x % 2 == 0 ? -1 : 1); double res = sign / jie(2 * x - 1); return res + f(x - 1); } int main() { int n; while(scanf("%d", &n) != EOF) { printf("%.5f\n", f(n)); } return 0; }
7-7 计算Fibonacci数列—递归
#include <stdio.h> long Fib(int n); int count=0; int main() { int n; long f; scanf("%d", &n); f = Fib(n); printf("Fib(%d)=%ld, count=%d\n", n, f, count); return 0; } long Fib(int n) { count++; if(n == 1 || n == 2) return 1; return Fib(n - 1) + Fib(n - 2); }
7-8 整数转换为字符串
#include <iostream> #include <algorithm> #include <cstring> #include <string> using namespace std; string f(int x) { string s; if(x >= 0 && x <= 9) return s += '0' + x; int tem = x % 10; return f(x / 10) += '0' + tem; } int main() { int t; cin >> t; while(t--) { int x; cin >> x; string s; if(x >= 0) s = f(x); else s = '-' + f(-x); cout << s << endl; } return 0; }
递归函数只对非负整数进行处理,负整数在主函数中另外处理。
7-9 简单的归并
#include <iostream> #include <algorithm> #include <cstring> #include <set> using namespace std; multiset<int> box; int main() { int t; cin >> t; while(t--) { box.clear(); int n, m; cin >> n; while(n--) { int x; cin >> x; box.insert(x); } cin >> m; while(m--) { int x; cin >> x; box.insert(x); } for(set<int>::iterator it = box.begin(); it != box.end(); it++) { cout << *it; if(++it != box.end()) { cout << ' '; } it--; } puts(""); } return 0; }
这个能递归吗......