CSP考试

第221题

现有一个地址区间为0~10的哈希表,对于出现冲突情况,会往后找第一个空的地址存储(到10冲突了就从0开始往后),现在要依次存储(0,1,2,3,4,5,6,7),哈希函数为h(x)=xmod 11。请问7存储在哈希表哪个地址中( )。

第222题

G是一个非连通简单无向图(没有自环和重边),共有36条边,则该图至少有( )个点。

第223题

令根结点的高度为1,则一棵含有2021个结点的二叉树的高度至少为( )。

第224题

前序遍历和中序遍历相同的二叉树为且仅为( )。

第225题

定义一种字符串操作为交换相邻两个字符。将“DACFEB”变为 “ABCDEF”最少需要( )次上述操作。

第226题

有如下递归代码

solve(t, n)
    if t = 1 return 1
    else return 5 * solve(t - 1, n) mod n

则solve(23, 23)的结果为( )。


第227题

斐波那契数列的定义为:F1=1,F2=1,Fn=Fn-1+Fn-2(n>=3)。现在用如下程序来计算斐波那契数列的第n项,其时间复杂度为( )。

F(n):
    if n <= 2 return 1
    else return F(n - 1) + F(n - 2)


第228题

有8个苹果从左到右排成一排,你要从中挑选至少一个苹果,并且不能同时挑选相邻的两个苹果,一共有( )种方案。

第229题

设一个三位数CSP-S1提高级初赛试卷[2021]均为1∼9之间的整数,若以a、b、c作为三角形的三条边可以构成等腰三角形(包括等边),则这样的n有( )个。

第230题

有如下的有向图,节点为A,B,⋯,J,其中每条边的长度都标在图中。则节点A到节点J的最短路径长度为( )。

CSP-S1提高级初赛试卷[2021]

第231题
#include <iostream>
#include <cmath>
using namespace std;
const double r = acos(0.5);
int a1, b1, c1, d1;
int a2, b2, c2, d2;
inline int sq(const int x) { return x * x; }
inline int cu(const int x) { return x * x * x; }
int main()
{
    cout.flags(ios::fixed);
    cout.precision(4);
    cin >> a1 >> b1 >> c1 >> d1;
    cin >> a2 >> b2 >> c2 >> d2;
    int t = sq(a1 - a2) + sq(b1 - b2) + sq(c1 - c2);
    if (t <= sq(d2 - d1)) cout << cu(min(d1, d2)) * r * 4;
    else if (t >= sq(d2 + d1)) cout << 0;
        else {
            double x = d1 - (sq(d1) - sq(d2) + t) / sqrt(t) / 2;
            double y = d2 - (sq(d2) - sq(d1) + t) / sqrt(t) / 2;
            cout << (x * x * (3 * d1 - x) + y * y * (3 * d2 - y)) * r;
        }
    cout << endl;
    return 0;
}

假设输入的所有数的绝对值都不超过1000,将第21行中t的类型声明从int改为double,不会影响程序运行的结果。

第232题
#include <iostream>
#include <cmath>
using namespace std;
const double r = acos(0.5);
int a1, b1, c1, d1;
int a2, b2, c2, d2;
inline int sq(const int x) { return x * x; }
inline int cu(const int x) { return x * x * x; }
int main()
{
    cout.flags(ios::fixed);
    cout.precision(4);
    cin >> a1 >> b1 >> c1 >> d1;
    cin >> a2 >> b2 >> c2 >> d2;
    int t = sq(a1 - a2) + sq(b1 - b2) + sq(c1 - c2);
    if (t <= sq(d2 - d1)) cout << cu(min(d1, d2)) * r * 4;
    else if (t >= sq(d2 + d1)) cout << 0;
        else {
            double x = d1 - (sq(d1) - sq(d2) + t) / sqrt(t) / 2;
            double y = d2 - (sq(d2) - sq(d1) + t) / sqrt(t) / 2;
            cout << (x * x * (3 * d1 - x) + y * y * (3 * d2 - y)) * r;
        }
    cout << endl;
    return 0;
}

假设输入的所有数的绝对值都不超过1000,将第26、27行中的“/ sqrt(t) / 2”替换为“/ 2 / sqrt(t)”,不会影响程序运行的结果。

第233题
#include <iostream>
#include <cmath>
using namespace std;
const double r = acos(0.5);
int a1, b1, c1, d1;
int a2, b2, c2, d2;
inline int sq(const int x) { return x * x; }
inline int cu(const int x) { return x * x * x; }
int main()
{
    cout.flags(ios::fixed);
    cout.precision(4);
    cin >> a1 >> b1 >> c1 >> d1;
    cin >> a2 >> b2 >> c2 >> d2;
    int t = sq(a1 - a2) + sq(b1 - b2) + sq(c1 - c2);
    if (t <= sq(d2 - d1)) cout << cu(min(d1, d2)) * r * 4;
    else if (t >= sq(d2 + d1)) cout << 0;
        else {
            double x = d1 - (sq(d1) - sq(d2) + t) / sqrt(t) / 2;
            double y = d2 - (sq(d2) - sq(d1) + t) / sqrt(t) / 2;
            cout << (x * x * (3 * d1 - x) + y * y * (3 * d2 - y)) * r;
        }
    cout << endl;
    return 0;
}

