hiho一下第182周《Popular Products》题目分析

0
0

本题是一道比较简单的题目。

需要注意一份purchase list中可能包含多个相同的商品,需要去重。

另外有一些实现上的技巧,例如利用set去重,以及利用map自动对key排序的特性。

可以参考以下代码:

#include<iostream>
#include<cassert>
#include<map>
#include<set>
using namespace std;
int n, m;
int main()
{
        map<string, int> purchase;
        cin >> n;
        for(int i = 0; i < n; i++) {
                set<string> prods;
                cin >> m;
                while(m--) {
                        string id, date, price;
                        cin >> id >> date >> price;
                        prods.insert(id + price);
                }
                for(auto& prod : prods) {
                        purchase[prod]++;
                }
        }
        for(auto& kv : purchase) {
                if(kv.second == n)
                        cout << kv.first.substr(0, 9) << endl;
        }
        return 0;
}

2 answer(s)

0

40分的PE是什么情况?该怎么输出格式才是对的呢各位?初学者,见谅!

  • PE一般是输出中有多余或者缺少空格空行。这题有多个答案的时候要每行输出一个。

  • 过了,多谢多谢!

  • 添加评论
  • reply
0
  for(auto& kv : purchase) {
                if(kv.second == n)
                        cout << kv.first.substr(0, 9) << endl;
        }

这个是什么意思,找出最受欢迎的,不应该是最多的商品吗,这里等于n 有问题吧

  • 题目中说受欢迎的是指在所有list中都出现的

  • You should output the product id in increasing order. 他不是说按升序输出所有的吗

  • If two different product share the same id (with different price) you should output the id twice.产品id相同 价格不同时,要输出产品两次 为什么样例中的1111-1111产品没有输出两次?

  • 回复 入门新手:因为1111-1111产品没有都出现所以lists,我觉题目意思是有两个不同价格的产品相同id都出现在lists的时候。

  • 题写错了吧,应该是输出 出现了 n 次的产品(id和时间一致)

  • 首先肯定是出现了n次才会被输出。但是考虑这样的情况:1111-1111 价格998 出现了n次,1111-1111价格999也出现了n次,以及1111-2222 价格100出现了n次。这时输出什么呢?题目描述就是说明1111-1111要输出两次(因为两种价格都出现了n次),1111-2222要排在1111-1111后面

  • 添加评论
  • reply

write answer 切换为英文 切换为中文


转发分享