1 条题解
-
0
【例50.2】 计算书费
解题思路
单价转成角的整数数组,逐项乘购买数量求总角数,再除10以一位小数显示。
正确性说明
按顺序完成所述运算后,每个输入元素都被处理在相应位置或统计项中。保存的中间状态与已处理数据对应,最终输出与题目定义一致。
易错点
价格顺序与输入书目顺序一一对应,不能排序。
复杂度
时间O(1),空间O(1)。
参考代码(C++11)
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int price[] = {289, 327, 456, 780, 350, 862, 278, 430, 560, 650}; long long sum = 0; for (int p : price) { int count; cin >> count; sum += 1LL * p * count; } cout << fixed << setprecision(1) << sum / 10.0 << "\n"; return 0; }
- 1