推荐

GESP C++ 一级历年真题题解

_Separation 2026-5-17 20:26:20 27 浏览 0 点赞 0 收藏

GESP C++ 一级历年真题题解

1. 闰年求和

题目大意

给定起始年份和终止年份,求它们之间 不包含起始年份和终止年份 的所有闰年年份之和。

题解

闰年判断规则:

如果年份能被 400400 整除,或者能被 44 整除但不能被 100100 整除,则是闰年。

a+1a+1 枚举到 b1b-1,判断是否为闰年,是就累加。

代码

#include <bits/stdc++.h>
#define LOG(FMT...) fprintf(stderr, FMT)
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
int read(){
    int x = 0,f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
    while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
    return x * f;
}
int a, b;
bool ok(int y) {
    return y % 400 == 0 || (y % 4 == 0 && y % 100 != 0);
}
inline void solve() {
    cin >> a >> b;
    int Ans = 0;
    for (int y = a + 1; y <= b - 1; y++) {
        if (ok(y)) Ans += y;
    }
    printf("%d\n", Ans);
}
int main() {
    int _ = 1;
    while (_--) solve();
    return 0;
}

2. 当天的第几秒

题目大意

输入一个时刻的时、分、秒,以及字符 A\texttt{A}P\texttt{P}A\texttt{A} 表示上午,P\texttt{P} 表示下午。求这个时刻是当天的第几秒。

题解

把时、分、秒统一转成秒。

如果是下午 P\texttt{P},小时数要加 1212

答案为:

h×3600+m×60+sh \times 3600 + m \times 60 + s

代码

#include <bits/stdc++.h>
#define LOG(FMT...) fprintf(stderr, FMT)
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
int read(){
    int x = 0,f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
    while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
    return x * f;
}
int h, m, s;
char c;
inline void solve() {
    cin >> h >> m >> s >> c;
    if (c == 'P') h += 12;
    printf("%d\n", (h * 3600 + m * 60 + s));
}
int main() {
    int _ = 1;
    while (_--) solve();
    return 0;
}

3. 长方形面积

题目大意

给定长方形面积 AA,求有多少种长和宽都是整数的长方形。要求长大于等于宽。

题解

如果 ii 是面积 AA 的因数,那么可以令宽为 ii,长为 A/iA / i

由于要求长大于等于宽,只需要枚举 ii11A\sqrt A

如果 A%i==0A \% i == 0,就找到一种。

代码

#include <bits/stdc++.h>
#define LOG(FMT...) fprintf(stderr, FMT)
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
int read(){
    int x = 0,f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
    while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
    return x * f;
}
int A;
inline void solve() {
    cin >> A;
    int Ans = 0;
    for (int i = 1; i * i <= A; i++) {
        if (A % i == 0) Ans++;
    }
    printf("%d\n", Ans);
}
int main() {
    int _ = 1;
    while (_--) solve();
    return 0;
}

4. 每月天数

题目大意

输入年份和月份,输出这个月有多少天。

题解

普通月份天数固定。

二月需要判断闰年:

闰年二月 2929 天,平年二月 2828 天。

代码

#include <bits/stdc++.h>
#define LOG(FMT...) fprintf(stderr, FMT)
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
int read(){
    int x = 0,f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
    while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
    return x * f;
}
int y, m;
bool ok(int y) {
    return y % 400 == 0 || (y % 4 == 0 && y % 100 != 0);
}
inline void solve() {
    cin >> y >> m;
    if (m == 2) {
        if (ok(y)) printf("29\n");
        else printf("28\n");
    } else if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) {
        printf("31\n");
    } else {
        printf("30\n");
    }
}
int main() {
    int _ = 1;
    while (_--) solve();
    return 0;
}

5. 时间规划

题目大意

输入开始时刻和结束时刻,求中间相差多少分钟。

题解

把两个时刻都转成 从当天 00 点开始经过的分钟数

然后用结束分钟数减去开始分钟数。

