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


Hrbust ACM练习 2024级第3周题单 题解分享

104

相关文章链接

前言

题单链接:2024级第3周题单 - Virtual Judge (vjudge.net)
有些题不能提交 Python 代码,所以只有 Cpp 代码。

我尝试找了找这些题的出处,搜索引擎中搜到的题解大多比笔者写得更好,所以建议直接检索对应题目的题解。

  • A 题是 SWUSTOJ 1178
  • B 题是 零起点学算法106
  • C 题是 水仙花数,很多 oj 都有,比较常见。但是这个变体没搜到,可能是原创题。
  • D 题是 HDU 2016
  • E 题是 HDU 2017
  • F 题是 HDU 2043
  • G 题是 HDU 1062
  • H 题是 HDU 2020
  • I 题是 51Nod 3212
  • J 题是 计蒜客-T1715
  • K 题是 计蒜客-T1232

A 题

Python Code

s = input()
for c in s:
    if "a" <= c <= "y" or "A" <= c <= "Y":
        print(chr(ord(c) + 1), end="")
    elif c == "z":
        print("a", end="")
    elif c == "Z":
        print("A", end="")
    else:
        print(c, end="")

Cpp Code

#include <bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    getline(cin, s);
    for (char c : s)
    {
        if ('a' <= c && c <= 'y')
        {
            cout << char(c + 1);
        }
        else if ('A' <= c && c <= 'Y')
        {
            cout << char(c + 1);
        }
        else if (c == 'z')
        {
            cout << 'a';
        }
        else if (c == 'Z')
        {
            cout << 'A';
        }
        else
        {
            cout << c;
        }
    }
    cout << " ";
    return 0;
}

B 题

Python Code

s = input().split()
rt_s = []
for i in s:
    rt_s.append(i.capitalize())
print(" ".join(rt_s))

Cpp Code

#include <bits/stdc++.h>
using namespace std;
int main()
{
    string word;
    while (cin >> word)
    {
        word[0] = toupper(word[0]);
        cout << word << " ";
    }
}

C 题

这题 水仙花数 很常见,很多 oj 都有类似的

Cpp Code

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int a, b, yesorno;
    while (cin >> a >> b)
    {
        yesorno = 0;
        for (int i = a; i <= b; i++)
        {
            if (pow((i / 100), 3) + pow((i / 10 % 10), 3) + pow((i % 10), 3) == i)
            {
                cout << i << " ";
                yesorno = 1;
            }
        }
        if (yesorno == 0)
        {
            cout << "no" << endl;
        } else{
            cout << endl;
        }
    }
}

D 题

Cpp Code

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n, b;
    while (cin >> n)
    {
        if (n == 0)
        {
            return 0;
        }

        list<int> int_list;
        while (n > 0)
        {
            n--;
            cin >> b;
            int_list.push_back(b);
        }

        list<int> new_int_list = int_list;
        new_int_list.sort();
        int min_number = new_int_list.front();

        list<int>::iterator it = find(int_list.begin(), int_list.end(), min_number);

        int index;
        if (it != int_list.end())
        {
            index = distance(int_list.begin(), it);
        }

        auto it2 = next(int_list.begin(), index);

        *it2 = *int_list.begin();
        *int_list.begin() = min_number;

        while (int_list.size() != 0)
        {
            cout << int_list.front() << " ";
            int_list.pop_front();
        }
        cout << endl;
    }
}

E 题

Cpp Code

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin >> n;
    while (n > 0)
    {
        n--;
        string s;
        int ret = 0;
        cin >> s;
        for (char c : s)
        {
            if (isdigit(c))
            {
                ret++;
            }
        }
        cout << ret;
    }
}

F 题

Cpp Code

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

bool check(string s)
{
    bool upper = false, lower = false, digit = false, special = false;

    for (char c : s)
    {
        if (isupper(c))
        {
            upper = true;
            continue;
        }
        else if (islower(c))
        {
            lower = true;
            continue;
        }
        else if (isdigit(c))
        {
            digit = true;
            continue;
        }
        else if (c == '~' or c == '!' or c == '@' or c == '#' or c == '$' or c == '%' or c == '^')
        {
            special = true;
            continue;
        }
    }

    if (upper + lower + digit + special >= 3)
    {
        return true;
    }
    else
    {
        return false;
    }
}

int main()
{
    int n;
    cin >> n;
    while (n > 0)
    {
        n--;
        string s;
        cin >> s;
        if (s.size() < 8 or s.size() > 16)
        {
            cout << "NO" << endl;
            continue;
        }

        if (check(s))
        {
            cout << "YES" << endl;
            continue;
        }
        else
        {
            cout << "NO" << endl;
            continue;
        }
    }
    return 0;
}

G 题

Cpp Code

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin >> n;
    cin.get();
    while (n > 0)
    {
        n--;
        string str;
        list<char> char_list;
        list<char> char_list_r;
        getline(cin, str);
        for (char c : str)
        {
            if (c == ' ')
            {
                for (auto const &i : char_list_r)
                {
                    cout << i;
                }
                char_list_r.clear();
                cout << ' ';
                continue;
            }
            char_list_r.push_front(c);
        }
        for (auto const &i : char_list_r)
        {
            cout << i;
        }
        char_list_r.clear();
        cout << endl;
    }
}

H 题

Cpp Code

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

bool cmp(const int a, const int b)
{
    return abs(a) > abs(b);
}

int main()
{
    int n, b;
    while (cin >> n)
    {
        if (n == 0)
        {
            return 0;
        }
        list<int> int_list;
        while (n > 0)
        {
            n--;
            cin >> b;
            int_list.push_back(b);
        }
        int_list.sort(cmp);
        for (auto const &i : int_list)
        {
            cout << i << " ";
        }
        cout << endl;
    }
}

