一次线性扫描,按'体温 ≥ 37.5 且咳嗽'过滤姓名,重点是把一行三种类型的 token 分别读对。
OJ: roj
题目 ID: 1399
难度:入门
标签:模拟python
创建: 2026-09-30 08:35
更新: 2026-10-05 12:53
题目描述
甲流初筛:体温超过 37.5 度(含等于 37.5 度)并且咳嗽的病人,判定为甲流病人。输入第一行是病人数 5,五个病人依次为 Zhang 38.3 0、Li 37.5 1、Wang 37.1 1、Zhao 39.0 1、Liu 38.2 1;样例输出为 Li、Zhao、Liu 三行加人数行 3。
思路
每个病人是否入选只依赖自己的三个字段,对全部记录做一次线性扫描逐个判定即可,scanf("%s %lf %lld") 把一行三种类型各自转换最省事。
参考代码
Python 版:
python
#!/usr/bin/env python3
# Author by Rainboy blog: https://rainboylv.com github: https://github.com/rainboylvx
# rbook: -> https://rbook.roj.ac.cn https://rbook2.roj.ac.cn
# rainboy的学习导航网站: https://idx.roj.ac.cn
# create_at: 2026-09-30 08:37
# update_at: 2026-09-30 08:37
import sys
THRESHOLD = 37.5 # 初筛体温线,题面明确"含等于 37.5 度"
def solve() -> None:
tokens = iter(sys.stdin.buffer.read().split())
n = int(next(tokens))
flu: list[str] = [] # 按输入顺序收集被初筛为甲流的姓名
for _ in range(n):
name = next(tokens).decode()
temp = float(next(tokens))
cough = next(tokens) == b'1' # 是否咳嗽:1 是,0 否
if temp >= THRESHOLD and cough:
flu.append(name)
print('\n'.join(flu + [str(len(flu))]))
if __name__ == "__main__":
solve()C++ 版(同一算法):
cpp
/**
* Author by Rainboy blog: https://rainboylv.com github: https://github.com/rainboylvx
* rbook: -> https://rbook.roj.ac.cn https://rbook2.roj.ac.cn
* rainboy的学习导航网站: https://idx.roj.ac.cn
* create_at: 2026-10-05 12:53
* update_at: 2026-10-05 12:53
*/
#include <cstdio>
#include <string>
#include <vector>
typedef long long ll;
ll n; // 病人总数
ll cnt; // 被初筛为甲流的人数
std::vector<std::string> ans; // 按输入顺序保存入选病人的姓名
int main() {
scanf("%lld", &n);
for (ll i = 1; i <= n; ++i) {
char name[16]; // 姓名最多 8 个字符,留足余量
double temp;
ll cough;
scanf("%s %lf %lld", name, &temp, &cough);
// 初筛条件:体温 >= 37.5(含等于)且咳嗽,两个条件缺一不可
if (temp >= 37.5 && cough == 1) {
ans.push_back(name);
++cnt;
}
}
// 先按输入顺序输出名单,最后一行输出人数
for (size_t i = 0; i < ans.size(); ++i)
printf("%s\n", ans[i].c_str());
printf("%lld\n", cnt);
return 0;
}