Chinaunix首页 | 论坛 | 博客
  • 博客访问: 146522
  • 博文数量: 8
  • 博客积分: 342
  • 博客等级: 一等列兵
  • 技术积分: 109
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-02 09:31
文章分类

全部博文(8)

文章存档

2012年(8)

分类: C/C++

2012-10-12 08:38:35

2小时,两个题目。在线编程,英文题目。当时没做好,完了自己把他们完成了。答案是我自己写的,自己测试没问题,若有错误请指正。
                                                         Question 1 / 2
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. #include <cstring>
  8. #include <cstdio>

  9. using namespace std;

  10. char* findMostlyBroughtItem(char* shippingRecordArray[], int length, char* givenItem);

  11. inline bool isSpace(char x){
  12.     return x == ' ' || x == '\r' || x == '\n' || x == '\f' || x == '\b' || x == '\t';
  13. }

  14. char * rightTrim(char *str){
  15.     int len = strlen(str);
  16.     while(--len>=0){
  17.         if(isSpace(str[len])){
  18.             str[len] = '\0';
  19.         }else{
  20.             break;
  21.         }
  22.     }
  23.     return str;
  24. }

  25. char * getInputLine(char *buffer, int length){
  26.     if(fgets(buffer,length, stdin)==NULL){
  27.         return NULL;
  28.     }
  29.     rightTrim(buffer);
  30.     if(strlen(buffer)<=0){
  31.         return NULL;
  32.     }
  33.     return buffer;
  34. }

  35. int splitAndConvert(char* strings,char* array[]){
  36.     char*tokenPtr = strtok(strings," ");
  37.     int i=0;
  38.     while(tokenPtr!=NULL){
  39.         array[i] = tokenPtr;
  40.         i++;
  41.         tokenPtr=strtok(NULL," ");
  42.     }
  43.     return i;
  44. }

  45. int main()
  46. {
  47.     char givenItem[1000] = {0} ;
  48.     while(getInputLine(givenItem, 1000)){
  49.     char line[1000];
  50.     getInputLine(line, 1000);
  51.         
  52.     char* shoppingRecordArray[1000] = {0};
  53.         
  54.         int length = splitAndConvert(line,shoppingRecordArray);
  55.         if(length==0){
  56.             break;
  57.         }
  58.         

  59.         char * item = findMostlyBroughtItem(shoppingRecordArray, length, givenItem);
  60.         if (NULL != item)
  61.         {   // 原来系统提供的代码。这里没有NULL判断
  62.             cout<<item<<endl;
  63.             free(item) // 自己加的
  64.         }
  65.     }
  66.     return 0;
  67. }

  68. void
  69. print(pair<string, int> p) {
  70.     cout << p.first << p.second << endl;
  71. }

  72. //your code is here 
  73. //下面才是让写代码的地方,其他的系统已经自动给出。主函数,只有一点点修改。

  74. char* findMostlyBroughtItem(char* shoppingRecordArray[], int length, char* givenItem)
  75. {
  76.     if (NULL == shoppingRecordArray || NULL == givenItem)
  77.         return NULL;
  78.     
  79.     string obj_item(givenItem);
  80.     // 将用户信息 与 购买商品信息 存入multimap record
  81.     multimap<string, string> record;
  82.     for (int i = 0; i < length; i += 2)
  83.     {
  84.         string customer(shoppingRecordArray[i]);
  85.         string item(shoppingRecordArray[i+1]);

  86.         record.insert(pair<string, string>(customer, item));
  87.     }

  88.     // 提取出购买了obj_item商品的客户名称集合 customers
  89.     set<string> customers;
  90.     for (map<string, string>::iterator it = record.begin(); it != record.end(); it++)
  91.     {
  92.         if (0 == (*it).second.compare(obj_item))
  93.         {
  94.             customers.insert((*it).first);
  95.         }
  96.     }
  97.     // 遍历购买记录 multimap record
  98.     // 若客户名称 在 集合set customers 存在,则将商品插入map result
  99.     map<string, int> result;
  100.     for (map<string, string>::iterator it = record.begin(); it != record.end(); it++)
  101.     {
  102.         for (set<string>::iterator ic = customers.begin(); ic != customers.end(); ic++)
  103.         {
  104.             if (0 == (*it).first.compare(*ic))
  105.             {
  106.                 /*
  107.                 if (result.end() != result.find((*it).second))
  108.                 {
  109.                     result[(*it).second] += 1;
  110.                 }
  111.                 else
  112.                     result.insert(pair<string, int>((*it).second, 1));
  113.                     */
  114.                 result[(*it).second] += 1;
  115.                 break;
  116.             }
  117.         }

  118.     }


  119.     pair<string, int> top("None", 0);
  120.     // 遍历map result, 寻找最大,而非obj_item的商品名称
  121.     for (map<string, int>::iterator it = result.begin(); it != result.end(); it++)
  122.     {
  123.         if (0 == (*it).first.compare(obj_item))
  124.             continue;
  125.         if ((*it).second > top.second)
  126.             top = make_pair((*it).first, (*it).second);
  127.     }

  128.     //cout << "Top: " << top.first << "\t" << top.second << endl;
  129.     
  130.     char *p = (char *)malloc(top.first.length() + 1);
  131.     if (NULL != p)
  132.     {
  133.         strcpy(p, top.first.c_str());
  134.         return p;
  135.     }

  136.     return NULL;
  137. }

                                                       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. using namespace std;

  7. char* calculateOperationSequence(int *originalArray, int *resultArray, int length);

  8. inline bool isSpace(char x){
  9.     return x == ' ' || x == '\r' || x == '\n' || x == '\r' || x == '\b' || x == '\t';
  10. }

  11. char * rightTrim(char *str){
  12.     int len = strlen(str);
  13.     while(--len>=0){
  14.         if(isSpace(str[len])){
  15.             str[len] = '\0';
  16.         }else{
  17.             break;
  18.         }
  19.     }
  20.     return str;
  21. }

  22. char * getInputLine(char *buffer, int length){
  23.     if(fgets(buffer,length, stdin)==NULL){
  24.         return NULL;
  25.     }
  26.     rightTrim(buffer);
  27.     if(strlen(buffer)<=0){
  28.         return NULL;
  29.     }
  30.     return buffer;
  31. }

  32. int splitAndConvert(char* strings,int *array){
  33.     char*tokenPtr = strtok(strings,",");
  34.     int i=0;
  35.     while(tokenPtr!=NULL){
  36.         array[i] = atoi(tokenPtr);
  37.         i++;
  38.         tokenPtr=strtok(NULL,",");
  39.     }
  40.     return i;
  41. }

  42. int main(){
  43.     char line[1000] = {0} ;
  44.     while(getInputLine(line,1000)){
  45.         int originalArray[30] = {0};
  46.         int originalArrayLength = splitAndConvert(line,originalArray);
  47.         if(originalArrayLength==0){
  48.             break;
  49.         }
  50.         
  51.     getInputLine(line, 1000);
  52.     int resultArray[30] = {0};
  53.         int resultArrayLength = splitAndConvert(line,resultArray);
  54.         if(resultArrayLength==0){
  55.             break;
  56.         }
  57.         char *operationSequence = calculateOperationSequence(originalArray, resultArray, resultArrayLength);

  58.     if (NULL != operationSequence)
  59.     {   // 原来系统提供的代码。这里没有NULL判断
  60.         cout<< operationSequence <<endl;
  61.         free(operationSequence); // 自己加的
  62.     }
  63.     else
  64.      cout<< "None" <<endl; // 自己加的
  65.     }
  66.     return 0;
  67. }

  68. //your code is here  
  69. //下面才是让写代码的地方,其他的系统已经自动给出。主函数,只有一点点修改。
  70. char* calculateOperationSequence(int * originalArray, int * resultArray, int length)
  71. {
  72.     if (NULL == originalArray || NULL == resultArray || length <= 0)
  73.         return NULL;
  74.     //使用一个栈模拟入栈和出栈操作就ok了。
  75.     string str;
  76.     stack<int> st;
  77.     int i = 0;
  78.     int j = 0;
  79.     st.push(originalArray[i]);


  80.     char tmp[5] = "\0";
  81.     str.append("push");
  82.     sprintf(tmp, "%d", originalArray[i]);
  83.     str.append(tmp);
  84.     str.append("|");

  85.     i++;

  86.     while (!st.empty())
  87.     {
  88.         if (j < length && st.top() == resultArray[j])
  89.         {
  90.             str.append("pop");
  91.             sprintf(tmp, "%d", resultArray[j]);
  92.             str.append(tmp);
  93.             str.append("|");
  94.             st.pop();
  95.             j++;
  96.             
  97.             if (i < length)
  98.             {
  99.                 st.push(originalArray[i]);
  100.                 str.append("push");
  101.                 sprintf(tmp, "%d", originalArray[i]);
  102.                 str.append(tmp);
  103.                 str.append("|");
  104.                 i++;
  105.             }
  106.         }
  107.         else
  108.         {
  109.             if (i < length)
  110.             {
  111.                 st.push(originalArray[i]);
  112.                 str.append("push");
  113.                 sprintf(tmp, "%d", originalArray[i]);
  114.                 str.append(tmp);
  115.                 str.append("|");
  116.                 i++;
  117.             }
  118.             else
  119.                 break;
  120.         }
  121.     }


  122.     if (!st.empty())
  123.         return NULL;

  124.     char *p = (char *)malloc(1 + str.length());
  125.     if (NULL != p)
  126.     {
  127.         strcpy(p, str.c_str());
  128.         p[str.length() - 1] = '\0';
  129.         return p;
  130.     }

  131.     return NULL;
  132. }


阅读(22125) | 评论(1) | 转发(0) |
0

上一篇:关于公司笔试面试题目的困惑

下一篇:没有了

给主人留下些什么吧!~~

syuucin2013-04-14 14:54:25

q2:
public class PushAndPopTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        int[] input = {1,2,3,4};
        int[] output = {1,3,2,4};