京东笔试C++岗位

1.采购单
时间限制:C/C++语言 1000MS;其他语言 3000MS
内存限制:C/C++语言 65536KB;其他语言 589824KB
题目描述:
过年啦!小B高兴的不行了,她收到了很多红包,可以实现好多的愿望呢。小B可是对商店货架上心仪的货物红眼好久了,只因囊中羞涩作罢,这次她可是要大大的shopping一番。小B想去购物时,总是习惯性的把要买的东西列在一个购买清单上,每个物品单独列一行(即便要买多个某种物品),这次也不例外。
小B早早的来到了商店,由于她太激动,以至于她到达商店的时候,服务员还没有把各个商品的价签排好,所有的价签还都在柜台上。因此还需要一段时间,等服务器把价签放到对应的商品处,小B才能弄清她的购买清单所需的费用。
小B都有些迫不及待了,她希望你能够根据购买清单,帮她算算最好和最坏的情况下所需的费用,你能帮她吗?
输入
输入中有多组测试数据。每组测试数据的第一行为两个整数n和m(1=<n, m=<1000),分别表示价签的数量以及小B的购买清单中所列的物品数。第二行为空格分隔的n个正整数,表示货架上各类物品的价格,每个数的大小不超过100000。随后的m行为购买清单中物品的名称,所有物品名称为非空的不超过32个拉丁字母构成的字符串,保证清单中不同的物品种类数不超过n,且商店有小B想要购买的所有物品。

输出
对每组测试数据,在单独的行中输出两个数a和b,表示购买清单上所有的物品可能需要的最小和最大费用。

样例输入
5 3
4 2 1 10 5
apple
orange
mango
6 5
3 5 1 6 8 1
peach
grapefruit
banana
orange
orange
样例输出
7 19
11 30

AC代码如下:
#include <stdio.h>
#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdlib>

using namespace std;