I 题

Python Code

n = int(input())
l = []
while True:
    if n == 0:
        break
    l.append(str(n % 10))
    n //= 10
min_n = sorted(l)
if min_n[0] == "0":
    for index, n in enumerate(min_n):
        if n != "0":
            min_n[0] = min_n[index]
            min_n[index] = "0"
            break
print("".join(sorted(l, reverse=True)), "".join(min_n))

Cpp Code

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

int main() {
    long long n;
    cin >> n;
    list<long long> int_list;
    list<long long> int_list_r;
    while (true) {
        if (n == 0) {
            break;
        }
        int_list.push_back(n % 10);
        n /= 10;
    }
    int_list.sort();

    for (long long n : int_list) {
        int_list_r.push_front(n);
    }

    for (auto const &i : int_list_r) {
        cout << i;
    }
    cout << " ";

    while (int_list.front() == 0) {
        auto it = int_list.begin();
        while (*(it++) == 0) {
            continue;
        }
        int_list.emplace(it, 0);
        int_list.pop_front();
    }
    for (auto const &i : int_list) {
        cout << i;
    }
}

J 题

这题奇怪的是思路一实现一只需要开 99 大小的数组就能过,而思路二实现二需要开 100 大小的数组才能过。
思路一实现二不知道为什么不能过任何一个测试点,如果在阅读这段文字的大佬您知道怎么解决,欢迎在评论区深入交流!

思路一实现一 Cpp Code(关键:用 scanf 写入 char[] 无需取址、用 strcpy 写入结构体 char[])

#include <bits/stdc++.h>
using namespace std;
struct T
{
    char name[21];
    long long order;
} tl[99];

bool cmp(T a, T b)
{
    return a.order < b.order;
}

int main()
{
    int n;
    cin >> n;

    for (int i = 0; i < n; i++)
    {
        char name[21];
        long long y, m, d, input_order;
        scanf("%s %lld %lld %lld", name, &y, &m, &d);

        input_order = n - i;

        strcpy(tl[i].name, name);
        tl[i].order = y * 10000000 + m * 100000 + d * 1000 + input_order;
    }

    sort(tl, tl + n, cmp);

    for (int i = 0; i < n; i++)
    {
        cout << tl[i].name << endl;
    }

    return 0;
}

思路一实现二 Cpp Code 另一种实现 但是没有过线上测试(关键:结构体用 string、scanf 写入 string 的方法)

#include <bits/stdc++.h>
using namespace std;
struct T
{
    string name;
    long long order;
} tl[100];

bool cmp(T a, T b)
{
    return a.order < b.order;
}

int main()
{
    int n;
    cin >> n;

    for (int i = 0; i < n; i++)
    {
        string name;
        name.resize(21);
        long long y, m, d, input_order;
        scanf("%s %lld %lld %lld", &name[0], &y, &m, &d);

        input_order = n - i;

        tl[i].name = name;
        tl[i].order = y * 10000000 + m * 100000 + d * 1000 + input_order;
    }

    sort(tl, tl + n, cmp);

    for (int i = 0; i < n; i++)
    {
        cout << tl[i].name << endl;
    }

    return 0;
}

思路二实现一 Cpp Code 另一种实现(关键:cin 直接写入结构体 string)

#include <bits/stdc++.h>
using namespace std;
struct T
{
    long long y, m, d, order;
    string name;
} tl[100];

bool cmp(T n1, T n2)
{
    if (n1.y != n2.y)
    {
        return n1.y < n2.y;
    }
    if (n1.m != n2.m)
    {
        return n1.m < n2.m;
    }
    if (n1.d != n2.d)
    {
        return n1.d < n2.d;
    }
    return n1.order > n2.order;
}

int main()
{
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> tl[i].name >> tl[i].y >> tl[i].m >> tl[i].d;
        tl[i].order = i;
    }

    sort(tl, tl + n, cmp);

    for (int i = 0; i < n; i++)
    {
        cout << tl[i].name << endl;
    }

    return 0;
}

K 题

Python Code

t = input()
n = int(input())
l = [input() for _ in range(n)]

if t == "inc":
    l.sort()
elif t == "dec":
    l.sort(reverse=True)
elif t == "ncinc":
    l.sort(key=lambda s: s.lower())
else:
    l.sort(key=lambda s: s.lower(), reverse=True)

for s in l:
    print(s)

Cpp Code

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

string tolower_string(const string &s)
{
    string lower_s = s;
    transform(lower_s.begin(), lower_s.end(), lower_s.begin(), ::tolower);
    return lower_s;
}

bool cicmp(const string &m, const string &n) // case insensitive compare
{
    return tolower_string(m) < tolower_string(n);
}

bool cicmpr(const string &m, const string &n)
{
    return tolower_string(m) > tolower_string(n);
}

int main()
{
    string t;
    int n;

    cin >> t;
    cin >> n;

    vector<string> l(n);
    for (int i = 0; i < n; ++i)
    {
        cin >> l[i];
    }

    if (t == "inc")
    {
        sort(l.begin(), l.end());
        // sort(l.begin(), l.end(), less<string>());
    }
    else if (t == "dec")
    {
        sort(l.begin(), l.end(), greater<string>());
    }
    else if (t == "ncinc")
    {
        sort(l.begin(), l.end(), cicmp);
    }
    else
    {
        sort(l.begin(), l.end(), cicmpr);
    }

    for (const string &s : l)
    {
        cout << s << endl;
    }

    return 0;
}

推荐阅读

新手程序员必备的三项技能