• 趋势科技2011校招笔试题+答案解析


    1、下面程序的输出是多少?

    1. void GetMemory(char *p) 
    2.     p = (char *)malloc(11); 
    3.   
    4. int main(void) 
    5.   char *str = "Hello"; 
    6.   GetMemory(str); 
    7.   strcpy(str,"Hello World"); 
    8.   printf("%s",str); 
    9.   return 0; 

    A、Hello         B、Hello World    C、Hello Worl     D、Run time error/Core dump

     

    2、下面哪个会使这段程序编译错误?

    1. class A 
    2. public: 
    3.     A() 
    4.     { 
    5.     } 
    6. }; 
    7. class B:public A 

      10. public: 

      11.     B() 

      12.     { 

      13.     } 

      14. }; 

      15. A *pb = new B(); 

      16. B b; 

     

    A、 A *pa = dynamic_cast<A *>(pb);

    B、 A *pa = static_cast<A *>(pb);

    C、 A a = static_cast<A >(b);

    D、 A a = dynamic_cast<A >(b);

    E、None of above

    3、下面程序执行的结果是()

    1. void main() 
    2.     char s[] = "abcde"; 
    3.     s += 2; 
    4.     printf("%c\n",s[0]); 

    A、a             B、b                 C、c                    D、编译错误

     

    4、下面程序执行的结果是()

    1. int main(void) 
    2.     char matrix[3][3]={{'a','b','c'},{'d','e','f'},{'g','h','i'}}; 
    3.     printf("%c",matrix[1][4]); 
    4.     return 0; 

    A、c               B、f              C、g                    D、h

     

     

    二、算法题

    1、如何用两个栈来实现一个队列,并分析有关队列操作的运行时间。

    2、如何用两个队列实现一个栈,并分析有关栈操作的运行时间。

    参考答案(欢迎讨论) 转载请注明来源 http://www.cnblogs.com/jerry19880126/

    选择题:

    1. D。GetMemory(str)并不会为str新分配空间,因为str和形参的p虽然指向相同,但它们自身的地址是不同的,p在执行malloc之后就指向不同的位置了,随后因为是局部变量而被释放(但malloc的空间没有析构,成为无法被引用的空间了)。str一直都是指向”Hello”的。str不是字符串数组(只是一个指向字符串常量首地址的指针),没有可用的连续空间,不能用strcpy。
    2. D。用dynamic_cast进行转换时,待转换的类型只能是指针或引用(更详细的总结我会近期更新在博客里)。
    3. D。数组的首地址是常量,不可以变更,若char* p = s. p是允许有p+=2的操作的。
    4. D。数组是连续存储的,元素在空间是连续排布的。

    算法题:

    1.

    队列的操作主要有:入队,出队,返回队列长度,返回队首元素,判断队列是否为空。若用两个stack实现,可以令其中一个inStack专门处理入队操作,另一个outStack专门处理出队操作。对于入队而言,可以直接把元素加入到inStack中,复杂度为O(1),出栈的时候需要做些处理,因为队列是先入后出的,而栈是先入先出,所以输出应该反序,比如inStack接收到的元素顺序为1, 2, 3,栈顶元素是3,但出队希望让1先出。解决方法是把inStack里的元素放到outStack中,这样outStack接收到的元素顺序就是3, 2, 1了,栈顶元素就是我们想要的1,时间复杂度为O(N)。更具体地,入队直接inStack.push(元素),出队先判断outStack是否为空,若不为空,则直接outStack.pop(),若为空,则把inStack元素导入到outStack里,再执行outStack.pop()。其他操作都比较简单,具体参考CPP程序,复制到visual studio中可以直接运行的。

    用两个栈实现队列
      1 #include <stack>
      2 #include <string>
      3 #include <iostream>
      4 #include <cassert>
      5 using namespace std;
      6 
      7 
      8 template <class T>
      9 class myQueue
     10 {
     11 private:
     12     stack<T> inStack; // 用于入队操作
     13     stack<T> outStack; // 用于出队操作
     14 public:
     15     void enqueue(const T& element); // 入队
     16     T dequeue(); // 出队
     17     T top(); // 查看队首元素,并不令之出队
     18     bool empty() const; // 判断队列是否为空
     19     size_t size() const; // 返回队列元素的个数
     20 };
     21 
     22 /************************************************************************/
     23 /*
     24 入队操作,算法复杂度为O(1)
     25 */
     26 /************************************************************************/
     27 template <class T>
     28 void myQueue<T>::enqueue(const T& element)
     29 {
     30     inStack.push(element);
     31 }
     32 
     33 /************************************************************************/
     34 /*
     35 出队操作,算法复杂度为O(N)
     36 */
     37 /************************************************************************/
     38 template <class T>
     39 T myQueue<T>::dequeue()
     40 {
     41     assert(!empty());// 若队列为空,则抛出异常
     42     if(outStack.empty())
     43     {
     44         // outStack为空,则将inStack里的元素放入outStack里面
     45         while(!inStack.empty())
     46         {
     47             T temp = inStack.top();
     48             inStack.pop();
     49             outStack.push(temp);
     50         }
     51     }
     52     T temp = outStack.top();
     53     outStack.pop();
     54     return temp;
     55 }
     56 
     57 /************************************************************************/
     58 /*
     59 查看队首元素,算法复杂度为O(N),与出队操作相比,只是少了一次弹出操作而已
     60 */
     61 /************************************************************************/
     62 template <class T>
     63 T myQueue<T>::top()
     64 {
     65     assert(!empty());// 若队列为空,则抛出异常
     66     if(outStack.empty())
     67     {
     68         // outStack为空,则将inStack里的元素放入outStack里面
     69         while(!inStack.empty())
     70         {
     71             T temp = inStack.top();
     72             inStack.pop();
     73             outStack.push(temp);
     74         }
     75     }
     76     T temp = outStack.top();
     77     return temp;
     78 }
     79 
     80 /************************************************************************/
     81 /*
     82 判断队列是否为空,算法复杂度O(1)
     83 */
     84 /************************************************************************/
     85 template <class T>
     86 bool myQueue<T>::empty() const
     87 {
     88     return size() == 0;
     89 }
     90 
     91 /************************************************************************/
     92 /*
     93 返回队列的长度,算法复杂度为O(1)
     94 */
     95 /************************************************************************/
     96 template <class T>
     97 size_t myQueue<T>::size() const
     98 {
     99     return inStack.size() + outStack.size();
    100 }
    101 
    102 
    103 // 测试样例
    104 int main()
    105 {
    106     myQueue<string> q;
    107     cout << "队列现在是否空 " << q.empty() << endl; 
    108     q.enqueue("hello");
    109     q.enqueue("world");
    110     q.enqueue("how");
    111     q.enqueue("are");
    112     q.enqueue("you");
    113     cout << "队列长度为 " << q.size() << endl;
    114     cout << "队首元素为 " << q.top() << endl;
    115     cout << "队列长度为 " << q.size() << endl;
    116     q.dequeue();
    117     q.dequeue();
    118     q.dequeue();
    119     cout << "删除三个元素后,队列长度为 " << q.size() << endl;
    120     cout << "队首元素为 " << q.top() << endl;
    121     q.dequeue();
    122     q.dequeue();
    123     cout << "再删除两个元素后,队列长度为 " << q.size() << endl;
    124     cout << "队列是否空 " << q.empty() << endl;
    125     q.enqueue("Good");
    126     q.enqueue("Bye");
    127     cout << "入队两个元素后,队首元素为 " << q.top() << endl;
    128     cout << "队列是否空 " << q.empty() << endl;
    129     return 0;
    130 
    131 }

    2.

    栈的操作主要有:入栈,出栈,返回栈顶元素,返回栈长度以及判断栈是否为空。若用两个queue实现(可以定义成queue的数组queue q[2]),可以增加一个currentIndex来指向当前选中的queue。入栈操作可以直接把元素加到queue里,即queue[currentIndex].push(element),时间复杂度为O(1),出栈操作要复杂一些,还是因为栈和队列元素的出入顺序不同,处理方法是将size()-1个元素从q[currentIndex]转移到空闲队列q[(currentIndex + 1)%2]中,q[currentIndex]最后一个剩下的元素恰对应栈顶元素,之后更新一下currentIndex即可,时间复杂度为O(N)。其他操作都比较简单,具体参考CPP程序,复制到visual studio中可以直接运行的。

    用两个队列实现栈
      1 #include <iostream>
      2 #include <cassert>
      3 #include <queue>
      4 
      5 using namespace std;
      6 
      7 
      8 template <class T>
      9 class myStack
     10 {
     11 private:
     12     queue<T> q[2];
     13     int currentIndex;
     14 
     15 public:
     16     myStack():currentIndex(0){}
     17     bool empty() const;
     18     size_t size() const;
     19     void push(const T& element);
     20     void pop();
     21     T top();    
     22 };
     23 
     24 
     25 /************************************************************************/
     26 /*
     27 判断栈是否为空,算法复杂度为O(1)
     28 */
     29 /************************************************************************/
     30 template <class T>
     31 bool myStack<T>::empty() const
     32 {
     33     return size() == 0;
     34 }
     35 
     36 /************************************************************************/
     37 /*
     38 返回栈的大小,算法复杂度为O(1)
     39 */
     40 /************************************************************************/
     41 template <class T>
     42 size_t myStack<T>::size() const
     43 {
     44     return q[0].size() + q[1].size();
     45 }
     46 
     47 
     48 /************************************************************************/
     49 /*
     50 入栈操作,算法复杂度为O(1)
     51 */
     52 /************************************************************************/
     53 template <class T>
     54 void myStack<T>::push(const T& element)
     55 {
     56     q[currentIndex].push(element); // 入队
     57 }
     58 
     59 
     60 /************************************************************************/
     61 /* 
     62 出栈操作,算法复杂度为O(N)
     63 */
     64 /************************************************************************/
     65 template <class T>
     66 void myStack<T>::pop()
     67 {
     68     assert(!empty()); // 若栈为空,则抛出异常
     69     int nextIndex = (currentIndex + 1) % 2;
     70     while(q[currentIndex].size() > 1)
     71     {
     72         // 从一个队列搬移到另一个队列
     73         T element = q[currentIndex].front();
     74         q[currentIndex].pop();
     75         q[nextIndex].push(element);
     76     }
     77     // 最后一个元素弹出
     78     q[currentIndex].pop();
     79     currentIndex = nextIndex;
     80 }
     81 
     82 
     83 /************************************************************************/
     84 /*
     85 返回栈顶元素,算法复杂度为O(1)
     86 */
     87 /************************************************************************/
     88 template <class T>
     89 T myStack<T>::top()
     90 {
     91     assert(!empty()); // 若栈为空,则抛出异常
     92     int nextIndex = (currentIndex + 1) % 2;
     93     T element;
     94     while(q[currentIndex].size() > 0)
     95     {
     96         // 从一个队列搬移到另一个队列
     97         element = q[currentIndex].front();
     98         q[currentIndex].pop();
     99         q[nextIndex].push(element);
    100     }
    101     // 返回最后一个元素
    102     currentIndex = nextIndex;
    103     return element;
    104 }
    105 
    106 
    107 // 测试程序
    108 int main()
    109 {
    110     myStack<int> s;
    111     for(int i = 0; i < 5; ++i)
    112     {
    113         s.push(i);
    114     }
    115     for(int i = 0; i < 5; ++i)
    116     {
    117         cout << s.top() << endl;
    118         s.pop();
    119     }
    120     cout << "栈当前是否空 " << s.empty() << endl;
    121     s.push(2);
    122     cout << "插入一个元素后,栈当前是否空 " << s.empty() << endl;
    123     s.push(-4);
    124     cout << "插入-4后,栈当前大小 " << s.size() << endl;
    125     cout << "栈顶元素为 " << s.top() << endl;
    126     return 0;
    127 }
  • 相关阅读:
    SQL随机排序
    根据经纬度获取所在城市的相关信息以及根据地点城市获取经纬度
    百度收集自动推送脚本——python版
    采集(未测试)
    网页代码测试工具(很有用)
    微信红包源码2020年最新版(完整测试版)
    mvc返回多个结果集,返回多个视图
    计算工龄(mssql标量值函数)
    YZMCMS发布问题以及解决方法
    用输出的方式向页面和js增加引入
  • 原文地址:https://www.cnblogs.com/jerry19880126/p/2636142.html
Copyright © 2020-2023  润新知