1 条题解
-
0
【例17.2】 蜗牛爬行
解题思路
将路程拆成前 6 厘米与超出部分,前段长度 min(n,6),后段长度 max(n-6,0),分别乘 5、7 后相加。
正确性说明
两段互不重复且拼成全程,每段采用对应单价,故总和为总消耗。
易错点
不是超过 6 厘米后所有路程都按 7 计算。
复杂度
输入规模固定,时间 O(1),额外空间 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; cout << min(n, 6) * 5 + max(n - 6, 0) * 7 << "\n"; return 0; }
- 1