本站在允许 JavaScript 运行的环境下浏览效果更佳


Hrbust ACM编程练习 20240922 题解分享

57

相关文章链接

本文还在施工中

宣传:24级计算机学院学习交流群

QQ群号:902195227

前言

题单链接:编程练习20240922 @ Hrbust Online Judge

A 题

d 的数据类型要用 double 而不是 float ,否则最后计算的答案会有精度损失。

Cpp Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t > 0) {
        t--;
        int p, w, s;
        double d = 0;
        cin >> p >> w >> s;
        if (s < 250) {
            d = 0;
        } else if (s < 500) {
            d = 0.02;
        } else if (s < 1000) {
            d = 0.05;
        } else if (s < 2000) {
            d = 0.08;
        } else if (s < 3000) {
            d = 0.1;
        } else {
            d = 0.15;
        }
        cout << fixed << setprecision(3) << 1.0 * p * w * s * (1 - d);
        cout << endl;
    }
    return 0;
}

B 题

打表代码

Python Code

l = []
for i in range(-9, 9 + 1):
    temp_l = []
    for _ in range(1, 9 + 1):
        temp_l.append(0)
    l.append(temp_l)

for a in range(-9, 9 + 1):
    for n in range(1, 9 + 1):
        sum = 0
        for le in range(1, n + 1):
            sum += int(le * "1") * a
        l[a + 9][n - 1] = sum
print(l)

以上代码输出

[[-9, -108, -1107, -11106, -111105, -1111104, -11111103, -111111102, -1111111101], [-8, -96, -984, -9872, -98760, -987648, -9876536, -98765424, -987654312], [-7, -84, -861, -8638, -86415, -864192, -8641969, -86419746, -864197523], [-6, -72, -738, -7404, -74070, -740736, -7407402, -74074068, -740740734], [-5, -60, -615, -6170, -61725, -617280, -6172835, -61728390, -617283945], [-4, -48, -492, -4936, -49380, -493824, -4938268, -49382712, -493827156], [-3, -36, -369, -3702, -37035, -370368, -3703701, -37037034, -370370367], [-2, -24, -246, -2468, -24690, -246912, -2469134, -24691356, -246913578], [-1, -12, -123, -1234, -12345, -123456, -1234567, -12345678, -123456789], [0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 12, 123, 1234, 12345, 123456, 1234567, 12345678, 123456789], [2, 24, 246, 2468, 24690, 246912, 2469134, 24691356, 246913578], [3, 36, 369, 3702, 37035, 370368, 3703701, 37037034, 370370367], [4, 48, 492, 4936, 49380, 493824, 4938268, 49382712, 493827156], [5, 60, 615, 6170, 61725, 617280, 6172835, 61728390, 617283945], [6, 72, 738, 7404, 74070, 740736, 7407402, 74074068, 740740734], [7, 84, 861, 8638, 86415, 864192, 8641969, 86419746, 864197523], [8, 96, 984, 9872, 98760, 987648, 9876536, 98765424, 987654312], [9, 108, 1107, 11106, 111105, 1111104, 11111103, 111111102, 1111111101]]

用任意文本编辑器将[替换为{]替换为}即可直接使用

AC 代码

Cpp Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int ans[][9] = {
    {-9, -108, -1107, -11106, -111105, -1111104, -11111103, -111111102,
     -1111111101},
    {-8, -96, -984, -9872, -98760, -987648, -9876536, -98765424, -987654312},
    {-7, -84, -861, -8638, -86415, -864192, -8641969, -86419746, -864197523},
    {-6, -72, -738, -7404, -74070, -740736, -7407402, -74074068, -740740734},
    {-5, -60, -615, -6170, -61725, -617280, -6172835, -61728390, -617283945},
    {-4, -48, -492, -4936, -49380, -493824, -4938268, -49382712, -493827156},
    {-3, -36, -369, -3702, -37035, -370368, -3703701, -37037034, -370370367},
    {-2, -24, -246, -2468, -24690, -246912, -2469134, -24691356, -246913578},
    {-1, -12, -123, -1234, -12345, -123456, -1234567, -12345678, -123456789},
    {0, 0, 0, 0, 0, 0, 0, 0, 0},
    {1, 12, 123, 1234, 12345, 123456, 1234567, 12345678, 123456789},
    {2, 24, 246, 2468, 24690, 246912, 2469134, 24691356, 246913578},
    {3, 36, 369, 3702, 37035, 370368, 3703701, 37037034, 370370367},
    {4, 48, 492, 4936, 49380, 493824, 4938268, 49382712, 493827156},
    {5, 60, 615, 6170, 61725, 617280, 6172835, 61728390, 617283945},
    {6, 72, 738, 7404, 74070, 740736, 7407402, 74074068, 740740734},
    {7, 84, 861, 8638, 86415, 864192, 8641969, 86419746, 864197523},
    {8, 96, 984, 9872, 98760, 987648, 9876536, 98765424, 987654312},
    {9, 108, 1107, 11106, 111105, 1111104, 11111103, 111111102, 1111111101}};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t > 0) {
        t--;
        int a, n;
        cin >> a >> n;
        cout << ans[a + 9][n - 1];
        cout << endl;
    }
    return 0;
}

