1 条题解
-
0
练28.3 短信计费
解题思路
一次消息 x 字需要 ceil(x/70) 条,整数式 (x+69)/70。逐次统计条数,总费用为总条数×0.1 元。
正确性说明
按上述推导执行:每次仅处理当前输入或当前步骤,并把它对答案的贡献计入对应状态。所有需要处理的情况均被覆盖,结束时保存的量就是题目所求。
易错点
每次发送单独向上取整,不能把当月字数合起来再拆分。
复杂度
时间 O(n),额外空间 O(1)。
参考代码(C++11)
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; int count = 0; while (n--) { int x; cin >> x; count += (x + 69) / 70; } cout << fixed << setprecision(1) << count / 10.0 << "\n"; return 0; }
- 1