代码

#include <bits/stdc++.h>
#define LOG(FMT...) fprintf(stderr, FMT)
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
int read(){
    int x = 0,f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
    while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
    return x * f;
}
int h1, m1, h2, m2;
inline void solve() {
    cin >> h1 >> m1 >> h2 >> m2;
    int s = h1 * 60 + m1;
    int e = h2 * 60 + m2;
    printf("%d\n", e - s);
}
int main() {
    int _ = 1;
    while (_--) solve();
    return 0;
}

6. 累计相加

题目大意

输入 nn,求:

1+(1+2)+(1+2+3)++(1+2++n)1 + (1+2) + (1+2+3) + \cdots + (1+2+\cdots+n)

题解

ii 项是:

1+2++i1 + 2 + \cdots + i

可以边枚举边维护当前前缀和。

例如:

当前 SumSum 表示 1+2++i1+2+\cdots+i,每次把 SumSum 加入答案。

代码

#include <bits/stdc++.h>
#define LOG(FMT...) fprintf(stderr, FMT)
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
int read(){
    int x = 0,f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
    while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
    return x * f;
}
int n;
long long Sum = 0, Ans = 0;
inline void solve() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        Sum += i;
        Ans += Sum;
    }
    printf("%d\n", Ans);
}
int main() {
    int _ = 1;
    while (_--) solve();
    return 0;
}

7. 买文具

题目大意

签字笔 22 元一支,需要 XX 支;记事本 55 元一本,需要 YY 本;直尺 33 元一把,需要 ZZ 把。小明有 QQ 元,判断钱够不够。

题解

先计算总花费:

cost=2X+5Y+3Zcost = 2X + 5Y + 3Z

如果 QcostQ \ge cost,输出 Yes\texttt{Yes} 和剩余的钱。

否则输出 No\texttt{No} 和缺少的钱。

代码

#include <bits/stdc++.h>
#define LOG(FMT...) fprintf(stderr, FMT)
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
int read(){
    int x = 0,f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
    while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
    return x * f;
}
int x, y, z, q;
inline void solve() {
    cin >> x >> y >> z >> q;
    int cost = 2 * x + 5 * y + 3 * z;
    if (q >= cost) printf("Yes\n%d\n", (q - cost));
    else printf("No\n%d\n", (cost - q));
}
int main() {
    int _ = 1;
    while (_--) solve();
    return 0;
}

8. 小明的幸运数

题目大意

个位数为 kk 的正整数,或者 kk 的倍数,都叫作 kk 幸运数。求区间 [L,R][L, R] 内所有 kk 幸运数的和。

题解

直接枚举 LLRR

如果一个数满足:

x%k==0x \% k == 0

或者:

x%10==kx \% 10 == k

就加入答案。

代码

#include <bits/stdc++.h>
#define LOG(FMT...) fprintf(stderr, FMT)
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
int read(){
    int x = 0,f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
    while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
    return x * f;
}
int k, L, R;
inline void solve() {
    cin >> k >> L >> R;
    int Ans = 0;
    for (int i = L; i <= R; i++) {
        if (i % k == 0 || i % 10 == k) {
            Ans += i;
        }
    }
    printf("%d\n", Ans);
}
int main() {
    int _ = 1;
    while (_--) solve();
    return 0;
}

9. 小杨的考试

题目大意

今天是星期 XX,还有 NN 天考试,求考试那天是星期几。题目中用 77 表示星期日。

题解

星期是每 77 天循环一次。

答案为:

(X+N1)(X + N - 1) % 7 + 1

这样可以把结果控制在 1177 之间。

代码

#include <bits/stdc++.h>
#define LOG(FMT...) fprintf(stderr, FMT)
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
int read(){
    int x = 0,f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
    while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
    return x * f;
}
int x, n;
inline void solve() {
    cin >> x >> n;
    int Ans = (x + n - 1) % 7 + 1;
    printf("%d\n", Ans);
}
int main() {
    int _ = 1;
    while (_--) solve();
    return 0;
}

