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

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. 当天的第几秒
题目大意
输入一个时刻的时、分、秒,以及字符 或 。 表示上午, 表示下午。求这个时刻是当天的第几秒。
题解
把时、分、秒统一转成秒。
如果是下午 ,小时数要加 。
答案为:
代码
#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. 长方形面积
题目大意
给定长方形面积 ,求有多少种长和宽都是整数的长方形。要求长大于等于宽。
题解
如果 是面积 的因数,那么可以令宽为 ,长为 。
由于要求长大于等于宽,只需要枚举 从 到 。
如果 ,就找到一种。
代码
#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. 每月天数
题目大意
输入年份和月份,输出这个月有多少天。
题解
普通月份天数固定。
二月需要判断闰年:
闰年二月 天,平年二月 天。
代码
#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. 时间规划
题目大意
输入开始时刻和结束时刻,求中间相差多少分钟。
题解
把两个时刻都转成 从当天 点开始经过的分钟数 。
然后用结束分钟数减去开始分钟数。
代码
#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. 累计相加
题目大意
输入 ,求:
题解
第 项是:
可以边枚举边维护当前前缀和。
例如:
当前 表示 ,每次把 加入答案。
代码
#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. 买文具
题目大意
签字笔 元一支,需要 支;记事本 元一本,需要 本;直尺 元一把,需要 把。小明有 元,判断钱够不够。
题解
先计算总花费:
如果 ,输出 和剩余的钱。
否则输出 和缺少的钱。
代码
#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. 小明的幸运数
题目大意
个位数为 的正整数,或者 的倍数,都叫作 幸运数。求区间 内所有 幸运数的和。
题解
直接枚举 到 。
如果一个数满足:
或者:
就加入答案。
代码
#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. 小杨的考试
题目大意
今天是星期 ,还有 天考试,求考试那天是星期几。题目中用 表示星期日。
题解
星期是每 天循环一次。
答案为:
这样可以把结果控制在 到 之间。
代码
#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. 小杨报数
题目大意
从 到 报数,但跳过 的倍数。按顺序输出所有要报的数。
题解
枚举 到 。
如果当前数不是 的倍数,就输出。
代码
#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. 小杨买书
题目大意
一本书 元。输入小杨有多少钱,求最多可以买几本书,还剩多少钱。
题解
买书本数:
剩余钱数:
代码
#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. 找因数
题目大意
输入一个正整数 ,从小到大输出它的所有因数。
题解
从 枚举到 。
如果 ,说明 是 的因数,输出即可。
代码
#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. 休息时间
题目大意
输入开始学习的时、分、秒,以及学习时长 秒,求开始休息的时刻。
题解
把开始时间转成总秒数:
加上 秒后,再转回时、分、秒。
代码
#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. 立方数
题目大意
输入正整数 ,判断它是否是某个正整数的立方。
题解
枚举正整数 。
如果存在:
则输出 ,否则输出 。
代码
#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. 小杨购物
题目大意
小杨有 元,商品 A 单价 元,商品 B 单价 元。他想购买相同数量的 A 和 B,求最多可以买多少个 A 和多少个 B。
题解
如果买 个 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. 美丽数字
题目大意
一个数如果是 的倍数,但不是 的倍数,就叫美丽数字。输入 个数,统计美丽数字的个数。
题解
对每个数 判断:
并且:
满足就计数。
代码
#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. 温度转换
题目大意
输入开尔文温度 ,计算摄氏温度 和华氏温度 。
公式为:
如果华氏温度高于 ,输出 ,否则输出 和 ,保留两位小数。
题解
直接按公式计算。
注意输出小数时使用:
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. 奇数和偶数
题目大意
输入 个正整数,统计其中奇数和偶数的个数。
题解
如果一个数 满足:
则是奇数,否则是偶数。
代码
#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. 图书馆里的老鼠
题目大意
图书馆有 本书,老鼠每 小时啃光一本书。经过 小时后,问还剩多少本完整的书。
题解
如果老鼠正在啃一本书,即使还没啃光,这本书也不算完整。
所以被破坏的书数量是:
整数写法:
答案为:
代码
#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. 四舍五入
题目大意
输入 个整数,把每个整数四舍五入到最接近的整十数。
题解
对一个正整数 ,四舍五入到整十数可以写成:
因为整数除法会自动向下取整。
代码
#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. 假期阅读
题目大意
一本书有 页,小 A 每天最多读 页,假期有 天,求最多能读多少页。
题解
最多能读:
但不能超过书的总页数 。
所以答案是:
代码
#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. 值日
题目大意
小杨每 天值日一次,小红每 天值日一次。今天他们一起值日,求至少多少天后他们再次同一天值日。
题解
这就是求 和 的最小公倍数。
公式:
代码
#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. 商店折扣
题目大意
商店有两种优惠:
第一种:满 元减 元,只能用一次。
第二种:打 折,即价格变为原来的 。
给定商品总价 ,求最少需要支付多少钱,保留两位小数。
题解
分别计算两种优惠后的价格。
满减方案:
如果 ,价格为 ,否则还是 。
打折方案:
输出较小值。
代码
#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. 金字塔
题目大意
金字塔有 层,从下到上每层需要:
块石头。求总共需要多少块石头。
题解
直接求平方和:
,直接循环即可。
代码
#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. 小杨的爱心快递
题目大意
快递有两种计费方式:
按体积:费用为 。
按重量:
如果 ,费用为 ;
否则费用为 。
最终运费取两种方式中的较小值,输出一位小数。
题解
分别计算体积费用和重量费用。
然后输出:
代码
#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. 手机电量显示
题目大意
输入多组手机电量 。
如果 ,输出 ;
如果 ,输出 ;
如果 ,输出电量数字本身。
题解
按照题目条件进行分类判断即可。
注意边界:
输出 ;
输出 。
代码
#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. 交朋友
题目大意
班上有 个小朋友,Alice 的身高是 。她想和身高最接近自己的人交朋友。如果有多个身高差一样,则选择较矮的人。
题解
只需要比较 。
对每个人计算:
选择差值最小的。
如果差值相同,选择身高更小的。
代码
#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. 数字替换
题目大意
输入一个整数 ,把其中所有数字 替换成 ,输出替换后的结果。
题解
把输入当成字符串处理最方便。
逐个字符检查:
如果是 ,改成 。
最后输出字符串。
代码
#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;
}
京公网安备11010802045784号