1 条题解
-
0
练28.2 满足条件的数累加
解题思路
枚举 m~n,检查 i%17==0,满足就加到总和。
正确性说明
按上述推导执行:每次仅处理当前输入或当前步骤,并把它对答案的贡献计入对应状态。所有需要处理的情况均被覆盖,结束时保存的量就是题目所求。
易错点
两端包含;输出累加值,不是倍数数量。
复杂度
时间 O(n-m+1),空间 O(1)。
参考代码(C++11)
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int m, n; cin >> m >> n; int sum = 0; for (int i = m; i <= n; i++) if (i % 17 == 0) sum += i; cout << sum << "\n"; return 0; }
- 1