10. 小杨报数

题目大意

11NN 报数,但跳过 MM 的倍数。按顺序输出所有要报的数。

题解

枚举 11NN

如果当前数不是 MM 的倍数,就输出。

代码

#include <bits/stdc++.h>
#define LOG(FMT...) fprintf(stderr, FMT)
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
int read(){
    int x = 0,f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
    while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
    return x * f;
}
int n, m;
inline void solve() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        if (i % m != 0) {
            printf("%d\n", i);
        }
    }
}
int main() {
    int _ = 1;
    while (_--) solve();
    return 0;
}

11. 小杨买书

题目大意

一本书 1313 元。输入小杨有多少钱,求最多可以买几本书,还剩多少钱。

题解

买书本数:

m/13m / 13

剩余钱数:

m%13m \% 13

代码

#include <bits/stdc++.h>
#define LOG(FMT...) fprintf(stderr, FMT)
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
int read(){
    int x = 0,f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
    while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
    return x * f;
}
int m;
inline void solve() {
    cin >> m;
    printf("%d\n%d\n", m / 13. m % 13);
}
int main() {
    int _ = 1;
    while (_--) solve();
    return 0;
}

12. 找因数

题目大意

输入一个正整数 aa,从小到大输出它的所有因数。

题解

11 枚举到 aa

如果 aa % i = 0,说明 iiaa 的因数,输出即可。

代码

#include <bits/stdc++.h>
#define LOG(FMT...) fprintf(stderr, FMT)
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
int read(){
    int x = 0,f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
    while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
    return x * f;
}
int a;
inline void solve() {
    cin >> a;
    for (int i = 1; i <= a; i++) {
        if (a % i == 0) {
            printf("%d\n", i);
        }
    }
}
int main() {
    int _ = 1;
    while (_--) solve();
    return 0;
}

13. 休息时间

题目大意

输入开始学习的时、分、秒,以及学习时长 kk 秒,求开始休息的时刻。

题解

把开始时间转成总秒数:

h×3600+m×60+sh \times 3600 + m \times 60 + s

加上 kk 秒后,再转回时、分、秒。

代码

#include <bits/stdc++.h>
#define LOG(FMT...) fprintf(stderr, FMT)
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
int read(){
    int x = 0,f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
    while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
    return x * f;
}
int h, m, s, k;
inline void solve() {
    cin >> h >> m >> s >> k;
    int t = h * 3600 + m * 60 + s + k;
    int nh = t / 3600;
    int nm = t % 3600 / 60;
    int ns = t % 60;
    printf("%d %d %d\n", nh, nm, ns);
}
int main() {
    int _ = 1;
    while (_--) solve();
    return 0;
}

14. 立方数

题目大意

输入正整数 nn,判断它是否是某个正整数的立方。

题解

枚举正整数 xx

如果存在:

x×x×x=nx \times x \times x = n

则输出 Yes\texttt{Yes},否则输出 No\texttt{No}

代码

#include <bits/stdc++.h>
#define LOG(FMT...) fprintf(stderr, FMT)
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
int read(){
    int x = 0,f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
    while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
    return x * f;
}
int n;
inline void solve() {
    cin >> n;
    bool ok = false;
    for (int x = 1; x * x * x <= n; x++) {
        if (x * x * x == n) {
            ok = true;
        }
    }
    if (ok) printf("Yes\n");
    else printf("No\n");
}
int main() {
    int _ = 1;
    while (_--) solve();
    return 0;
}

15. 小杨购物

题目大意

小杨有 nn 元,商品 A 单价 aa 元,商品 B 单价 bb 元。他想购买相同数量的 A 和 B,求最多可以买多少个 A 和多少个 B。

题解

如果买 xx 个 A 和 xx 个 B,总花费为:

x×a+x×b=x(a+b)x \times a + x \times b = x(a+b)

