1 条题解
-
0
【深基4.习10】[COCI2017-2018#6] Davor
解题思路
每周存款为 7x+21k,52 周共 364x+1092k。将 x 从 100 向 1 枚举,检查 n-364x 是否为正且能被 1092 整除,首个成功即输出。
正确性说明
枚举覆盖全部允许的 x;方程唯一确定对应 k。倒序搜索首先得到最大 x,此时也使 k 最小。
易错点
k 必须严格大于 0,不能接受余数为零但 k=0 的方案。
复杂度
时间 O(100),空间 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; for (int x = 100; x >= 1; x--) { int r = n - 364 * x; if (r > 0 && r % 1092 == 0) { cout << x << "\n" << r / 1092 << "\n"; break; } } return 0; }
- 1