C 题

打表代码

Python Code

for i in range(1000):
    if (i**2) <= 1000000:
        if str(i**2).endswith(str(i)):
            print(i**2, end=",")

以上代码输出

0,1,25,36,625,5776,141376,390625,

AC 代码

Cpp Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int ans[] = {0, 1, 25, 36, 625, 5776, 141376, 390625};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t > 0) {
        t--;
        int a, b;
        cin >> a >> b;
        bool flag = true;
        for (int number : ans) {
            if (number <= b && number >= a) {
                // 避免输出多余空格的设计
                if (flag) {
                    cout << number;
                    flag = false;
                } else {
                    cout << " " << number;
                }
            }
        }
        cout << endl;
    }
    return 0;
}

D 题

Cpp Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    while (n > 0) {
        n--;
        int a, b;
        cin >> a >> b;
        vector<int> l;
        bool start_flag = true;
        for (int i = a; i <= b; i++) {
            if (i % 3 != 0) {
                if (start_flag) {
                    cout << i;
                    start_flag = false;
                    continue;
                }
                cout << " " << i;
            }
        }
        cout << endl;
    }

    return 0;
}

E 题

Cpp Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t > 0) {
        t--;
        int a;
        cin >> a;
        int max_line = 2 * a - 1;
        for (int i = 1; i <= max_line; i += 2) {
            for (int ii = 0; ii <= ((max_line - i) / 2) - 1; ii++) {
                cout << " ";
            }
            for (int iii = 0; iii <= i - 1; iii++) {
                cout << "*";
            }

            cout << endl;
        }
        for (int i = max_line; i > 1; i -= 2) {
            for (int ii = (max_line - i) / 2 + 1; ii > 0; ii--) {
                cout << " ";
            }
            for (int iii = (i - 1) - 1; iii > 0; iii--) {
                cout << "*";
            }

            cout << endl;
        }
    }
    return 0;
}

F 题

Cpp Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t > 0) {
        t--;
        int a;
        cin >> a;
        int max_line = 2 * a + 1;
        for (int i = 1; i <= max_line; i += 2) {
            int c = (i + 1) / 2;
            for (int ii = 0; ii <= ((max_line - i) / 2) - 1; ii++) {
                cout << " ";
            }
            bool flag = false;
            for (int iii = 0; iii <= i - 1; iii++) {
                cout << c;
                if (c > 1) {
                    if (flag) {
                        c++;
                    } else {
                        c--;
                    }
                } else if (c == 1) {
                    c++;
                    flag = true;
                }
            }

            cout << endl;
        }
        for (int i = max_line; i > 1; i -= 2) {
            int c = (i - 2 + 1) / 2;
            for (int ii = (max_line - i) / 2 + 1; ii > 0; ii--) {
                cout << " ";
            }
            bool flag = false;
            for (int iii = i - 2; iii > 0; iii--) {
                cout << c;
                if (c > 1) {
                    if (flag) {
                        c++;
                    } else {
                        c--;
                    }
                } else if (c == 1) {
                    c++;
                    flag = true;
                }
            }

            cout << endl;
        }
    }
    return 0;
}

G 题

Cpp Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t > 0) {
        t--;
        int o_a, o_b, max_number = 0, ans_x = 0, ans_y = 0, a = 0;
        cin >> o_a >> o_b;
        while (a < o_a) {
            a++;
            int b = 0;
            while (b < o_b) {
                b++;
                int temp;
                cin >> temp;
                if (temp > max_number) {
                    max_number = temp;
                    ans_x = a;
                    ans_y = b;
                }
            }
        }
        cout << ans_x << " " << ans_y << endl;
    }
    return 0;
}

H 题

Cpp Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t, n, x;
    cin >> t;
    while (t > 0) {
        cin >> n >> x;
        t--;
        bool flag = true, flag2 = true;
        while (n > 0) {
            n--;
            int temp;
            cin >> temp;
            if (temp >= x && flag2) {
                if (flag) {
                    cout << x << " " << temp;
                    flag = false;
                } else {
                    cout << " " << x << " " << temp;
                }
                flag2 = false;
            } else {
                if (flag) {
                    cout << temp;
                    flag = false;
                } else {
                    cout << " " << temp;
                }
            }
        }
        if (flag2) {
            cout << " " << x;
        }
        cout << endl;
    }
    return 0;
}

