Informatics Olympiad One Pass 1.6: Functions (2)

​The first part of C++ language

Chapter 6 Functions

Section 2 Recursive Algorithm

1158 Find 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 Fibonacci Sequence
#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 Reversed number
#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 Conversion
#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 string reverse order
#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 function
#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 function
#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 polynomial
#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 Find 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 Find f(x,n) again
#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;
}

If your child is in the fourth grade or above, is interested in computer programming and has spare capacity in cultural lessons, please contact customer service (WeChat ID: xiaolan7321) to participate in informatics learning. We are professional informatics competition coaches. We use online small class teaching methods. The goal is to help primary and middle school students who love programming to achieve excellent results in informatics competitions at home and abroad.

Teaching features:

  • Online small class teaching, lay a good code foundation. Avoid the problem that students in large classes are either "can't keep up" or "not enough to eat".

  • Rich teaching experience, familiar with students' knowledge structure and learning ability, and arrange the schedule reasonably.

  • Practice with competitions, and continuously improve students' abilities through examinations and competitions.

Guess you like

Origin blog.csdn.net/davidliule/article/details/106139662