假设输入的所有数的绝对值都不超过1000,将第28行中的“x * x”改成“sq(x)”,“y * y”改成“sq(y)”,不会影响程序运行的结果。

第234题
#include <iostream>
#include <cmath>
using namespace std;
const double r = acos(0.5);
int a1, b1, c1, d1;
int a2, b2, c2, d2;
inline int sq(const int x) { return x * x; }
inline int cu(const int x) { return x * x * x; }
int main()
{
    cout.flags(ios::fixed);
    cout.precision(4);
    cin >> a1 >> b1 >> c1 >> d1;
    cin >> a2 >> b2 >> c2 >> d2;
    int t = sq(a1 - a2) + sq(b1 - b2) + sq(c1 - c2);
    if (t <= sq(d2 - d1)) cout << cu(min(d1, d2)) * r * 4;
    else if (t >= sq(d2 + d1)) cout << 0;
        else {
            double x = d1 - (sq(d1) - sq(d2) + t) / sqrt(t) / 2;
            double y = d2 - (sq(d2) - sq(d1) + t) / sqrt(t) / 2;
            cout << (x * x * (3 * d1 - x) + y * y * (3 * d2 - y)) * r;
        }
    cout << endl;
    return 0;
}

假设输入的所有数的绝对值都不超过1000,当输入为“0 0 0 1 1 0 0 1”时,输出为“1.3090”。

第235题
#include <iostream>
#include <cmath>
using namespace std;
const double r = acos(0.5);
int a1, b1, c1, d1;
int a2, b2, c2, d2;
inline int sq(const int x) { return x * x; }
inline int cu(const int x) { return x * x * x; }
int main()
{
    cout.flags(ios::fixed);
    cout.precision(4);
    cin >> a1 >> b1 >> c1 >> d1;
    cin >> a2 >> b2 >> c2 >> d2;
    int t = sq(a1 - a2) + sq(b1 - b2) + sq(c1 - c2);
    if (t <= sq(d2 - d1)) cout << cu(min(d1, d2)) * r * 4;
    else if (t >= sq(d2 + d1)) cout << 0;
        else {
            double x = d1 - (sq(d1) - sq(d2) + t) / sqrt(t) / 2;
            double y = d2 - (sq(d2) - sq(d1) + t) / sqrt(t) / 2;
            cout << (x * x * (3 * d1 - x) + y * y * (3 * d2 - y)) * r;
        }
    cout << endl;
    return 0;
}

假设输入的所有数的绝对值都不超过1000,当输入为“1 1 1 1 1 1 1 2”时,输出为( )。

第236题
#include <iostream>
#include <cmath>
using namespace std;
const double r = acos(0.5);
int a1, b1, c1, d1;
int a2, b2, c2, d2;
inline int sq(const int x) { return x * x; }
inline int cu(const int x) { return x * x * x; }
int main()
{
    cout.flags(ios::fixed);
    cout.precision(4);
    cin >> a1 >> b1 >> c1 >> d1;
    cin >> a2 >> b2 >> c2 >> d2;
    int t = sq(a1 - a2) + sq(b1 - b2) + sq(c1 - c2);
    if (t <= sq(d2 - d1)) cout << cu(min(d1, d2)) * r * 4;
    else if (t >= sq(d2 + d1)) cout << 0;
        else {
            double x = d1 - (sq(d1) - sq(d2) + t) / sqrt(t) / 2;
            double y = d2 - (sq(d2) - sq(d1) + t) / sqrt(t) / 2;
            cout << (x * x * (3 * d1 - x) + y * y * (3 * d2 - y)) * r;
        }
    cout << endl;
    return 0;
}

假设输入的所有数的绝对值都不超过1000,这段代码的含义为( )。

第237题
#include <algorithm>
#include <iostream>
using namespace std;
int n, a[1005];
struct Node
{
    int h, j, m, w;
    Node(const int _h, const int _j, const int _m, const int _w):
        h(_h), j(_j), m(_m), w(_w)
    { }
    Node operator+ (const Node &o) const
    {
        return Node(
            max(h, w + o.h),
            max(max(j, o.j), m + o.h),
            max(m + o.w, o.m),
            w + o.w);
    }
};
Node solve1(int h, int m)
{
    if (h > m)
        return Node(-1, -1, -1, -1);
    if (h == m)
        return Node(max(a[h], 0), max(a[h], 0), max(a[h], 0), a[h]);
    int j = (h + m) >> 1;
    return solve1(h, j) + solve1(j + 1, m);
}
int solve2(int h, int m)
{
    if (h > m)
        return -1;
    if (h == m)
        return max(a[h], 0);
    int j = (h + m) >> 1;
    int wh = 0, wm = 0;
    int wht = 0, wmt = 0;
    for (int i = j; i >= h; i--) {
        wht += a[i];
        wh = max(wh, wht);
    }
    for (int i = j + 1; i <= m; i++) {
        wmt += a[i];
        wm = max(wm, wmt);
    }
    return max(max(solve2(h, j), solve2(j + 1, m)), wh + wm);
}
int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];
    cout << solve1(1, n).j << endl;
    cout << solve2(1, n) << endl;
    return 0;
}