所以最大数量为:

n/(a+b)n / (a+b)

代码

#include <bits/stdc++.h>
#define LOG(FMT...) fprintf(stderr, FMT)
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
int read(){
    int x = 0,f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
    while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
    return x * f;
}
int n, a, b;
inline void solve() {
    cin >> n >> a >> b;
    printf("%d\n", (n / (a + b)));
}
int main() {
    int _ = 1;
    while (_--) solve();
    return 0;
}

16. 美丽数字

题目大意

一个数如果是 99 的倍数,但不是 88 的倍数,就叫美丽数字。输入 nn 个数,统计美丽数字的个数。

题解

对每个数 xx 判断:

x%9==0x \% 9 == 0

并且:

x%80x \% 8 \ne 0

满足就计数。

代码

#include <bits/stdc++.h>
#define LOG(FMT...) fprintf(stderr, FMT)
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
int read(){
    int x = 0,f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
    while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
    return x * f;
}
int n;
inline void solve() {
    cin >> n;
    int Ans = 0;
    for (int i = 1, x; i <= n; i++) {
        cin >> x;
        if (x % 9 == 0 && x % 8 != 0) {
            Ans++;
        }
    }
    printf("%d\n", Ans);
}
int main() {
    int _ = 1;
    while (_--) solve();
    return 0;
}

17. 温度转换

题目大意

输入开尔文温度 KK,计算摄氏温度 CC 和华氏温度 FF

公式为:

C=K273.15C = K - 273.15 F=C×1.8+32F = C \times 1.8 + 32

如果华氏温度高于 212212,输出 Temperature is too high!\texttt{Temperature is too high!},否则输出 CCFF,保留两位小数。

题解

直接按公式计算。

注意输出小数时使用:

fixed << setprecision(2)

或者使用:

printf("%.2lf", Ans)

代码

#include <bits/stdc++.h>
#define LOG(FMT...) fprintf(stderr, FMT)
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
int read(){
    int x = 0,f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
    while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
    return x * f;
}
double K;
inline void solve() {
    cin >> K;
    double C = K - 273.15;
    double F = C * 1.8 + 32;
    if (F > 212) {
        printf("Temperature is too high!\n");
    } else {
        printf("%.2lf %.2lf\n", C, F);
    }
}
int main() {
    int _ = 1;
    while (_--) solve();
    return 0;
}

18. 奇数和偶数

题目大意

输入 nn 个正整数,统计其中奇数和偶数的个数。

题解

如果一个数 xx 满足:

x%2=1x \% 2 = 1

则是奇数,否则是偶数。

代码

#include <bits/stdc++.h>
#define LOG(FMT...) fprintf(stderr, FMT)
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
int read(){
    int x = 0,f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
    while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
    return x * f;
}
int n;
inline void solve() {
    cin >> n;
    int odd = 0, even = 0;
    for (int i = 1, x; i <= n; i++) {
        cin >> x;
        if (x % 2 == 1) odd++;
        else even++;
    }
    printf("%d %d\n", odd, even);
}
int main() {
    int _ = 1;
    while (_--) solve();
    return 0;
}

19. 图书馆里的老鼠

题目大意

图书馆有 nn 本书,老鼠每 xx 小时啃光一本书。经过 yy 小时后,问还剩多少本完整的书。

题解

如果老鼠正在啃一本书,即使还没啃光,这本书也不算完整。

所以被破坏的书数量是:

y/x\lceil y / x \rceil

整数写法:

(y+x1)/x(y + x - 1) / x

答案为:

ny/xn - \lceil y / x \rceil

代码

#include <bits/stdc++.h>
#define LOG(FMT...) fprintf(stderr, FMT)
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
int read(){
    int x = 0,f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
    while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
    return x * f;
}
int n, x, y;
inline void solve() {
    cin >> n >> x >> y;
    int bad = (y + x - 1) / x;
    printf("%d\n", (n - bad));
}
int main() {
    int _ = 1;
    while (_--) solve();
    return 0;
}

