2013amazon网上测试题
2012-10-10 16:05
351 查看
问题1:
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:
The push and pop operations deal with the original int array from left to right.
The input is two integer array. They are the original array and the result array. These interger array is split by space.
The output is the operation sequence.
If the original array cannot make to the result array with stack push and pop, The output should be 'None'.
The operation "push1" means push the first element of the original array to the stack.
The operation "pop1" means pop the first element of the original array from the stack, and add this element to the tail of the result array.
Please don't include any space in the output string.
Sample1:
Input:
1 2 3 4
1 2 3 4
Output:
push1|pop1|push2|pop2|push3|pop3|push4|pop4
Sample2:
Input:
1 2 3 4
4 3 2 1
Output:
push1|push2|push3|push4|pop4|pop3|pop2|pop1
解答:
问题2:
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:
One customer can only buy one item once.
The mostly brought item should not be item X.
If no customer brought item X, then return “None”
If all the customers who brought item X only brought item X, then return “None”
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.
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)
解答:
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:
The push and pop operations deal with the original int array from left to right.
The input is two integer array. They are the original array and the result array. These interger array is split by space.
The output is the operation sequence.
If the original array cannot make to the result array with stack push and pop, The output should be 'None'.
The operation "push1" means push the first element of the original array to the stack.
The operation "pop1" means pop the first element of the original array from the stack, and add this element to the tail of the result array.
Please don't include any space in the output string.
Sample1:
Input:
1 2 3 4
1 2 3 4
Output:
push1|pop1|push2|pop2|push3|pop3|push4|pop4
Sample2:
Input:
1 2 3 4
4 3 2 1
Output:
push1|push2|push3|push4|pop4|pop3|pop2|pop1
解答:
#include <iostream> #include <string> #include <stack> #include <vector> #include <stdio.h> using namespace std; stack<int> sta;//栈记录 vector<string> seq;//操作序列 vector<int> popSeq;//出栈序列 bool flag = false; //arr:入栈序列 out:出栈序列 sta:当前栈中数据 current:当前处理到的arr中的位置 len:入栈序列长度 bool g(int arr[], int out[], int current, int len) { //如果已经找到操作序列,其它的即不用再操作 if (flag) return true; //操作1:入栈 if (current < len) { char str1[10]; sprintf(str1, "push%d", arr[current]); //产生push1 seq.push_back(str1);//记录序列加1 sta.push(arr[current]);//当前值入栈 g(arr, out, ++current, len);//处理下一个值 int temp1 = sta.top(); current--;//插入元素的位置也要恢复 sta.pop();//恢复之前的状态 seq.pop_back();//记录序列减1 } //否则,最后一个已经入栈 //操作2:出栈 if (!sta.empty()) { int temp2 = sta.top(); char str2[10]; sprintf(str2, "pop%d", temp2); //产生push1 seq.push_back(str2);//记录出栈序列 popSeq.push_back(temp2); sta.pop(); g(arr, out, current, len);//处理的还是当前值,只不过出栈一个元素 sta.push(temp2);//恢复之前的状态 seq.pop_back();//恢复出栈序列 popSeq.pop_back(); } //结束判断 if (seq.size() == len * 2) { for (int i = 0; i < len; i++) { if (popSeq[i] != out[i]) { return false;//add } } for (int tt = 0; tt < len*2 - 1; tt++) { cout<<seq[tt]<<"|"; } cout<<seq[len*2 - 1]<<endl; flag = true; return true;//add //这里可输出所有序列 /*for (int tt = 0; tt < len*2; tt++) { cout<<seq[tt]; } cout<<endl; for (int tt = 0; tt < len; tt++) { cout<<popSeq[tt]; } cout<<endl;*/ } return false;//add } int main() { int arr[] = {1,2,3}; int out[] = {1,3,2}; int len = 3; g(arr, out, 0, len); if (!flag) cout<<"None"<<endl; int tt; cin>>tt; return 0; }
问题2:
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:
One customer can only buy one item once.
The mostly brought item should not be item X.
If no customer brought item X, then return “None”
If all the customers who brought item X only brought item X, then return “None”
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.
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> using namespace std; int main() { //测试1开始 /*multimap<string, string> customer2orders; customer2orders.insert(make_pair("custA","item1")); customer2orders.insert(make_pair("custB","item1")); customer2orders.insert(make_pair("custC","item1")); customer2orders.insert(make_pair("custA","item2")); customer2orders.insert(make_pair("custB","item3")); customer2orders.insert(make_pair("custA","item3")); multimap<string, string> orders2customer; orders2customer.insert(make_pair("item1","custA")); orders2customer.insert(make_pair("item1","custB")); orders2customer.insert(make_pair("item1","custC")); orders2customer.insert(make_pair("item2","custA")); orders2customer.insert(make_pair("item3","custB")); orders2customer.insert(make_pair("item3","custA")); map<string, int> itemCount; itemCount.insert(make_pair("item1", 0)); itemCount.insert(make_pair("item2", 0)); itemCount.insert(make_pair("item3", 0)); //以该item作为开始 string inputItem = "item2";*/ //测试1结束 //============================================ //测试2开始 multimap<string, string> customer2orders; customer2orders.insert(make_pair("custA","item1")); customer2orders.insert(make_pair("custB","item1")); customer2orders.insert(make_pair("custA","item2")); customer2orders.insert(make_pair("custB","item3")); customer2orders.insert(make_pair("custC","item1")); customer2orders.insert(make_pair("custC","item3")); customer2orders.insert(make_pair("custD","item2")); multimap<string, string> orders2customer; orders2customer.insert(make_pair("item1","custA")); orders2customer.insert(make_pair("item1","custB")); orders2customer.insert(make_pair("item2","custA")); orders2customer.insert(make_pair("item3","custB")); orders2customer.insert(make_pair("item1","custC")); orders2customer.insert(make_pair("item3","custC")); orders2customer.insert(make_pair("item2","custD")); map<string, int> itemCount; itemCount.insert(make_pair("item1", 0)); itemCount.insert(make_pair("item2", 0)); itemCount.insert(make_pair("item3", 0)); itemCount.insert(make_pair("item4", 0)); string inputItem = "item1";//以该item作为开始 //测试2结束 multimap<string,string>::iterator iter; pair< multimap<string,string>::iterator, multimap<string,string>::iterator>pos; pos=orders2customer.equal_range(inputItem); for(iter=pos.first;iter!=pos.second;iter++) { multimap<string,string>::iterator iter2; pair<multimap<string,string>::iterator, multimap<string,string>::iterator>pos2; pos2 = customer2orders.equal_range(iter->second); for(iter2 = pos2.first; iter2!=pos2.second; iter2++) { map<string, int>::iterator iter3 = itemCount.begin(); for (iter3; iter3 != itemCount.end(); iter3++) { if (iter2->second == iter3->first && iter2->second != inputItem)//除去自身的 { iter3->second += 1; } } } } string output; int max = -1; for(map<string,int>::iterator iter4 = itemCount.begin(); iter4 != itemCount.end(); iter4++) { if (iter4->second > max) { output = iter4->first; max = iter4->second; } } //找出最大的item cout<<output<<endl; return 0; }
相关文章推荐
- 在网上找了一份HTML阶段测试题,初学者有兴趣可以试下
- VS2015+opencv,按照网上的VS2013教程配置的,运行的时候出现【无法启动此程序,因为计算机中丢失MSVCP120D.dll。尝试重新安装该程序以解决问题。】
- 从网上找的 visual studio 的各个版本下载地址,很全,从 6.0 一直 到 vs2013,要的拿去吧
- 2013网上购票经历
- Amazon Hiring Campus 2013 - Final 6
- Amazon Hiring Campus 2013
- Amazon Campus(2013-Sep-24)Question 1 / 2 (Amazon Campus(15): Clock wise walk)
- amazon 2013校园招聘题目
- Amazon Campus(2013-Sep-24)Question 2 / 2 (Amazon Campus(17):Find the differences of items in amazon)
- Amazon Campus(2013-Sep-22)Question 2 / 2 (Amazon Campus(9): MM-Chess)
- 亚马逊在线技术笔试(Amazon Hiring Campus 2013 - Final 6)
- Amazon Campus(2013-Sep-22)Question 1 / 2 (Amazon Campus(5): completely inside interval)
- asp.net+sql server网上商城项目 外包2000~3000元
- 2013_3_9 山东自考上机_Java 山东自考上机_数据库系统原理_记忆版
- Cheatsheet: 2013 10.01 ~ 10.08
- vs 2013 + git
- Win8.1 x64 + VS2013 下CGAL安装及配置
- 2013
- VS2013 安装错误 Windows 程序兼容性模式已打开
- 在网上看到这篇《大牛讲解信号与系统以及数字信号处理》