发布于 2014-09-23 10:21:58 | 388 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

亚马逊(Amazon)

亚马逊公司(Amazon,简称亚马逊;NASDAQ:AMZN),是美国最大的一家网络电子商务公司,位于华盛顿州的西雅图。是网络上最早开始经营电子商务的公司之一,亚马逊成立于1995年,一开始只经营网络的书籍销售业务,现在则扩及了范围相当广的其他产品,已成为全球商品品种最多的网上零售商和全球第二大互联网企业,在公司名下,也包括了AlexaInternet、a9、lab126、和互联网电影数据库(Internet Movie Database,IMDB)等子公司。


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)
 
  1. /* Enter your code here. Read input from STDIN. Print output to STDOUT */
  2. #include <iostream>
  3. #include <string>
  4. #include <map>
  5. #include <set>
  6. #include <algorithm>
  7.  
  8. #include <cstring>
  9. #include <cstdio>
  10.  
  11. using namespace std;
  12.  
  13. char* findMostlyBroughtItem(char* shippingRecordArray[], int length, char* givenItem);
  14.  
  15. inline bool isSpace(char x){
  16.     return x == ' ' || x == '\r' || x == '\n' || x == '\f' || x == '\b' || x == '\t';
  17. }
  18.  
  19. char * rightTrim(char *str){
  20.     int len = strlen(str);
  21.     while(--len>=0){
  22.         if(isSpace(str[len])){
  23.             str[len] = '\0';
  24.         }else{
  25.             break;
  26.         }
  27.     }
  28.     return str;
  29. }
  30.  
  31. char * getInputLine(char *buffer, int length){
  32.     if(fgets(buffer,length, stdin)==NULL){
  33.         return NULL;
  34.     }
  35.     rightTrim(buffer);
  36.     if(strlen(buffer)<=0){
  37.         return NULL;
  38.     }
  39.     return buffer;
  40. }
  41.  
  42. int splitAndConvert(char* strings,char* array[]){
  43.     char*tokenPtr = strtok(strings," ");
  44.     int i=0;
  45.     while(tokenPtr!=NULL){
  46.         array[i] = tokenPtr;
  47.         i++;
  48.         tokenPtr=strtok(NULL," ");
  49.     }
  50.     return i;
  51. }
  52.  
  53. int main()
  54. {
  55.     char givenItem[1000] = {0} ;
  56.     while(getInputLine(givenItem, 1000)){
  57.     char line[1000];
  58.     getInputLine(line, 1000);
  59.         
  60.     char* shoppingRecordArray[1000] = {0};
  61.         
  62.         int length = splitAndConvert(line,shoppingRecordArray);
  63.         if(length==0){
  64.             break;
  65.         }
  66.         
  67.  
  68.         char * item = findMostlyBroughtItem(shoppingRecordArray, length, givenItem);
  69.         if (NULL != item)
  70.         {   // 原来系统提供的代码。这里没有NULL判断
  71.             cout<<item<<endl;
  72.             free(item) // 自己加的
  73.         }
  74.     }
  75.     return 0;
  76. }
  77.  
  78. void
  79. print(pair<string, int> p) {
  80.     cout << p.first << p.second << endl;
  81. }
  82.  
  83. //your code is here 
  84. //下面才是让写代码的地方,其他的系统已经自动给出。主函数,只有一点点修改。
  85.  
  86. char* findMostlyBroughtItem(char* shoppingRecordArray[], int length, char* givenItem)
  87. {
  88.     if (NULL == shoppingRecordArray || NULL == givenItem)
  89.         return NULL;
  90.     
  91.     string obj_item(givenItem);
  92.     // 将用户信息 与 购买商品信息 存入multimap record
  93.     multimap<string, string> record;
  94.     for (int i = 0; i < length; i += 2)
  95.     {
  96.         string customer(shoppingRecordArray[i]);
  97.         string item(shoppingRecordArray[i+1]);
  98.  
  99.         record.insert(pair<string, string>(customer, item));
  100.     }
  101.  
  102.     // 提取出购买了obj_item商品的客户名称集合 customers
  103.     set<string> customers;
  104.     for (map<string, string>::iterator it = record.begin(); it != record.end(); it++)
  105.     {
  106.         if (0 == (*it).second.compare(obj_item))
  107.         {
  108.             customers.insert((*it).first);
  109.         }
  110.     }
  111.     // 遍历购买记录 multimap record
  112.     // 若客户名称 在 集合set customers 存在,则将商品插入map result
  113.     map<string, int> result;
  114.     for (map<string, string>::iterator it = record.begin(); it != record.end(); it++)
  115.     {
  116.         for (set<string>::iterator ic = customers.begin(); ic != customers.end(); ic++)
  117.         {
  118.             if (0 == (*it).first.compare(*ic))
  119.             {
  120.                 /*
  121.                 if (result.end() != result.find((*it).second))
  122.                 {
  123.                     result[(*it).second] += 1;
  124.                 }
  125.                 else
  126.                     result.insert(pair<string, int>((*it).second, 1));
  127.                     */
  128.                 result[(*it).second] += 1;
  129.                 break;
  130.             }
  131.         }
  132.  
  133.     }
  134.  
  135.  
  136.     pair<string, int> top("None", 0);
  137.     // 遍历map result, 寻找最大,而非obj_item的商品名称
  138.     for (map<string, int>::iterator it = result.begin(); it != result.end(); it++)
  139.     {
  140.         if (0 == (*it).first.compare(obj_item))
  141.             continue;
  142.         if ((*it).second > top.second)
  143.             top = make_pair((*it).first, (*it).second);
  144.     }
  145.  
  146.     //cout << "Top: " << top.first << "\t" << top.second << endl;
  147.     
  148.     char *p = (char *)malloc(top.first.length() + 1);
  149.     if (NULL != p)
  150.     {
  151.         strcpy(p, top.first.c_str());
  152.         return p;
  153.     }
  154.  
  155.     return NULL;
  156. }
                                                       Question 2 / 2