20. 四舍五入

题目大意

输入 nn 个整数,把每个整数四舍五入到最接近的整十数。

题解

对一个正整数 xx,四舍五入到整十数可以写成:

((x+5)/10)×10((x + 5) / 10) \times 10

因为整数除法会自动向下取整。

代码

#include <bits/stdc++.h>
#define LOG(FMT...) fprintf(stderr, FMT)
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
int read(){
    int x = 0,f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
    while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
    return x * f;
}
int n;
inline void solve() {
    cin >> n;
    for (int i = 1, x; i <= n; i++) {
        cin >> x;
        printf("%d\n", ((x + 5) / 10 * 10));
    }
}
int main() {
    int _ = 1;
    while (_--) solve();
    return 0;
}

21. 假期阅读

题目大意

一本书有 nn 页,小 A 每天最多读 kk 页,假期有 tt 天,求最多能读多少页。

题解

最多能读:

k×tk \times t

但不能超过书的总页数 nn

所以答案是:

min(n,k×t)\min(n, k \times t)

代码

#include <bits/stdc++.h>
#define LOG(FMT...) fprintf(stderr, FMT)
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
int read(){
    int x = 0,f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
    while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
    return x * f;
}
int n, k, t;
inline void solve() {
    cin >> n >> k >> t;
    printf("%d\n", min(n, k * t));
}
int main() {
    int _ = 1;
    while (_--) solve();
    return 0;
}

22. 值日

题目大意

小杨每 mm 天值日一次,小红每 nn 天值日一次。今天他们一起值日,求至少多少天后他们再次同一天值日。

题解

这就是求 mmnn 的最小公倍数。

公式:

lcm(m,n)=mgcd(m,n)×nlcm(m,n) = \frac{m}{gcd(m,n)} \times n

代码

#include <bits/stdc++.h>
#define LOG(FMT...) fprintf(stderr, FMT)
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
int read(){
    int x = 0,f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
    while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
    return x * f;
}
int m, n;
int gcd(int a, int b) {
    while (b != 0) {
        int r = a % b;
        a = b;
        b = r;
    }
    return a;
}
inline void solve() {
    cin >> m >> n;
    int Ans = m / gcd(m, n) * n;
    printf("%d\n", Ans);
}
int main() {
    int _ = 1;
    while (_--) solve();
    return 0;
}

23. 商店折扣

题目大意

商店有两种优惠:

第一种:满 xx 元减 yy 元,只能用一次。

第二种:打 nn 折,即价格变为原来的 n/10n / 10

给定商品总价 pp,求最少需要支付多少钱,保留两位小数。

题解

分别计算两种优惠后的价格。

满减方案:

如果 pxp \ge x,价格为 pyp-y,否则还是 pp

打折方案:

p×n/10p \times n / 10

输出较小值。

代码

#include <bits/stdc++.h>
#define LOG(FMT...) fprintf(stderr, FMT)
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
int read(){
    int x = 0,f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
    while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
    return x * f;
}
int x, y, n, p;
inline void solve() {
    cin >> x >> y >> n >> p;
    double a = p;
    if (p >= x) a = p - y;
    double b = p * n / 10.0;
    printf("%.2lf\n", min(a, b));
}
int main() {
    int _ = 1;
    while (_--) solve();
    return 0;
}

24. 金字塔

题目大意

金字塔有 nn 层,从下到上每层需要:

n2,(n1)2,,22,12n^2, (n-1)^2, \cdots, 2^2, 1^2

块石头。求总共需要多少块石头。

题解

直接求平方和:

12+22++n21^2 + 2^2 + \cdots + n^2

n50n \le 50,直接循环即可。

代码

