每条短信限 70 字,每次发送占 ceil(字数/70) 条,把总条数整数累加后除以 10 换成元,保留一位小数输出。
OJ: roj
题目 ID: 1398
难度:入门
标签:入门数学python
创建: 2026-09-30 08:35
更新: 2026-10-05 12:44
题目描述
一条短信最多 70 字(含),每条资费 0.1 元,超过 70 字就按每 70 字一条拆成多条发送。输入第一行是当月发送次数 10 后跟 39 49 42 61 44 147 42 72 35 46,输出 1.3。
思路
每次发送占 printf("%.1f") 输出一位小数即可。
参考代码
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:35
# update_at: 2026-09-30 08:35
import sys
def solve() -> None:
data = iter(map(int, sys.stdin.buffer.read().split()))
n = next(data) # 当月发送短信的次数
# 每条短信拆成 ceil(字数/70) 条计费,总条数 * 0.1 元
total = sum((length + 69) // 70 for length in data)
print(f"{total / 10:.1f}")
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:44
* update_at: 2026-10-05 12:44
*/
// main.cpp:统计当月短信资费,每次发送占 ceil(字数/70) 条,每条 0.1 元。
#include <cstdio>
typedef long long ll;
int main() {
ll n; // 当月发送短信的总次数
if (scanf("%lld", &n) != 1) {
return 0;
}
ll total = 0; // 总条数,整数累加避免浮点误差
for (ll i = 1; i <= n; i++) {
ll length; // 本次发送的字数
scanf("%lld", &length);
// 向上取整写成 (length + 69) / 70 的整数除法,正好等于需要几条短信
total += (length + 69) / 70;
}
// 每条 0.1 元,等价于总条数除以 10,保留一位小数输出
printf("%.1f\n", total / 10.0);
return 0;
}