把单价放大 10 倍转成整数角,读入 10 个数量后做整数加权和,最后除以 10 输出一位小数。
OJ: roj
题目 ID: 1104
难度:入门
标签:入门数学模拟浮点数python
创建: 2026-09-29 19:35
更新: 2026-10-05 01:15
题目描述
输入 10 个整数,依次表示 10 种图书的购买数量;输出应付总费用,精确到小数点后一位。
思路
十种图书单价都是一位小数,把单价表放大 10 倍变成以「角」为单位的整数,读入数量后逐项做整数乘积并累加,最后把总和除以 10 按一位小数输出即可。
参考代码
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 19:35
# update_at: 2026-09-29 19:43
import sys
# 十种图书的单价,单位是「角」,顺序与题面的购买数量一一对应
PRICES_IN_JIAO: tuple[int, ...] = (289, 327, 456, 780, 350, 862, 278, 430, 560, 650)
def solve() -> None:
counts = map(int, sys.stdin.buffer.read().split()) # 十个购买数量
# 单价都是一位小数,按「角」做整数加权和可以全程无舍入;只在输出时换回元
total_in_jiao = sum(price * count for price, count in zip(PRICES_IN_JIAO, counts))
print(f"{total_in_jiao / 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 01:15
* update_at: 2026-10-05 01:16
*/
#include <cstdio>
typedef long long ll;
// 十种图书单价,单位:角(原题单价乘以 10 后的整数)
ll price[10] = {289, 327, 456, 780, 350, 862, 278, 430, 560, 650};
int main() {
ll total = 0; // 总费用,单位:角
ll c;
for (int i = 0; i < 10; ++i) {
scanf("%lld", &c);
total += price[i] * c; // 对应图书的费用累加
}
printf("%.1f\n", total / 10.0); // 角转元,固定输出一位小数
return 0;
}