2017亚马逊在线笔试题目
if(length==0){
break;
}
char * item = findMostlyBroughtItem(shoppingRecordArray, length, givenItem);
if (NULL != item)
{ // 原来系统提供的代码。这里没有NULL判断
cout<
free(item); // 自己加的
}
}
return 0;
}
void
print(pair
cout << p.first << p.second << endl;
}
//your code is here
//下面才是让写代码的地方,其他的系统已经自动给出。主函数,只有一点点修改。
char* findMostlyBroughtItem(char* shoppingRecordArray[], int length, char* givenItem)
{
if (NULL == shoppingRecordArray || NULL == givenItem)
return NULL;
string obj_item(givenItem);
// 将用户信息 与 购买商品信息 存入multimap record
multimap
for (int i = 0; i < length; i += 2)
{
string customer(shoppingRecordArray[i]);
string item(shoppingRecordArray[i+1]);
record.insert(pair
}
// 提取出购买了obj_item商品的客户名称集合 customers
set customers;
for (map
{
if (0 == (*it).second.compare(obj_item))
{
customers.insert((*it).first);
}
}
// 遍历购买记录 multimap record
// 若客户名称 在 集合set customers 存在,则将商品插入map result
map
for (map
{
for (set::iterator ic = customers.begin(); ic != customers.end(); ic++)
{
if (0 == (*it).first.compare(*ic))
{
/*
if (result.end() != result.find((*it).second))
{
result[(*it).second] += 1;
}
else
result.insert(pair
*/
result[(*it).second] += 1;
break;
}
}
}
pair
// 遍历map result, 寻找最大,而非obj_item的商品名称
for (map
{
if (0 == (*it).first.compare(obj_item))
continue;
if ((*it).second > top.second)
top = make_pair((*it).first, (*it).second);
}
//cout << "Top: " << top.first << "\t" << top.second << endl;
char *p = (char *)malloc(top.first.length() + 1);
if (NULL != p)
{
strcpy(p, top.first.c_str());
return p;
}
return NULL;
}
Question 2 / 2
Question:
As you know, two operations of Stack are push and pop. Now give you two integer arrays, one is the original array before
push and pop operations, the other one is the result array after a series of push and pop operations to the first array. Please
give the push and pop operation sequence.
For example:
If the original array is a[] = {1,2,3}, and the result array is b[] = {1,3,2}.
Then, the operation sequence is “push1|pop1|push2|push3|pop3|pop2”(operations are split by ‘|’ and no space).
Rules:
Time Remaining: 00:25:17
1. The push and pop operations deal with the original int array from left to right.
2. The input is two integer array. They are the original array and the result array. These interger array is split by space.