【面试题】2013亚马逊校园招聘--在线笔试题

来源:互联网 发布:初中物理软件大全 编辑:程序博客网 时间:2024/04/28 14:29

第一题:

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)

简单的说就是买了X的人又买了其他的东西,那么其他这些东西中,买的数量最多的是哪个??

 

答案:

 

2、给定一个原始序列和一个经过push N次pop N次形成的出栈序列,求push和pop的次序??

运行结果是

1,2,3,4
2,4,3,1
push1 push2 pop2 push3 push4 pop4 pop3 pop1

我的程序:

#include <iostream>#include <cstring>#include <stack>using namespace std;void calculateOperationSequence(int *originalArray, int *resultArray, int length);inline bool isSpace(char x){    return x == ' ' || x == '\r' || x == '\n' || x == '\r' || 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,int *array){    char*tokenPtr = strtok(strings,",");    int i=0;    while(tokenPtr!=NULL){        array[i] = atoi(tokenPtr);        i++;        tokenPtr=strtok(NULL,",");    }    return i;}int main(){    char line[1000] = {0} ;    while(getInputLine(line,1000)){        int originalArray[30] = {0};        int originalArrayLength = splitAndConvert(line,originalArray);        if(originalArrayLength==0){            break;        }getInputLine(line, 1000);int resultArray[30] = {0};        int resultArrayLength = splitAndConvert(line,resultArray);        if(resultArrayLength==0){            break;        }/*operationSequence = */calculateOperationSequence(originalArray, resultArray, resultArrayLength);/*cout<<operationSequence<<endl;*/    }    return 0; } //your code is herevoid calculateOperationSequence(int *originalArray, int *resultArray, int length){int j = 0;int i = 0;stack<int> s;while( j<length && i<length){if(originalArray[i] != resultArray[j]){s.push(originalArray[i]);cout << "push"<<originalArray[i]<<" ";i++;if(i>length-1){break;}}else{cout <<"push"<<originalArray[i++]<<" ";cout <<"pop"<<resultArray[j++]<< " ";if(i>length-1 || j>length-1){break;}}}while(!s.empty() && j<length){if(s.top() == resultArray[j]){s.pop();cout << "pop"<<resultArray[j]<<" ";j++;}else{return ;}}}


 

原创粉丝点击