int findstr(vector<string> &strFood, string &str) {
	int res = -1;
	if (strFood.empty())
		return -1;
	for (int i = 0; i < strFood.size(); ++i) {
		if (str == strFood[i]) {
			res = i;
			break;
		}
	}
	return res;
}
int main()
{
	int n, m;
	int tmp;
	string str;
	while (cin >> n >> m) {
		vector<int> price;
		vector<string> strFood;
		vector<int> FoodCount(n, 0);
		for (int i = 0; i < n; i++) {
			cin >> tmp;
			price.push_back(tmp);
		}
		sort(price.begin(), price.end());

		for (int i = 0; i < m; i++) {
			cin >> str;
			int j = findstr(strFood, str);
			if (j != -1)
				FoodCount[j]++;
			else {
				FoodCount[strFood.size()] = 1;
				strFood.push_back(str);
			}
		}
		sort(FoodCount.begin(), FoodCount.end());

		int min = 0;
		for (int i = 0, j = FoodCount.size() - 1; i < m && j >= 0; i++, j--) {
			min += FoodCount[j] * price[i];
		}
		int max = 0;
		for (int i = price.size() - 1, j = FoodCount.size() - 1; i >= 0 && j >= 0; i--, j--) {
			max += FoodCount[j] * price[i];
		}
		cout << min << " " << max << endl;
	}
	system("pause");
	return 0;
}
第二题题目请大神补充,我 ac 0%......
全部评论
怎么有题目的
点赞
送花
回复
分享
发布于 2016-09-05 21:44
求帮忙看看赛马不过的原因 本地OK的 #include<iostream> #include<cmath> #include<iomanip> #include<map> #include<unordered_map> #include<vector> #include<list> #include<string> #include<algorithm> using namespace std; int main() { int m, n; while (cin >> n >> m) { int temp, min_val = 0, max_val = 0;; vector<int>total; string food_input; unordered_map<string, int> food; for (int i = 0; i < n; ++i) { cin >> temp; cin.get(); total.push_back(temp); } sort(total.begin(), total.end()); for (int i = 0; i < m; ++i) { getline(cin, food_input); food[food_input]++; } vector<pair<string, int>> xmliu; for (auto it = food.begin(); it != food.end(); ++it) xmliu.push_back(make_pair(it->first, it->second)); sort(xmliu.begin(), xmliu.end(), [](const pair<string, int> &x, const pair<string, int> &y) -> int { return x.second < y.second;}); int j = 0; for (auto it = xmliu.rbegin(); it != xmliu.rend(); ++it) { min_val += (it->second * total[j]); ++j; } j = total.size() - 1; for (auto it = xmliu.rbegin(); it != xmliu.rend(); ++it) { max_val += (it->second * total[j]); --j; } cout << min_val << " " << max_val << endl; } return 0; }
点赞
送花
回复
分享
发布于 2016-09-05 22:39
滴滴
校招火热招聘中
官网直投
// 已经ac,只是差点一分钟提交 import java.util.Arrays; import java.util.Scanner; import java.util.TreeMap; publicclass Main { public static void main(String[] args) { Scanner cin = new Scanner(System.in); while(cin.hasNext()){ TreeMap<String, Integer> list = new TreeMap<>(); int n = cin.nextInt(); int m = cin.nextInt(); int[] price = new int[n]; int[] goods = new int[m]; for(int i=0;i<n;i++){ price[i] = cin.nextInt(); } String temp = cin.nextLine(); for(int i=0;i<m;i++){ String good = cin.nextLine(); //System.out.println(good); if(list.containsKey(good)){ list.put(good, list.get(good)+1); //System.out.println("22"); } else{ list.put(good,1); } } // System.out.println(list.size()); int j=0; int[] goodss = new int[list.size()]; for(String x : list.keySet()){ goodss[j++] = list.get(x); } m = j; // System.out.println(m); Arrays.sort(goodss); Arrays.sort(price); int minres = 0; int maxres = 0; j=0; for(int i=m-1;i>=0;i--){ minres += price[j] * goodss[i]; // System.out.println(price[j]+" *** "+goodss[i]); j++; } j = n-1; for(int i=m-1;i>=0;i--){ maxres += price[j] * goodss[i]; // System.out.println(price[i]+" *** "+goodss[i]); j--; } System.out.println(minres+" "+maxres); } } }
点赞
送花
回复
分享
发布于 2016-09-05 22:43
#include<vector> #include<algorithm> #include<stack> #include<iostream> #include<math.h> #include<map> #include<string> using namespace std; void MinMaxsum(vector<string>material, vector<int>price) { sort(price.begin(), price.end()); map<string, int>m; for (int i = 0; i < material.size(); i++) { if (m.find(material[i]) == m.end()) m[material[i]] = 1; else m[material[i]]++; } vector<int>sorts; for (map<string, int>::iterator it = m.begin(); it != m.end(); it++) { sorts.push_back((it->second)); } sort(sorts.begin(), sorts.end()); int n = sorts.size(); int min_val = 0; int max_val = 0; reverse(sorts.begin(), sorts.end()); for (int i = 0; i < n; i++) { min_val += sorts[i] * price[i]; } reverse(price.begin(), price.end()); for (int i = 0; i < n; i++) { max_val += sorts[i] * price[i]; } cout << min_val << " " << max_val << endl; } int main() { int n; while (cin>>n) { int m; vector<int>prices; cin >> m; for (int i = 0; i < n; i++) { int temp; cin >> temp; prices.push_back(temp); } vector<string>materials; for (int i = 0; i < m; i++) { string temp; cin >> temp; materials.push_back(temp); } MinMaxsum(materials, prices); } return 0; } 我这个AC了
点赞
送花
回复
分享
发布于 2016-09-05 22:54
system("pause"); 的原因吧
点赞
送花
回复
分享
发布于 2016-09-06 08:51

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务