#include <bits/stdc++.h>
#define LOG(FMT...) fprintf(stderr, FMT)
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
int read(){
    int x = 0,f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
    while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
    return x * f;
}
int n;
inline void solve() {
    cin >> n;
    int Ans = 0;
    for (int i = 1; i <= n; i++) {
        Ans += i * i;
    }
    printf("%d\n", Ans);
}
int main() {
    int _ = 1;
    while (_--) solve();
    return 0;
}

25. 小杨的爱心快递

题目大意

快递有两种计费方式:

按体积:费用为 0.5×V0.5 \times V

按重量:

如果 G<300G < 300,费用为 MM

否则费用为 NN

最终运费取两种方式中的较小值,输出一位小数。

题解

分别计算体积费用和重量费用。

然后输出:

min(体积费用,重量费用)\min(体积费用, 重量费用)

代码

#include <bits/stdc++.h>
#define LOG(FMT...) fprintf(stderr, FMT)
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
int read(){
    int x = 0,f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
    while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
    return x * f;
}
double v, g, m, n;
inline void solve() {
    cin >> v >> g >> m >> n;
    double a = 0.5 * v;
    double b;
    if (g < 300) b = m;
    else b = n;
    printf("%.1lf\n", min(a, b));
}
int main() {
    int _ = 1;
    while (_--) solve();
    return 0;
}

26. 手机电量显示

题目大意

输入多组手机电量 PP

如果 P10P \le 10,输出 R\texttt{R}

如果 10<P2010 < P \le 20,输出 L\texttt{L}

如果 P>20P > 20,输出电量数字本身。

题解

按照题目条件进行分类判断即可。

注意边界:

1010 输出 R\texttt{R}

2020 输出 L\texttt{L}

代码

#include <bits/stdc++.h>
#define LOG(FMT...) fprintf(stderr, FMT)
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
int read(){
    int x = 0,f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
    while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
    return x * f;
}
int p;
inline void solve() {
    cin >> p;
    if (p <= 10) printf("R\n");
    else if (p <= 20) printf("L\n");
    else printf("%d\n", p);
}
int main() {
    int _ = 1;
    cin >> _;
    while (_--) solve();
    return 0;
}

27. 交朋友

题目大意

班上有 44 个小朋友,Alice 的身高是 H1H_1。她想和身高最接近自己的人交朋友。如果有多个身高差一样,则选择较矮的人。

题解

只需要比较 H2,H3,H4H_2, H_3, H_4

对每个人计算:

HiH1|H_i - H_1|

选择差值最小的。

如果差值相同,选择身高更小的。

代码

#include <bits/stdc++.h>
#define LOG(FMT...) fprintf(stderr, FMT)
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
int read(){
    int x = 0,f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
    while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
    return x * f;
}
int h[N];
inline void solve() {
    for (int i = 1; i <= 4; i++) {
        cin >> h[i];
    }
    int Ans = h[2];
    int Best = abs(h[2] - h[1]);
    for (int i = 3; i <= 4; i++) {
        int d = abs(h[i] - h[1]);
        if (d < Best || (d == Best && h[i] < Ans)) {
            Best = d;
            Ans = h[i];
        }
    }
    printf("%d\n", Ans);
}
int main() {
    int _ = 1;
    while (_--) solve();
    return 0;
}

28. 数字替换

题目大意

输入一个整数 AA,把其中所有数字 44 替换成 88,输出替换后的结果。

题解

把输入当成字符串处理最方便。

逐个字符检查:

如果是 ’4’\texttt{'4'},改成 ’8’\texttt{'8'}

最后输出字符串。

代码

#include <bits/stdc++.h>
#define LOG(FMT...) fprintf(stderr, FMT)
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
int read(){
    int x = 0,f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
    while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
    return x * f;
}
string s;
inline void solve() {
    cin >> s;
    for (int i = 0; i < (int)s.size(); i++) {
        if (s[i] == '4') s[i] = '8';
    }
    cout << s << '\n';
}
int main() {
    int _ = 1;
    while (_--) solve();
    return 0;
}

评论

0 条
还没有评论。