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


Hrbust ACM练习 2024级第1~2周题单 题解分享

177

相关文章链接

前言

题单链接:2024级第1~2周题单 - Virtual Judge (vjudge.net)
熟悉下 oj 的使用。vjudge 感觉没洛谷好用,提交代码的编辑器没语法高亮。
下面的先是 Python3 代码,之后是 C++ 代码。
注意:主要题目数据范围,有些要开 long long。

A 题

Python Code

print("Hello World!")

Cpp Code

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

int main(){
    cout << "Hello World!";
    return 0;
}

B 题

Python code

print(chr(int(input())))

Cpp code

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

int main()
{
    int ch;
    cin >> ch;
    cout << static_cast<char>(ch);
    return 0;
}

C 题

Python code

print(sum(map(int,input().split())))

Cpp code

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

int main()
{
    int a, b;
    cin >> a >> b;
    cout << a+b;
    return 0;
}

D 题

Python code

print(int(input())**2)

Cpp code

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

int main()
{
    long long a;
    cin >> a;
    cout << a*a;
    return 0;
}

E 题

Python code

x=int(input())
print(x**2+2*x+5)

Cpp code

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

int main()
{
    int a;
    cin >> a;
    cout << a*a+a*2+5;
    return 0;
}

F 题

Python code

print(f'{(float(input())-32)/9*5:.5f}')

Cpp code

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

int main()
{
    double n;
    cin >> n;
    printf("%.5f\n", ((n-32)*5/9));
    return 0;
}

G 题

9 月 5 日编写

Python code

x1, y1 = map(int, input().split())
x2, y2 = map(int, input().split())
print(f"{((x1-x2)**2+(y1-y2)**2)**0.5:.3f}")

Cpp code

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int x1, x2, y1, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    cout << fixed << setprecision(3) << sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2));
    return 0;
}

H 题

Python code

x = float(input())
if 0 <= x < 5:
    y = -x + 2.5
elif 5 <= x < 10:
    y = 2 - 1.5 * (x - 3) * (x - 3)
elif 10 <= x < 20:
    y = x / 2 - 1.5
print(f"{y:.3f}")

Cpp code

#include <bits/stdc++.h>
using namespace std;
int main()
{
    double x, y;
    cin >> x;
    if (0 <= x && x < 5)
    {
        y = -x + 2.5;
    }
    else if (5 <= x && x < 10)
    {
        y = 2 - 1.5 * (x - 3) * (x - 3);
    }
    else if (10 <= x && x < 20)
    {
        y = x / 2 - 1.5;
    }
    cout << fixed << setprecision(3) << y;
    return 0;
}

I 题

Python code

n = int(input())
def a():
    for i in range(2, n):
        if n % i == 0:
            print("No")
            return
    print("Yes")
a()

Cpp code

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin >> n;
    for (int i = 2; i <= n - 1; i++)
    {
        if (n % i == 0)
        {
            cout << "No";
            return 0;
        }
    }
    cout << "Yes";
    return 0;
}

J 题

Python code

w = int(input())
if w > 2 and w % 2 == 0:
    print("YES")
else:
    print("NO")

Cpp code

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int w;
    cin >> w;
    if (w > 2 && w % 2 == 0)
    {
        cout << "YES";
    }
    else
    {
        cout << "NO";
    }
    return 0;
}

K 题

Python code

n = int(input())
rt = 0
while n > 0:
    n -= 1
    if sum(map(int, input().split())) >= 2:
        rt += 1
print(rt)

Cpp code

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int lines, ret;
    ret = 0;
    cin >> lines;
    while (lines > 0)
    {
        lines--;
        int x, y, z;
        cin >> x >> y >> z;
        if (x + y + z >= 2)
        {
            ret++;
        }
    }
    cout << ret;
    return 0;
}

L 题

Python code

n = int(input())
while n > 0:
    n -= 1
    word = input()
    if len(word) > 10:
        print(word[0] + str(len(word) - 2) + word[-1])
    else:
        print(word)

Cpp code

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin >> n;
    while (n > 0)
    {
        n--;
        string s;
        cin >> s;
        if (s.size() > 10)
        {
            cout << s.substr(0, 1) << s.size() - 2 << s.substr(s.size() - 1, s.size()) << endl;
        }
        else
        {
            cout << s << endl;
        }
    }
    return 0;
}

推荐阅读

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