LC.P2240[买钢笔和铅笔的方案数]

方法一:枚举

1
2
3
4
5
6
7
8
9
10
class 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)$