刚参加了亚马逊的在线笔试,果断酱油党啊。。。
两个小时两道题目。可选语言有C、C++、Java、PHP、Python、C#、Ruby、Perl、Haskell、Scala,要求全部使用标准输入输出,已给出部分代码,只要填上关键代码即可,而且不能再自己增加函数。
完了他那个页面上有个compile&test按钮,test一下有几个testcase,然后只有第一个testcase的expected output是可见的,其他全部是hiding的。第一题有9个testcase,可怜我第一次提交只有3个通过,修改完也只有4个通过。。囧。。第二题有7个testcase,第二题果然时间不够了没写完。。。。
下面看看题目:
Question 1 / 2
Let's assume that there is a simple market for beans. Every day there is a published bean price in the market. Traders can buy or sell at the published price. There is a trader who time travelled to future and brought back the price information for a number of days in the future. If you have this information and you are allowed to buy and sell many times. How do you make the maximum profit? The price information will be given as an array of numbers. Each number is for a day’s trading price. The numbers are all integers to simplify the problem. You will need to return the index of the buy-in point and sell-out point for maximum profit.
Rules:
1) The input line length less than 1000, and the trading price length less than 100;
2) The trading price is positive integer;
3) The trading prices are delimited by ' '(single space);
4) Please make sure every buying and selling period shortest. especially, please ouput '-' if all the trading prices are the same or no trading point;
Sample Input and Output:
Input 1
1 3 5 4 2 8 10
Output 1
1 3 5 7
To make the maximum profit, you should buy at $1 and sell at $5, and then buy at $5 and sell it at $10. so the output is "1 3 5 7".
Input 2
1 1 1 3 5 4 2 2 2 8 10
Ouput 2
3 5 9 11
已给的C代码为:
/
//C Sample
#include <stdio.h>
#include <string.h>
void calculateAndPrint(int array[], int length){
//Your Code is here
printf("-");
}
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(gets(line)){
int array[100] = {0};
int length = splitAndConvert(line,array);
if(length==0){
break;
}
calculateAndPrint(array, length);
printf("\n");
}
return 0;
}
Question 2 / 2
consider a kind of configuration file in amazon sofeware system. this kind of configuration file's format looks like this:
B=10;
A={
A=100;
B=BDE;
C=C;
D={
A=windows;
B=mac;
C={
A=redhat;
B=ubuntu;
};
};
A+={
A=200;
E=1000;
};
to repsent the key of the configuration, we use period(.) delimated method. for example,A.B represents the element B in Map A, and the value of A.B is BDE; similarly, the value of A.D.C.A is redhat. the the represent strin
...
- /* Enter your code here. Read input from STDIN. Print output to STDOUT */
- #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;
- }
-
- void
- print(pair<string, int> p) {
- cout << p.first << p.second << 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<string, string> record;
- for (int i = 0; i < length; i += 2)
- {
- string customer(shoppingRecordArray[i]);
- string item(shoppingRecordArray[i+1]);
-
- record.insert(pair<string, string>(customer, item));
- }
-
- // 提取出购买了obj_item商品的客户名称集合 customers
- set<string> customers;
- for (map<string, string>::iterator it = record.begin(); it != record.end(); it++)
- {
- if (0 == (*it).second.compare(obj_item))
- {
- customers.insert((*it).first);
- }
- }
- // 遍历购买记录 multimap record
- // 若客户名称 在 集合set customers 存在,则将商品插入map result
- map<string, int> result;
- for (map<string, string>::iterator it = record.begin(); it != record.end(); it++)
- {
- for (set<string>::iterator ic = customers.begin(); ic != customers.end(); ic++)
- {
- if (0 == (*it).first.compare(*ic))
- {
- /*
- if (result.end() != result.find((*it).second))
- {
- result[(*it).second] += 1;
- }
- else
- result.insert(pair<string, int>((*it).second, 1));
- */
- result[(*it).second] += 1;
- break;
- }
- }
-
- }
-
-
- pair<string, int> top("None", 0);
- // 遍历map result, 寻找最大,而非obj_item的商品名称
- for (map<string, int>::iterator it = result.begin(); it != result.end(); it++)
- {
- if (0 == (*it).first.compare(obj_item))
- continue;
- if ((*it).second > top.second)
- top = make_pair((*it).first, (*it).second);
- }
-
- //cout << "Top: " << top.first << "\t" << top.second << endl;
-
- char *p = (char *)malloc(top.first.length() + 1);
- if (NULL != p)
- {
- strcpy(p, top.first.c_str());
- return p;
- }
-
- return NULL;
- }
- #include <iostream>
- #include <cstring>
- #include <cstdio>
- #include <cstdlib>
- #include <stack>
-
- using namespace std;
-
- char* 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;
- }
- char *operationSequence = calculateOperationSequence(originalArray, resultArray, resultArrayLength);
-
- if (NULL != operationSequence)
- { // 原来系统提供的代码。这里没有NULL判断
- cout<< operationSequence <<endl;
- free(operationSequence); // 自己加的
- }
- else
- cout<< "None" <<endl; // 自己加的
- }
- return 0;
- }
-
- //your code is here
- //下面才是让写代码的地方,其他的系统已经自动给出。主函数,只有一点点修改。
- char* calculateOperationSequence(int * originalArray, int * resultArray, int length)
- {
- if (NULL == originalArray || NULL == resultArray || length <= 0)
- return NULL;
- //使用一个栈模拟入栈和出栈操作就ok了。
- string str;
- stack<int> st;
- int i = 0;
- int j = 0;
- st.push(originalArray[i]);
-
-
- char tmp[5] = "\0";
- str.append("push");
- sprintf(tmp, "%d", originalArray[i]);
- str.append(tmp);
- str.append("|");
-
- i++;
-
- while (!st.empty())
- {
- if (j < length && st.top() == resultArray[j])
- {
- str.append("pop");
- sprintf(tmp, "%d", resultArray[j]);
- str.append(tmp);
- str.append("|");
- st.pop();
- j++;
-
- if (i < length)
- {
- st.push(originalArray[i]);
- str.append("push");
- sprintf(tmp, "%d", originalArray[i]);
- str.append(tmp);
- str.append("|");
- i++;
- }
- }
- else
- {
- if (i < length)
- {
- st.push(originalArray[i]);
- str.append("push");
- sprintf(tmp, "%d", originalArray[i]);
- str.append(tmp);
- str.append("|");
- i++;
- }
- else
- break;
- }
- }
-
-
- if (!st.empty())
- return NULL;
-
- char *p = (char *)malloc(1 + str.length());
- if (NULL != p)
- {
- strcpy(p, str.c_str());
- p[str.length() - 1] = '\0';
- return p;
- }
-
- return NULL;
- }