算出基准有效率 x,每组改进疗法有效率 y 与之作差,按 y-x 与 5% 的严格大小三分类输出 better/worse/same,注意分子分母顺序与严格不等号。
OJ: roj
题目 ID: 1072
难度:入门
标签:入门数学浮点数python
创建: 2026-09-29 17:19
更新: 2026-10-05 00:13
题目描述
给定 better;worse;否则输出 same。
数据范围:
思路
先读入基准组的总病例数与有效病例数,算出 better / worse,恰好等于 same。
参考代码
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-29 17:20
# update_at: 2026-09-29 17:20
import sys
LIMIT = 0.05 # 有效率差超过 5% 才判定为 better / worse
def judge(diff: float) -> str:
"""回答"差值 diff 对应三档判定中的哪一档"。"""
if diff > LIMIT:
return "better"
if -diff > LIMIT: # x - y > 5%,即 y 落后超过 5%
return "worse"
return "same"
def solve() -> None:
data = iter(map(int, sys.stdin.buffer.read().split()))
n = next(data)
total, effective = next(data), next(data)
base = effective / total # 第一组是鸡尾酒疗法的有效率 x
out: list[str] = []
for _ in range(n - 1):
total, effective = next(data), next(data)
out.append(judge(effective / total - base)) # y - x:正为新疗法更优
print("\n".join(out))
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 00:11
* update_at: 2026-10-05 00:13
*/
#include <iostream>
using namespace std;
typedef long long ll;
const double LIMIT = 0.05; // 有效率差超过 5% 才判定为 better / worse
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll n;
cin >> n;
ll base_total, base_effective;
cin >> base_total >> base_effective;
double base = base_effective * 1.0 / base_total; // 鸡尾酒疗法有效率 x
for (ll i = 0; i < n - 1; ++i) {
ll total, effective;
cin >> total >> effective;
double diff = effective * 1.0 / total - base; // 新疗法有效率 y 与基准的差
if (diff > LIMIT) {
cout << "better\n";
} else if (-diff > LIMIT) { // x - y > 5%
cout << "worse\n";
} else {
cout << "same\n";
}
}
return 0;
}