第一部分 C++语言
第六章 函数
第二节 递归算法
1158 求1+2+3+…
#include <iostream>
using namespace std;
int f(int x) {
if (x == 1) return 1;
return f(x-1) + x;
}
int main() {
int n;
cin >> n;
cout << f(n);
return 0;
}
1159 斐波那契数列
#include <iostream>
using namespace std;
int f(int x) {
if (x == 1) return 0;
if (x == 2) return 1;
return f(x-1) + f(x-2);
}
int main() {
int n;
cin >> n;
cout << f(n);
return 0;
}
1160 倒序数
#include <iostream>
using namespace std;
void f(int x) {
if (x == 0) return;
cout << x % 10;
return f(x/10);
}
int main() {
int n;
cin >> n;
f(n);
return 0;
}
1161 转进制
#include <iostream>
using namespace std;
void f(int x, int m) {
if (x == 0) return;
f(x/m, m);
int t = x % m;
if (t < 10) cout << t;
else cout << char(t - 10 + 'A');
}
int main() {
int x, m;
cin >> x >> m;
f(x, m);
return 0;
}
1162 字符串逆序
#include <iostream>
using namespace std;
string s;
void f(int i) {
if (s[i] == '!') return;
f(i+1);
cout << s[i++];
}
int main() {
cin >> s;
f(0);
return 0;
}
1163 阿克曼(Ackmann)函数
#include <iostream>
using namespace std;
int A(int m, int n) {
if (m == 0) return n + 1;
else if (m > 0 && n == 0) return A(m-1, 1);
else return A(m-1, A(m, n-1));
}
int main() {
int m, n;
cin >> m >> n;
cout << A(m, n);
return 0;
}
1164 digit函数
#include <iostream>
using namespace std;
void digit(int n, int k) {
if (k == 1) cout << n % 10;
else digit(n/10, k-1);
}
int main() {
int n, k;
cin >> n >> k;
digit(n, k);
return 0;
}
1165 Hermite多项式
#include <cstdio>
#include <iostream>
using namespace std;
double h(int n, double x) {
if (n == 0) return 1;
else if (n == 1) return 2*x;
else return 2 * x * h(n-1, x) - 2 * (n-1) * h(n-2, x);
}
int main() {
int n;
double x;
cin >> n >> x;
printf("%.2lf\n",h(n,x));
return 0;
}
1166 求f(x,n)
#include <cstdio>
#include <iostream>
#include <cmath>
using namespace std;
double f(double x, int n) {
if (n == 1) return sqrt(1 + x);
else return sqrt(n + f(x, n-1));
}
int main() {
int n;
double x;
cin >> x >> n;
printf("%.2lf\n",f(x, n));
return 0;
}
1167 再求f(x,n)
#include <cstdio>
#include <iostream>
using namespace std;
double f(double x, int n) {
if (n == 1) return x / (1 + x);
else return x / (n + f(x, n-1));
}
int main() {
int n;
double x;
cin >> x >> n;
printf("%.2lf\n",f(x, n));
return 0;
}
如果您的孩子四年级及以上,对计算机编程感兴趣,且文化课学有余力,欢迎联系客服(微信号:xiaolan7321),参加信息学的学习。我们是专业的信息学竞赛教练,采用线上小班授课的方式,目标是帮助热爱编程的中小学生,在国内外信息学竞赛中取得优秀成绩。
教学特点:
-
线上小班授课,打好代码基础。避免大班课堂上学生要么“跟不上”,要么“吃不饱”的问题。
-
教学经验丰富,熟悉学生的知识结构与学习能力,合理安排进度。
-
以赛代练,通过考级与比赛,不断提高学生能力。
这是一篇关于C++编程的教程,专注于信息学奥赛中的函数和递归算法应用。文章涵盖了1158到1167等题目,包括求和、斐波那契数列、倒序数、转进制、字符串逆序、阿克曼函数、digit函数、Hermite多项式以及两个不同的f(x,n)求解问题。适合四年级以上对编程有兴趣的学生,提供线上小班教学,旨在培养学生的编程能力和竞赛水平。"
89221782,8056485,使用pandas进行贷款预测:12个数据分析技巧,"['数据分析', 'pandas']
1417

被折叠的 条评论
为什么被折叠?



