leetcode 1357. 每隔 n 个顾客打折(C++)

2023-05-16

超市里正在举行打折活动,每隔 n 个顾客会得到 discount 的折扣。

超市里有一些商品,第 i 种商品为 products[i] 且每件单品的价格为 prices[i] 。

结账系统会统计顾客的数目,每隔 n 个顾客结账时,该顾客的账单都会打折,折扣为 discount (也就是如果原本账单为 x ,那么实际金额会变成 x - (discount * x) / 100 ),然后系统会重新开始计数。

顾客会购买一些商品, product[i] 是顾客购买的第 i 种商品, amount[i] 是对应的购买该种商品的数目。

请你实现 Cashier 类:

  • Cashier(int n, int discount, int[] products, int[] prices) 初始化实例对象,参数分别为打折频率 n ,折扣大小 discount ,超市里的商品列表 products 和它们的价格 prices 。
  • double getBill(int[] product, int[] amount) 返回账单的实际金额(如果有打折,请返回打折后的结果)。返回结果与标准答案误差在 10^-5 以内都视为正确结果。

 

示例 1:

输入
["Cashier","getBill","getBill","getBill","getBill","getBill","getBill","getBill"]
[[3,50,[1,2,3,4,5,6,7],[100,200,300,400,300,200,100]],[[1,2],[1,2]],[[3,7],[10,10]],[[1,2,3,4,5,6,7],[1,1,1,1,1,1,1]],[[4],[10]],[[7,3],[10,10]],[[7,5,3,1,6,4,2],[10,10,10,9,9,9,7]],[[2,3,5],[5,3,2]]]
输出
[null,500.0,4000.0,800.0,4000.0,4000.0,7350.0,2500.0]
解释
Cashier cashier = new Cashier(3,50,[1,2,3,4,5,6,7],[100,200,300,400,300,200,100]);
cashier.getBill([1,2],[1,2]);                        // 返回 500.0, 账单金额为 = 1 * 100 + 2 * 200 = 500.
cashier.getBill([3,7],[10,10]);                      // 返回 4000.0
cashier.getBill([1,2,3,4,5,6,7],[1,1,1,1,1,1,1]);    // 返回 800.0 ,账单原本为 1600.0 ,但由于该顾客是第三位顾客,他将得到 50% 的折扣,所以实际金额为 1600 - 1600 * (50 / 100) = 800 。
cashier.getBill([4],[10]);                           // 返回 4000.0
cashier.getBill([7,3],[10,10]);                      // 返回 4000.0
cashier.getBill([7,5,3,1,6,4,2],[10,10,10,9,9,9,7]); // 返回 7350.0 ,账单原本为 14700.0 ,但由于系统计数再次达到三,该顾客将得到 50% 的折扣,实际金额为 7350.0 。
cashier.getBill([2,3,5],[5,3,2]);                    // 返回 2500.0

 

提示:

  • 1 <= n <= 10^4
  • 0 <= discount <= 100
  • 1 <= products.length <= 200
  • 1 <= products[i] <= 200
  • products 列表中 不会 有重复的元素。
  • prices.length == products.length
  • 1 <= prices[i] <= 1000
  • 1 <= product.length <= products.length
  • product[i] 在 products 出现过。
  • amount.length == product.length
  • 1 <= amount[i] <= 1000
  • 最多有 1000 次对 getBill 函数的调用。
  • 返回结果与标准答案误差在 10^-5 以内都视为正确结果。

C++

class Cashier {
public:
    Cashier(int n, int discount, vector<int>& products, vector<int>& prices) 
    {
        this->n=n;
        this->discount=discount;
        this->products=products;
        this->prices=prices;
        this->tmp=0;
        for(int i=0;i<products.size();i++)
        {
            vec[products[i]]=i;
        }
    }
    
    double getBill(vector<int> product, vector<int> amount) 
    {
        tmp++;
        double res=0;
        int m=product.size();
        for(int i=0;i<m;i++)
        {
            int price=prices[vec[product[i]]];
            int num=amount[i];
            res+=price*num;
        }
        if(tmp%n==0)
        {
            return res - (discount * res) / 100;
        }
        return res;
    }
    
private:
    int n;
    int discount;
    vector<int> products;
    vector<int> prices;
    int tmp;
    map<int,int> vec;
};

/**
 * Your Cashier object will be instantiated and called as such:
 * Cashier* obj = new Cashier(n, discount, products, prices);
 * double param_1 = obj->getBill(product,amount);
 */

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

leetcode 1357. 每隔 n 个顾客打折(C++) 的相关文章

  • webpack 错误

    1 ERROR in src main css Module build failed from node modules mini css extract plugin dist loader js ReferenceError docu
  • 客户端js 读取 json 数据

    采用 XMLHttpRequest 读取 1 new 初始化 XMLHttpRequest 2 open 设置请求方式 xff0c 地址 3 send 发起请求 4 onload 请求成功 xff0c 返回结果 代码 xff1a lt DO
  • dataTable的中文文档

    DataTables Table plug in for jQuery https datatables net 用google 打开 xff0c 直接翻译 参考 选项 DataTables 及其扩展是极其可配置的库 xff0c 它们对 H

