2240. Number of Ways to Buy Pens and Pencils

📑 目录 (Ctrl+点击跳转)

题目 2240. Number of Ways to Buy Pens and Pencils

image-0264fa6c

思路分析

image-5bfe7047

代码实现

class Solution {
    public long waysToBuyPensPencils(int total, int cost1, int cost2) {
       long counts=0;
       for(int i=0;1L*i*cost1<=total;i++){
            long rem = total-(1L*i*cost1);
            counts+=rem/cost2+1;
       }
       return counts;
    }
}
class Solution {
    public long waysToBuyPensPencils(int total, int cost1, int cost2) {
        int maxv=Math.max(cost1,cost2);
        int minv=Math.min(cost1,cost2);
        long counts=0;

        int n=total/maxv;
        for(int i=0;i<=n;i++){
            counts+=(total-maxv*i)/minv+1;
        }

        return counts;
    }
}

同类题型

视频讲解