亚马逊在线笔试题目
2小时,两个题目。在线编程,英文题目。当时没做好,完了自己把他们完成了。答案是我自己写的,自己测试没问题,若有错误请指正。
Question 1 / 2
Question:
We have an array representing customer’s shopping records.
For example, it’s an array like this:
custA, item1,
custB, item1,
custA, item2,
custB, item3,
custC, item1,
custC, item3,
custD, item2,
This array indicates that customer A bought item 1, customer B bought item 1, customer A bought item 2, customer B bought
item 3, etc..
For a given item X and shopping records array, write code to find out what else (item Y) was bought mostly by the customers
who bought item X.
For example, in above example, if X is item 1 then Y should be item 3.
Rules:
1. One customer can only buy one item once.
2. The mostly brought item should not be item X.
3. If no customer brought item X, then return “None”
4. If all the customers who brought item X only brought item X, then return “None”
5. The first line of input is the item X. The second line of input is the shopping record array, this shopping record array is
split by space.
6. If there are many other mostly brought items which have equally brought times, then return any one of those items.
Examples:
Input1:
item1
custA item1 custB item1 custA item2 custB item3 custC item1 custC item3 custD item2
Output1:
item3
Input2:
item2
custA item1 custB item1 custC item1 custA item2 custB item3 custA item3
Output2:
item1
(The output2 can be item3 too)
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
#include
#include
#include
#include
#include
#include
#include
using namespace std;
char* findMostlyBroughtItem(char* shippingRecordArray[], int length, char* givenItem);
inline bool isSpace(char x){
return x == ' ' || x == 'r' || x == 'n' || x == 'f' || x == 'b' || x == 't';
}
char * rightTrim(char *str){
int len = strlen(str);
while(--len>=0){
if(isSpace(str[len])){
str[len] = ' ';
}else{
break;
}
}
return str;
}
char * getInputLine(char *buffer, int length){
if(fgets(buffer,length, stdin)==NULL){
return NULL;
}
rightTrim(buffer);
if(strlen(buffer)<=0){
return NULL;
}
return buffer;
}
int splitAndConvert(char* strings,char* array[]){
char*tokenPtr = strtok(strings," ");
int i=0;
while(tokenPtr!=NULL){
array[i] = tokenPtr;
i++;
tokenPtr=strtok(NULL," ");
}
return i;
}
int main()
{
char givenItem[1000] = {0} ;
while(getInputLine(givenItem, 1000)){
char line[1000];
getInputLine(line, 1000);
char* shoppingRecordArray[1000] = {0};
int length = splitAndConvert(line,shoppingRecordArray);
if(length==0){
break;
}
char * item = findMostlyBroughtItem(shoppingRecordArray, length, givenItem);
if (NULL != item)
{ // 原来系统提供的代码。这里没有NULL判断
cout<
free(item); // 自己加的
}
}
return 0;
}
void
print(pair
cout << t << nd << 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]);
rt(pair
}
// 提取出购买了obj_item商品的`客户名称集合 customers
set customers;
for (map
{
if (0 == (*it)are(obj_item))
{
rt((*it)t);
}
}
// 遍历购买记录 multimap record
// 若客户名称 在 集合set customers 存在,则将商品插入map result
map
for (map
{
for (set::iterator ic = n(); ic != (); ic++)
{
if (0 == (*it)are(*ic))
{
/*
if (() != ((*it)nd))
{
result[(*it)nd] += 1;
}
else
rt(pair
*/
result[(*it)nd] += 1;
break;
}
}
}
pair
// 遍历map result, 寻找最大,而非obj_item的商品名称
for (map
{
if (0 == (*it)are(obj_item))
continue;
if ((*it)nd > nd)
top = make_pair((*it)t, (*it)nd);
}
//cout << "Top: " << t << "t" << nd << endl;
char *p = (char *)malloc(th() + 1);
if (NULL != p)
{
strcpy(p, t.c_str());
return p;
}
return NULL;
}
-
华中农业大学2016年硕士研究生复试录取方案
华中农业大学2016年硕士研究生复试录取方案已经公布了,请考生们抓紧时间做好考研复试的准备工作,下面是小编搜集整理的相关内容,供大家阅读查看。各学院:根据教育部《2016年全国硕士研究生招生工作管理规定》(教学〔2015〕9号)和湖北省教育考试院《关于做好2016年...
-
苏州大学2016年硕士研究生录取工作的通知
苏州大学2016年硕士研究生录取工作的通知已经公布下发了,请相关考生尽早做好考研复试的准备工作,通知的详细内容如下:一、指导思想坚持按需招生、德智体全面衡量、择优录取、宁缺毋滥的原则,进一步加强复试考核,规范招录程序,深化信息公开,不断加强监督管理,切实严明招...
-
2016考研新闻传播学复习考点:外国新闻传播史
以下是本站为大家整理好的有关考研的资料,希望对大家有所帮助,如有疑问请关注本站!一、古代新闻传播发展的三个里程碑及其意义:(一)语言的产生是传播史上第一个重要的里程碑。1、应付大自然的威胁能力增强和共同劳动2、人类思维能力加强3、加快了人们信息交流的传...
-
2015考研管理类综合逻辑真题详解:易出高分
2015考研初试部分科目已经结束,小编在第一时间提供2015考研管理类综合逻辑真题详解,供考生们参考!因为管理类综合的卷子是花卷,所以我们这里所说的题目跟你看到的题号可能不一样,而且每道题的选项很可能不同,也就是说你的A选项有可能是我的B选项,所以我们的题目大概...