LC.P2240[买钢笔和铅笔的方案数] 方法一:枚举12345678910class Solution { public long waysToBuyPensPencils(int total, int cost1, int cost2) { long ans = 0; for (int x = 0; x <= total / cost1; ++x) { int y = (total - x * cost1) / cost2 + 1; ans += y; } return ans; }} 时间复杂度:$O(total/cost1)$ 空间复杂度:$O(1)$