7-1 求斐波那契数的尾数

7-1 求斐波那契数的尾数

7-2 书籍排序

7-2 书籍排序

7-3 输出素数之和

7-3 输出素数之和

7-4 二分法求多项式单根

#include <cmath>
#include <iostream>
using namespace std;
double a3, a2, a1, a0;
 
double f(double x) { return a3 * x * x * x + a2 * x * x + a1 * x + a0; }
 
double solve(double l, double r) {
    while (r - l > 1e-6) {
        double mid = (l + r) / 2;
        if (f(mid) == 0)
            return mid;
        if (f(l) * f(mid) > 0)
            l = mid;
        else
            r = mid;
    }
    return l;
}
 
int main() {
    cin >> a3 >> a2 >> a1 >> a0;
    double a, b;
    cin >> a >> b;
    printf("%.2lf", solve(a, b));
}

7-5 猴子选大王

7-5 猴子选大王

7-6 出栈序列的合法性

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int m, n, k;
    cin >> m >> n >> k;
    while (k--)
    {
        int a[n];
        stack<int> s;
        int x = 1;
        bool is_right = true;
        for (int j = 0; j < n; j++)
            cin >> a[j];
        for (int j = 0; j < n;)
        {
            while (x <= a[j])
            {
                s.push(x++);
                if (s.size() > m)
                    is_right = false;
            }
            if (s.top() != a[j])
            {
                is_right = false;
                break;
            }
            else
            {
                s.pop();
                j++;
            }
        }
        cout << (is_right ? "YES" : "NO") << endl;
    }
    return 0;
}

7-7 包装机

#include <iostream>
#include <queue>
#include <stack>
using namespace std;
const int N = 100 + 5;
int main() {
    stack<char> s;
    queue<char> q[N];
    string t;
    int n, m, s_max, op;
    cin >> n >> m >> s_max;
    cin.get();
    for (int i = 1; i <= n; i++) {
        getline(cin, t);
        for (char c : t)
            q[i].push(c);
    }
    while (cin >> op && op != -1) {
        if (op == 0) {
            if (!s.empty()) {
                cout << s.top();
                s.pop();
            }
        } else if (!q[op].empty()) {
            if (s.size() >= s_max) {
                cout << s.top();
                s.pop();
            }
            s.push(q[op].front());
            q[op].pop();
        }
    }
    return 0;
}

7-8 完全二叉树的层序遍历

见qq群课件 二叉树讲解.pptx