亚马逊在线笔试题目.docx
- 文档编号:529771
- 上传时间:2022-10-10
- 格式:DOCX
- 页数:8
- 大小:16.90KB
亚马逊在线笔试题目.docx
《亚马逊在线笔试题目.docx》由会员分享,可在线阅读,更多相关《亚马逊在线笔试题目.docx(8页珍藏版)》请在冰豆网上搜索。
亚马逊在线笔试题目
2021亚马逊在线笔试题目
2021亚马逊在线笔试题目
2小时,两个题目。
在线编程,英文题目。
当时没做好,完了自己把他们完成了。
答案是我自己写的,自己测试没问题,若有错误请指正。
Question1/2
Question:
Wehaveanarrayrepresentingcustomersshoppingrecords.
Forexample,itsanarraylikethis:
custA,item1,
custB,item1,
custA,item2,
custB,item3,
custC,item1,
custC,item3,
custD,item2,
ThisarrayindicatesthatcustomerAboughtitem1,customerBboughtitem1,customerAboughtitem2,customerBboughtitem3,etc..
ForagivenitemXandshoppingrecordsarray,writecodetofindoutwhatelse(itemY)wasboughtmostlybythecustomerswhoboughtitemX.
Forexample,inaboveexample,ifXisitem1thenYshouldbeitem3.
Rules:
1.Onecustomercanonlybuyoneitemonce.
2.ThemostlybroughtitemshouldnotbeitemX.
3.IfnocustomerbroughtitemX,thenreturn"None'
4.IfallthecustomerswhobroughtitemXonlybroughtitemX,thenreturn"None'
5.ThefirstlineofinputistheitemX.Thesecondlineofinputistheshoppingrecordarray,thisshoppingrecordarrayissplitbyspace.
6.Iftherearemanyothermostlybroughtitemswhichhaveequallybroughttimes,thenreturnanyoneofthoseitems.
Examples:
Input1:
item1
custAitem1custBitem1custAitem2custBitem3custCitem1custCitem3custDitem2
Output1:
item3
Input2:
item2
custAitem1custBitem1custCitem1custAitem2custBitem3custAitem3
Output2:
item1
(Theoutput2canbeitem3too)
/*Enteryourcodehere.ReadinputfromSTDIN.PrintoutputtoSTDOUT*/
#include
#include
#include
#include
#include
#include
#include
usingnamespacestd;
char*findMostlyBroughtItem(char*shippingRecordArray[],intlength,char*givenItem);
inlineboolisSpace(charx){
returnx==''||x=='\r'||x=='\n'||x=='\f'||x=='\b'||x=='\t';
}
char*rightTrim(char*str){
intlen=strlen(str);
while(--len=0){
if(isSpace(str[len])){
str[len]='\0';
}else{
break;
}
}
returnstr;
}
char*getInputLine(char*buffer,intlength){
if(fgets(buffer,length,stdin)==NULL){
returnNULL;
}
rightTrim(buffer);
if(strlen(buffer)=0){
returnNULL;
}
returnbuffer;
}
intsplitAndConvert(char*strings,char*array[]){
char*tokenPtr=strtok(strings,);
inti=0;
while(tokenPtr!
=NULL){
array[i]=tokenPtr;
i++;
tokenPtr=strtok(NULL,);
}
returni;
}
intmain()
{
chargivenItem[1000]={0};
while(getInputLine(givenItem,1000)){
charline[1000];
getInputLine(line,1000);
char*shoppingRecordArray[1000]={0};
intlength=splitAndConvert(line,shoppingRecordArray);
if(length==0){
break;
}
char*item=findMostlyBroughtItem(shoppingRecordArray,length,givenItem);
if(NULL!
=item)
{//原来系统供应的代码。
这里没有NULL推断
cout
free(item);//自己加的
}
}
return0;
}
void
print(pair
coutp.firstp.secondendl;
}
//yourcodeishere
//下面才是让写代码的地方,其他的系统已经自动给出。
主函数,只有一点点修改。
char*findMostlyBroughtItem(char*shoppingRecordArray[],intlength,char*givenItem)
{
if(NULL==shoppingRecordArray||NULL==givenItem)
returnNULL;
stringobj_item(givenItem);
//将用户信息与购买商品信息存入multimaprecord
multimap
for(inti=0;ilength;i+=2)
{
stringcustomer(shoppingRecordArray[i]);
stringitem(shoppingRecordArray[i+1]);
record.insert(pair
}
//提取出购买了obj_item商品的客户名称集合customers
setcustomers;
for(map
{
if(0==(*it)pare(obj_item))
{
customers.insert((*it).first);
}
}
//遍历购买记录multimaprecord
//若客户名称在集合setcustomers存在,则将商品插入mapresult
map
for(map
{
for(set:
:
iteratoric=customers.begin();ic!
=customers.end();ic++)
{
if(0==(*it)pare(*ic))
{
/*
if(result.end()!
=result.find((*it).second))
{
result[(*it).second]+=1;
}
else
result.insert(pair
*/
result[(*it).second]+=1;
break;
}
}
}
pair
//遍历mapresult,查找最大,而非obj_item的商品名称
for(map
{
if(0==(*it)pare(obj_item))
continue;
if((*it).secondtop.second)
top=make_pair((*it).first,(*it).second);
}
//coutTop:
top.first\ttop.secondendl;
char*p=(char*)malloc(top.first.length()+1);
if(NULL!
=p)
{
strcpy(p,top.first.c_str());
returnp;
}
returnNULL;
}
Question2/2
Question:
Asyouknow,twooperationsofStackarepushandpop.Nowgiveyoutwointegerarrays,oneistheoriginalarraybefore
pushandpopoperations,theotheroneistheresultarrayafteraseriesofpushandpopoperationstothefirstarray.Please
givethepushandpopoperationsequence.
Forexample:
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 亚马逊 在线 笔试 题目