Question:
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:
Time Remaining: 00:25:17
1.  The push and pop operations deal with the original int array from left to right.
2.  The input is two integer array. They are the original array and the result array. These interger array is split by space.
3.  The output is the operation sequence.
4.  If the original array cannot make to the result array with stack push and pop, The output should be 'None'.
5.  The operation "push1" means push the first element of the original array to the stack.
6.  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.
7.  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

 

  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdio>
  4. #include <cstdlib>
  5. #include <stack>
  6.  
  7. using namespace std;
  8.  
  9. char* calculateOperationSequence(int *originalArray, int *resultArray, int length);
  10.  
  11. inline bool isSpace(char x){
  12.     return x == ' ' || x == '\r' || x == '\n' || x == '\r' || x == '\b' || x == '\t';
  13. }
  14.  
  15. char * rightTrim(char *str){
  16.     int len = strlen(str);
  17.     while(--len>=0){
  18.         if(isSpace(str[len])){
  19.             str[len] = '\0';
  20.         }else{
  21.             break;
  22.         }
  23.     }
  24.     return str;
  25. }
  26.  
  27. char * getInputLine(char *buffer, int length){
  28.     if(fgets(buffer,length, stdin)==NULL){
  29.         return NULL;
  30.     }
  31.     rightTrim(buffer);
  32.     if(strlen(buffer)<=0){
  33.         return NULL;
  34.     }
  35.     return buffer;
  36. }
  37.  
  38. int splitAndConvert(char* strings,int *array){
  39.     char*tokenPtr = strtok(strings,",");
  40.     int i=0;
  41.     while(tokenPtr!=NULL){
  42.         array[i] = atoi(tokenPtr);
  43.         i++;
  44.         tokenPtr=strtok(NULL,",");
  45.     }
  46.     return i;
  47. }
  48.  
  49. int main(){
  50.     char line[1000] = {0} ;
  51.     while(getInputLine(line,1000)){
  52.         int originalArray[30] = {0};
  53.         int originalArrayLength = splitAndConvert(line,originalArray);
  54.         if(originalArrayLength==0){
  55.             break;
  56.         }
  57.         
  58.     getInputLine(line, 1000);
  59.     int resultArray[30] = {0};
  60.         int resultArrayLength = splitAndConvert(line,resultArray);
  61.         if(resultArrayLength==0){
  62.             break;
  63.         }
  64.         char *operationSequence = calculateOperationSequence(originalArray, resultArray, resultArrayLength);
  65.  
  66.     if (NULL != operationSequence)
  67.     {   // 原来系统提供的代码。这里没有NULL判断
  68.         cout<< operationSequence <<endl;
  69.         free(operationSequence); // 自己加的
  70.     }
  71.     else
  72.      cout<< "None" <<endl; // 自己加的
  73.     }
  74.     return 0;
  75. }
  76.  
  77. //your code is here  
  78. //下面才是让写代码的地方,其他的系统已经自动给出。主函数,只有一点点修改。
  79. char* calculateOperationSequence(int * originalArray, int * resultArray, int length)
  80. {
  81.     if (NULL == originalArray || NULL == resultArray || length <= 0)
  82.         return NULL;
  83.     //使用一个栈模拟入栈和出栈操作就ok了。
  84.     string str;
  85.     stack<int> st;
  86.     int i = 0;
  87.     int j = 0;
  88.     st.push(originalArray[i]);
  89.  
  90.  
  91.     char tmp[5] = "\0";
  92.     str.append("push");
  93.     sprintf(tmp, "%d", originalArray[i]);
  94.     str.append(tmp);
  95.     str.append("|");
  96.  
  97.     i++;
  98.  
  99.     while (!st.empty())
  100.     {
  101.         if (j < length && st.top() == resultArray[j])
  102.         {
  103.             str.append("pop");
  104.             sprintf(tmp, "%d", resultArray[j]);
  105.             str.append(tmp);
  106.             str.append("|");
  107.             st.pop();
  108.             j++;
  109.             
  110.             if (i < length)
  111.             {
  112.                 st.push(originalArray[i]);
  113.                 str.append("push");
  114.                 sprintf(tmp, "%d", originalArray[i]);
  115.                 str.append(tmp);
  116.                 str.append("|");
  117.                 i++;
  118.             }
  119.         }
  120.         else
  121.         {
  122.             if (i < length)
  123.             {
  124.                 st.push(originalArray[i]);
  125.                 str.append("push");
  126.                 sprintf(tmp, "%d", originalArray[i]);
  127.                 str.append(tmp);
  128.                 str.append("|");
  129.                 i++;
  130.             }
  131.             else
  132.                 break;
  133.         }
  134.     }
  135.  
  136.  
  137.     if (!st.empty())
  138.         return NULL;
  139.  
  140.     char *p = (char *)malloc(1 + str.length());
  141.     if (NULL != p)
  142.     {
  143.         strcpy(p, str.c_str());
  144.         p[str.length() - 1] = '\0';
  145.         return p;
  146.     }
  147.  
  148.     return NULL;
  149. }

 



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务