随机推荐

  • nodejs核心API

    1 Buffer对象 不需要引入 Bufferd对象用途 以二进制流进行数据的传送传递 1 三种创建方式 类似于数组的创建 用16进制存储 let buf1 61 Buffer alloc 20 13 console log buf1 lt
  • mysql select, from ,join ,on ,where groupby,having ,order by limit的执行顺序和书写顺序

    mysql select xff0c from xff0c join xff0c on xff0c where groupby having order by limit的执行顺序和书写顺序 关键字或 解释执行顺序select 查询列表 x
  • jQuery对checkbox的各种操作

    注意 xff1a 操作checkbox的checked disabled属性时jquery1 6以前版本用attr 1 6以上 xff08 包含 xff09 建议用prop 1 根据id获取checkbox 34 cbCheckbox1 3
  • dom 操作排他思路

    1 遍历所有 xff0c 操作 xff0c 对具体的重新操作 实现点击一个按钮就把背景颜色改为pink颜色 lt DOCTYPE html gt lt html lang 61 34 en 34 gt lt head gt lt meta
  • web前端兼容

    兼容性 xff1a Compatibility overview https www quirksmode org compatibility html 当前测试 CSS所有 CSS 选择器和声明 xff08 最终 xff09 DOM所有
  • 使用跨域资源共享的 DOM 访问控制

    Dev Opera DOM Access Control Using Cross Origin Resource Sharing https dev opera com articles dom access control using c
  • react-typescript 错误

    64 types jsurl index d ts 39 is not a module npm i jsurl 安装错误 xff0c 因为 typescript无法用此方法安装 xff0c 卸载干净 1 增加types目录 新建jsurl
  • react-router-dom错误

    index tsx 24 Uncaught Error useLocation may be used only in the context of a lt Router gt component at invariant index t
  • Total Blocking Time 总阻塞时间 (TBT)

    总阻塞时间 TBT 是测量加载响应度的重要实验室指标 xff0c 因为该项指标有助于量化在页面交互性变为可靠前 xff0c 不可交互程度的严重性 xff0c 较低的 TBT 有助于确保页面的可用性 什么是 TBT xff1f 总阻塞时间 T
  • JavaScript中的回调的作用是什么

    这期内容当中小编将会给大家带来有关JavaScript中的回调的作用是什么 xff0c 文章内容丰富且以专业的角度为大家分析和叙述 xff0c 阅读完这篇文章希望大家可以有所收获 回调函数 首先写一个向人打招呼的函数 只需要创建一个接受 n
  • css如何换行

    在css中通过word break与white space这两个属性来设置自动换行 xff0c 其中word wrap属性允许长单词或URL地址换行到下一行 xff1b 而white space属性可以设置文本换行方式 本文操作环境 xff
  • JS 实现复制功能(document.execCommand)

    功能 xff1a 点击按钮 xff0c 复制值 实现方法 xff1a 通过原生js 的方法document execCommand 39 copy 39 坑 xff1a document execCommand copy 不生效 不能实现的
  • Linux chmod命令 修改文件权限被禁止(not permitted)的解决办法

    解决方法 在Linux环境下 xff0c 修改文件时以外导致文件没有权限读取和修改 xff0c 在修改相关文件 usr bin docker的属性的时 chmod 777 usr bin containerd chmod changing
  • springsource-tools下载安装

    下载springsource tools我弄了一个多小时才找到下载地址 xff0c 必须得好好记录一下 首先 需要避免一个误区 xff1a 下载的不是spring tools 这个下载后是一个jar包 也不是一个可执行文件的压缩包 xff0
  • canvas节点无法导出图片_前端实现图片压缩及遇到的问题

    图片上传是前端中常见的的业务场景 无论是前台还是后台 xff0c 适当的对图片进行压缩处理 xff0c 可以显著的提升用户体验 而在后台管理系统中 xff0c 图片压缩不仅仅能够提升后台管理员操作体验 xff0c 更是可以防止后台设置过大的
  • iframe嵌套其它网站页面详解

    iframe基本内涵 通常我们使用iframe直接直接在页面嵌套iframe标签指定src就可以了 lt iframe src 61 34 demo iframe sandbox htm 34 gt lt iframe gt 但是 xff0
  • iframe嵌入其他网站,如何自适应高度

    终于有一周时间 xff0c 工作不那么忙了 xff0c 腾出手来总结下工作过程中学到的知识 每天遇到新问题 xff0c 解决新问题 xff0c 但是却很少有时间去仔细研究下 xff0c 或者总结下 攒的多了 xff0c 就得从头捋一遍 说下
  • 解决anaconda安装pil包时的问题

    在anaconda中安装pil时出现UnsatisfiableError 看了较多的解决方法 看了较多的解决方法 很多都是讲怎样创建新的环境再安装 xff0c 在Linux中只需要将这样做 xff1a xff08 Linux小白的详细操作
  • 1.5 字符串

    1 5 1 单引号 双引号 三引号 a 61 34 Hello world 34 双引号 b 61 39 python is groovy 39 单引号 c 61 34 34 34 Computer says 39 No 39 34 34
  • leetcode 1357. 每隔 n 个顾客打折(C++)

    超市里正在举行打折活动 xff0c 每隔 n 个顾客会得到 discount 的折扣 超市里有一些商品 xff0c 第 i 种商品为 products i 且每件单品的价格为 prices i 结账系统会统计顾客的数目 xff0c 每隔 n