假设输入的所有数的绝对值都不超过1000,程序总是会正常执行并输出两行两个相等的数。

第238题
#include <algorithm>
#include <iostream>
using namespace std;
int n, a[1005];
struct Node
{
    int h, j, m, w;
    Node(const int _h, const int _j, const int _m, const int _w):
        h(_h), j(_j), m(_m), w(_w)
    { }
    Node operator+ (const Node &o) const
    {
        return Node(
            max(h, w + o.h),
            max(max(j, o.j), m + o.h),
            max(m + o.w, o.m),
            w + o.w);
    }
};
Node solve1(int h, int m)
{
    if (h > m)
        return Node(-1, -1, -1, -1);
    if (h == m)
        return Node(max(a[h], 0), max(a[h], 0), max(a[h], 0), a[h]);
    int j = (h + m) >> 1;
    return solve1(h, j) + solve1(j + 1, m);
}
int solve2(int h, int m)
{
    if (h > m)
        return -1;
    if (h == m)
        return max(a[h], 0);
    int j = (h + m) >> 1;
    int wh = 0, wm = 0;
    int wht = 0, wmt = 0;
    for (int i = j; i >= h; i--) {
        wht += a[i];
        wh = max(wh, wht);
    }
    for (int i = j + 1; i <= m; i++) {
        wmt += a[i];
        wm = max(wm, wmt);
    }
    return max(max(solve2(h, j), solve2(j + 1, m)), wh + wm);
}
int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];
    cout << solve1(1, n).j << endl;
    cout << solve2(1, n) << endl;
    return 0;
}

假设输入的所有数的绝对值都不超过1000,第28行与第38行分别有可能执行两次及以上。

第239题
#include <algorithm>
#include <iostream>
using namespace std;
int n, a[1005];
struct Node
{
    int h, j, m, w;
    Node(const int _h, const int _j, const int _m, const int _w):
        h(_h), j(_j), m(_m), w(_w)
    { }
    Node operator+ (const Node &o) const
    {
        return Node(
            max(h, w + o.h),
            max(max(j, o.j), m + o.h),
            max(m + o.w, o.m),
            w + o.w);
    }
};
Node solve1(int h, int m)
{
    if (h > m)
        return Node(-1, -1, -1, -1);
    if (h == m)
        return Node(max(a[h], 0), max(a[h], 0), max(a[h], 0), a[h]);
    int j = (h + m) >> 1;
    return solve1(h, j) + solve1(j + 1, m);
}
int solve2(int h, int m)
{
    if (h > m)
        return -1;
    if (h == m)
        return max(a[h], 0);
    int j = (h + m) >> 1;
    int wh = 0, wm = 0;
    int wht = 0, wmt = 0;
    for (int i = j; i >= h; i--) {
        wht += a[i];
        wh = max(wh, wht);
    }
    for (int i = j + 1; i <= m; i++) {
        wmt += a[i];
        wm = max(wm, wmt);
    }
    return max(max(solve2(h, j), solve2(j + 1, m)), wh + wm);
}
int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];
    cout << solve1(1, n).j << endl;
    cout << solve2(1, n) << endl;
    return 0;
}

假设输入的所有数的绝对值都不超过1000,当输入为“5 -10 11 -9 5 -7”时,输出的第二行为“7”。

第240题
#include <algorithm>
#include <iostream>
using namespace std;
int n, a[1005];
struct Node
{
    int h, j, m, w;
    Node(const int _h, const int _j, const int _m, const int _w):
        h(_h), j(_j), m(_m), w(_w)
    { }
    Node operator+ (const Node &o) const
    {
        return Node(
            max(h, w + o.h),
            max(max(j, o.j), m + o.h),
            max(m + o.w, o.m),
            w + o.w);
    }
};
Node solve1(int h, int m)
{
    if (h > m)
        return Node(-1, -1, -1, -1);
    if (h == m)
        return Node(max(a[h], 0), max(a[h], 0), max(a[h], 0), a[h]);
    int j = (h + m) >> 1;
    return solve1(h, j) + solve1(j + 1, m);
}
int solve2(int h, int m)
{
    if (h > m)
        return -1;
    if (h == m)
        return max(a[h], 0);
    int j = (h + m) >> 1;
    int wh = 0, wm = 0;
    int wht = 0, wmt = 0;
    for (int i = j; i >= h; i--) {
        wht += a[i];
        wh = max(wh, wht);
    }
    for (int i = j + 1; i <= m; i++) {
        wmt += a[i];
        wm = max(wm, wmt);
    }
    return max(max(solve2(h, j), solve2(j + 1, m)), wh + wm);
}
int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];
    cout << solve1(1, n).j << endl;
    cout << solve2(1, n) << endl;
    return 0;
}

假设输入的所有数的绝对值都不超过1000,solve1(1, n)的时间复杂度为( )。