2012 亚马逊在线笔试题目2
2014-12-12 11:56
155 查看
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)
#include < iostream >
#include < string >
#include < map >
#include < set >
#include < algorithm >
#include < cstring >
#include < cstdio >
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] = '\0';
}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<<item<<endl;
free(item); // 自己加的
}
}
return 0;
}
char* findMostlyBroughtItem(char* shoppingRecordArray[], int length, char* givenItem){
if(NULL==shoppingRecordArray || NULL==givenItem || length <=0)
return NULL;
multimap mymap;
multimap::iterator mapit;
multimap::iterator mapit2;
map buymap;
map::iterator itbuymap;
for(int i=0;i
string customer(shoppingRecordArray[i++]);
string item(shoppingRecordArray[i]);
mymap.insert(make_pair(customer,item));
}
string searchitem(givenItem);
for(mapit=mymap.begin();mapit!=mymap.end();mapit++){
//item found
if(searchitem==mapit->second) {
for(mapit2=mymap.begin();mapit2 != mymap.end();mapit2++) {
if(mapit->first==mapit2->first) {
itbuymap=buymap.find(mapit2->second);
if(itbuymap==buymap.end())
buymap.insert(make_pair(mapit2->second,1));
else
buymap[mapit2->second]++;
}
}
}
}
pair founditem("",0);
for(itbuymap=buymap.begin();itbuymap!=buymap.end();itbuymap++){
if(itbuymap->first !=searchitem && itbuymap->second > founditem.second)
founditem=make_pair(itbuymap->first,itbuymap->second);
}
char *p=NULL;
if(founditem.first != "") {
cout <<founditem.first<<endl;
p=new char[founditem.first.length()+1];
strcpy(p,founditem.first.c_str());
}
return p;
}
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)
#include < iostream >
#include < string >
#include < map >
#include < set >
#include < algorithm >
#include < cstring >
#include < cstdio >
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] = '\0';
}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<<item<<endl;
free(item); // 自己加的
}
}
return 0;
}
char* findMostlyBroughtItem(char* shoppingRecordArray[], int length, char* givenItem){
if(NULL==shoppingRecordArray || NULL==givenItem || length <=0)
return NULL;
multimap mymap;
multimap::iterator mapit;
multimap::iterator mapit2;
map buymap;
map::iterator itbuymap;
for(int i=0;i
string customer(shoppingRecordArray[i++]);
string item(shoppingRecordArray[i]);
mymap.insert(make_pair(customer,item));
}
string searchitem(givenItem);
for(mapit=mymap.begin();mapit!=mymap.end();mapit++){
//item found
if(searchitem==mapit->second) {
for(mapit2=mymap.begin();mapit2 != mymap.end();mapit2++) {
if(mapit->first==mapit2->first) {
itbuymap=buymap.find(mapit2->second);
if(itbuymap==buymap.end())
buymap.insert(make_pair(mapit2->second,1));
else
buymap[mapit2->second]++;
}
}
}
}
pair founditem("",0);
for(itbuymap=buymap.begin();itbuymap!=buymap.end();itbuymap++){
if(itbuymap->first !=searchitem && itbuymap->second > founditem.second)
founditem=make_pair(itbuymap->first,itbuymap->second);
}
char *p=NULL;
if(founditem.first != "") {
cout <<founditem.first<<endl;
p=new char[founditem.first.length()+1];
strcpy(p,founditem.first.c_str());
}
return p;
}
相关文章推荐
- 2012 亚马逊在线笔试题目1
- 2014亚马逊在线笔试题目
- 2014年亚马逊在线笔试题目及解法_9_24(字符串编辑距离升级版)
- 2013亚马逊在线笔试题目
- 2014亚马逊在线笔试题目及解决方案(MMChess问题)
- 网易互联网2017在线笔试题目-魔力手环
- 微软2016校园招聘9月在线笔试-题目3 : Fibonacci
- 亚马逊2013的一道在线笔试题
- 百度2012校园招聘机器学习/数据挖掘工程师(北京)笔试题目
- 2012九月3号阿里巴巴笔试题目
- 百度的在线笔试题目【暂无答案】
- 亚马逊在线笔试第三题--(变种的)三叉树的最近公共祖先问题
- [2014亚马逊amazon] 在线笔试题 大于非负整数N的第一个回文数 Symmetric Number
- 2016阿里巴巴c/c++研发工程师在线笔试题(附加题)(题目写的很乱,纯属碎片)
- 2018大疆校招软件岗位笔试题目(求有效工作时间)——未在线验证
- 2017微软秋季校园招聘在线编程笔试 题目2 Composition
- 滴滴出行2015在线笔试题目
- 百度的在线笔试题目[添加了答案]
- 2012百度招聘笔试题目
- 微软201610 在线笔试 题目3 Registration Day