全部知识点

第1221题

有如下递归代码

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

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


第1222题

斐波那契数列的定义为: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)


第1223题

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

第1224题

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

第1225题

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

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

第1226题
#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,不会影响程序运行的结果。

第1227题
#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)”,不会影响程序运行的结果。

第1228题
#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)”,不会影响程序运行的结果。

第1229题
#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”。

第1230题
#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”时,输出为( )。

第1231题
#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,这段代码的含义为( )。

第1232题
#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,程序总是会正常执行并输出两行两个相等的数。

第1233题
#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行分别有可能执行两次及以上。

第1234题
#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”。

第1235题
#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)的时间复杂度为( )。

第1236题
#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,solve2(1, n)的时间复杂度为( )。

第1237题
#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,当输入为“10 -3 2 10 0 -8 9 -4 -5 9 4”时,输出的第一行为( )。

第1238题
#include <iostream>
#include <string>
using namespace std;
char base[64];
char table[256];
void init()
{
    for (int i = 0; i < 26; i++) base[i] = 'A' + i;
    for (int i = 0; i < 26; i++) base[26 + i] = 'a' + i;
    for (int i = 0; i < 10; i++) base[52 + i] = '0' + i;
    base[62] = '+', base[63] = '/';
    for (int i = 0; i < 256; i++) table[i] = 0xff;
    for (int i = 0; i < 64; i++) table[base[i]] = i;
    table['='] = 0;
}
string encode(string str)
{
    string ret;
    int i;
    for (i = 0; i + 3 <= str.size(); i += 3) {
        ret += base[str[i] >> 2];
        ret += base[(str[i] & 0x03) << 4 | str[i + 1] >> 4];
        ret += base[(str[i + 1] & 0x0f) << 2 | str[i + 2] >> 6];
        ret += base[str[i + 2] & 0x3f];
    }
    if (i < str.size()) {
        ret += base[str[i] >> 2];
        if (i + 1 == str.size()) {
            ret += base[(str[i] & 0x03) << 4];
            ret += "==";
        }
        else {
            ret += base[(str[i] & 0x03) << 4 | str[i + 1] >> 4];
            ret += base[(str[i + 1] & 0x0f) << 2];
            ret += "=";
        }
    }
    return ret;
}
string decode(string str)
{
    string ret;
    int i;
    for (i = 0; i < str.size(); i += 4) {
        ret += table[str[i]] << 2 | table[str[i + 1]] >> 4;
        if (str[i + 2] != '=')
            ret += (table[str[i + 1]] & 0x0f) << 4 | table[str[i + 2]] >> 2;
        if (str[i + 3] != '=')
            ret += table[str[i + 2]] << 6 | table[str[i + 3]];
    }
    return ret;
}
int main()
{
    init();
    cout << int(table[0]) << endl;
    int opt;
    string str;
    cin >> opt >> str;
    cout << (opt ? decode(str) : encode(str)) << endl;
    return 0;
}

假设输入总是合法的(一个整数和一个不含空白字符的字符串,用空格隔开),程序总是先输出一行一个整数,再输出一行一个字符串。

第1239题
#include <iostream>
#include <string>
using namespace std;
char base[64];
char table[256];
void init()
{
    for (int i = 0; i < 26; i++) base[i] = 'A' + i;
    for (int i = 0; i < 26; i++) base[26 + i] = 'a' + i;
    for (int i = 0; i < 10; i++) base[52 + i] = '0' + i;
    base[62] = '+', base[63] = '/';
    for (int i = 0; i < 256; i++) table[i] = 0xff;
    for (int i = 0; i < 64; i++) table[base[i]] = i;
    table['='] = 0;
}
string encode(string str)
{
    string ret;
    int i;
    for (i = 0; i + 3 <= str.size(); i += 3) {
        ret += base[str[i] >> 2];
        ret += base[(str[i] & 0x03) << 4 | str[i + 1] >> 4];
        ret += base[(str[i + 1] & 0x0f) << 2 | str[i + 2] >> 6];
        ret += base[str[i + 2] & 0x3f];
    }
    if (i < str.size()) {
        ret += base[str[i] >> 2];
        if (i + 1 == str.size()) {
            ret += base[(str[i] & 0x03) << 4];
            ret += "==";
        }
        else {
            ret += base[(str[i] & 0x03) << 4 | str[i + 1] >> 4];
            ret += base[(str[i + 1] & 0x0f) << 2];
            ret += "=";
        }
    }
    return ret;
}
string decode(string str)
{
    string ret;
    int i;
    for (i = 0; i < str.size(); i += 4) {
        ret += table[str[i]] << 2 | table[str[i + 1]] >> 4;
        if (str[i + 2] != '=')
            ret += (table[str[i + 1]] & 0x0f) << 4 | table[str[i + 2]] >> 2;
        if (str[i + 3] != '=')
            ret += table[str[i + 2]] << 6 | table[str[i + 3]];
    }
    return ret;
}
int main()
{
    init();
    cout << int(table[0]) << endl;
    int opt;
    string str;
    cin >> opt >> str;
    cout << (opt ? decode(str) : encode(str)) << endl;
    return 0;
}

假设输入总是合法的(一个整数和一个不含空白字符的字符串,用空格隔开),对于任意不含空白字符的字符串str1,先执行程序输入“0 str1”,得到输出的第二行记为str2;再执行程序输入“1 str2”,输出的第二行必为str1。

第1240题
#include <iostream>
#include <string>
using namespace std;
char base[64];
char table[256];
void init()
{
    for (int i = 0; i < 26; i++) base[i] = 'A' + i;
    for (int i = 0; i < 26; i++) base[26 + i] = 'a' + i;
    for (int i = 0; i < 10; i++) base[52 + i] = '0' + i;
    base[62] = '+', base[63] = '/';
    for (int i = 0; i < 256; i++) table[i] = 0xff;
    for (int i = 0; i < 64; i++) table[base[i]] = i;
    table['='] = 0;
}
string encode(string str)
{
    string ret;
    int i;
    for (i = 0; i + 3 <= str.size(); i += 3) {
        ret += base[str[i] >> 2];
        ret += base[(str[i] & 0x03) << 4 | str[i + 1] >> 4];
        ret += base[(str[i + 1] & 0x0f) << 2 | str[i + 2] >> 6];
        ret += base[str[i + 2] & 0x3f];
    }
    if (i < str.size()) {
        ret += base[str[i] >> 2];
        if (i + 1 == str.size()) {
            ret += base[(str[i] & 0x03) << 4];
            ret += "==";
        }
        else {
            ret += base[(str[i] & 0x03) << 4 | str[i + 1] >> 4];
            ret += base[(str[i + 1] & 0x0f) << 2];
            ret += "=";
        }
    }
    return ret;
}
string decode(string str)
{
    string ret;
    int i;
    for (i = 0; i < str.size(); i += 4) {
        ret += table[str[i]] << 2 | table[str[i + 1]] >> 4;
        if (str[i + 2] != '=')
            ret += (table[str[i + 1]] & 0x0f) << 4 | table[str[i + 2]] >> 2;
        if (str[i + 3] != '=')
            ret += table[str[i + 2]] << 6 | table[str[i + 3]];
    }
    return ret;
}
int main()
{
    init();
    cout << int(table[0]) << endl;
    int opt;
    string str;
    cin >> opt >> str;
    cout << (opt ? decode(str) : encode(str)) << endl;
    return 0;
}

假设输入总是合法的(一个整数和一个不含空白字符的字符串,用空格隔开),当输入为“1 SGVsbG93b3JsZA==”时,输出的第二行为“HelloWorld”。

0.056763s