I 题

Cpp Code

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    cin.ignore();
    while (t > 0) {
        t--;
        string s;
        int letter = 0, digit = 0, space = 0, others = 0;

        getline(cin, s);
        for (char c : s) {
            if (isupper(c) or islower(c)) {
                letter++;
            } else if (isdigit(c)) {
                digit++;
            } else if (isspace(c)) {
                space++;
            } else {
                others++;
            }
        }

        printf("%d %d %d %d\n", letter, space, digit, others);
    }
    return 0;
}

J 题

Cpp Code

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    cin.ignore();
    while (t > 0) {
        t--;
        string s;
        int word = 0;
        bool last_one = false;

        getline(cin, s);
        bool flag = false;
        for (char c : s) {
            if (isspace(c)) {
                last_one = flag;
                flag = false;
            } else {  //  非空
                last_one = flag;
                flag = true;
            }
            if (flag && last_one == false) {
                word++;
            }
        }

        printf("%d\n", word);
    }
    return 0;
}

K 题

Cpp Code

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    cin.ignore();
    while (t > 0) {
        t--;
        string s;
        int line = 3;
        int upper = 0, lower = 0, digit = 0, space = 0, others = 0;

        while (line > 0) {
            line--;
            getline(cin, s);
            for (char c : s) {
                if (isupper(c)) {
                    upper++;
                } else if (islower(c)) {
                    lower++;
                } else if (isdigit(c)) {
                    digit++;
                } else if (isspace(c)) {
                    space++;
                } else {
                    others++;
                }
            }
        }
        printf("%d %d %d %d %d\n", upper, lower, digit, space, others);
    }
    return 0;
}

L 题

Cpp Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t > 0) {
        t--;
        int n;
        cin >> n;
        int ret = 0;
        while (n >= 5) {
            n /= 5;
            ret += n;
        }
        cout << ret << endl;
    }
    return 0;
}

M 题

约瑟夫问题 - OI Wiki (oi-wiki.org)

Cpp Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int josephus(int n, int k) {
    int res = 0;
    for (int i = 2; i <= n; ++i) {
        res = (res + k) % i;
    }
    return res;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t > 0) {
        t--;
        int M, K;
        cin >> M >> K;
        // 约瑟夫环公式使用示例
        int i;
        for (i = 1; i <= M; i++) {
            if (josephus(M, i) + 1 == K) {
                cout << i << "\n";
                break;
            };
        }
        if (i > M) {
            cout << "No Solution!\n";
        }
    }

    return 0;
}

N 题

一开始 AC 的代码,因为之前的代码 TL 了
Cpp Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

vector<int> prime = {
    2,   3,   5,   7,   11,  13,  17,  19,  23,  29,  31,  37,  41,  43,
    47,  53,  59,  61,  67,  71,  73,  79,  83,  89,  97,  101, 103, 107,
    109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181,
    191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263,
    269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349,
    353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433,
    439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521,
    523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613,
    617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701,
    709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809,
    811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887,
    907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t > 0) {
        t--;
        ll n;
        cin >> n;
        cout << n << "=";
        bool flag = true;
        for (int p_number : prime) {
            while (n % p_number == 0) {
                n /= p_number;
                if (flag) {
                    cout << p_number;
                    flag = false;
                } else {
                    cout << "*" << p_number;
                }
            }
        }
        int divisor = 1021;
        while (n > 1) {
            while (n % divisor == 0) {
                if (flag) {
                    cout << divisor;
                    flag = false;
                } else {
                    cout << "*" << divisor;
                }
                n /= divisor;
                prime.push_back(divisor);
                divisor = prime.back();
            }
            divisor++;
        }
        cout << endl;
    }

    return 0;
}

事实是我想多了,这样也能 AC。之前 TL 的代码找不到了,也要成未解之谜了。
Cpp Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t > 0) {
        t--;
        int n;
        cin >> n;
        cout << n << "=";
        bool flag = true;
        int divisor = 2;
        while (n > 1) {
            while (n % divisor == 0) {
                if (flag) {
                    cout << divisor;
                    flag = false;
                } else {
                    cout << "*" << divisor;
                }
                n /= divisor;
                divisor = 2;
            }
            divisor++;
        }
        cout << endl;
    }
    return 0;
}

O 题

Cpp Code


P 题

Cpp Code


Q 题

Cpp Code


R 题

Cpp Code