四道面试题

来源:互联网 发布:淘宝模特拍摄视频震惊 编辑:程序博客网 时间:2024/05/16 18:36

1、给定一个N个整数元素的数组,元素分别为A1, A2, A3....AN,每个元素分别对应一个权重W1(小于1的float), W2,W3....WN,其和为1,找出其中一个元素Ak,使所有小于Ak的元素的权重之和小于1/2,所有大于Ak的元素的权重之和>=1/2。
        思路:首先将该数组按元素值的大小进行升序排列,同样的那个权值数组也要对应的进行排序,因为原先的那个数组的下标和权值数组的下标是相对应的,如果权值数组不跟着变化的,那么就无法知道某一个数的权值是多少了,就无法对应起来了。。
        核心代码如下:

[cpp] view plain copy
  1. sum = w[1];//小于Ak的元素的权重之和  
  2. for(k=2;k<=n;k++)  
  3. {  
  4.     if(sum>1/2)  
  5.         return -1;          //没有找到符合要求的元素Ak  
  6.     if(sum < 1/2 && sum + w[k] <= 1/2)    //sum < 1/2 保证使所有小于Ak的元素的权重之和小于1/2。     sum + w[k] <= 1/2,使得小于等于Ak的元素权重之和小于等于1/2,也就是所有大于Ak的元素的权重之和>=1/2。  
  7.         return A[k];  
  8.     sum += w[k];  
  9. }  

2、给定一个N个元素的整数,元素分别为A1,A2,A3....AN,将数组变为A1<A2>A3<A4......的锯齿状数组,时间复杂度?
       这个题目比排序更好点,将数组分为两部分,前半部分的值全部小于后半部分的值,接下来从两个起始位置逐个取数就可以了。

       首先用nth_element把数组划分,以中位数为分界点,所有小于中位数(x)的元素在x之前,大于x的元素在x之后。
       然后从两头分别取一个小于x的数,大小x的数,保存到另一数组中。
       nth_element的时间复杂度是O(n)的。

       例如:  
       原数组   1 5 3 7 4 2 6
       nth_后    1 3 2 4 7 6 5 (只保证4在中间,前三个不一定有序,但都比4小)
       新数组   1 7 3 6 2 5 4

代码如下:

[cpp] view plain copy
  1. #include "iostream"  
  2. #include "algorithm"  
  3. using namespace std;  
  4.   
  5. int main(void)  
  6. {  
  7.     int a[] = { 1, 5, 3, 7, 4, 2, 6 };  
  8.     int b[100];  
  9.     int size = sizeof( a ) / sizeof( a[0] );  
  10.     int mid = size / 2;  
  11.     nth_element( a, a+mid, a+size );  
  12.     int index1, index2, index3 = 0;  
  13.     for( index1 = 0, index2 = mid+1; index1 < mid; ++index1, ++index2 )  
  14.     {  
  15.         b[index3++] = a[index1];  
  16.         if( index2 < size )  
  17.             b[index3++] = a[index2];  
  18.     }  
  19.     b[index3] = a[mid];  
  20.   
  21.     forint i1 = 0; i1 < size; ++i1 )  
  22.     {  
  23.         cout << b[i1] << " ";  
  24.     }  
  25.     system("pause");  
  26.     return 0;  
  27. }  

3、10个人去看电影,其中5个人每人只有5元的钞票,另外5个人每个人只有10元的钞票,电影票的票价是5元,现在10个人排队去买票,问有多少种排列的方法,使得每个人在买票的时候售票员都有钱找。

C(2n,n)/(n+1)
4、编写C++中的两个类,一个只能在栈中分配空间,一个只能在堆中分配。

[cpp] view plain copy
  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4. // 只在堆上的类  
  5. // 私有化析构函数,通过一个public函数来进行实际的析构。  
  6. class HeapOnly  
  7. {  
  8. public:   
  9.     HeapOnly()  
  10.     {  
  11.         cout << "constructor." << endl;  
  12.     }  
  13.     void destroy () const  
  14.     {  
  15.         delete this;  
  16.     }  
  17. private:  
  18.     ~HeapOnly()  
  19.     {  
  20.     }  
  21. };  
  22.   
  23. int main(void)  
  24. {  
  25.     HeapOnly *p = new HeapOnly;  
  26.     p->destroy(); // HeapOnly h;   
  27.   
  28.     return 0;  
  29. }  
  30.   
  31.   
  32.   
  33. #include<iostream>  
  34. using namespace std;  
  35.   
  36.   
  37. // 私有重载new即可,限制了不能建立在new出的堆上  
  38. class StackOnly  
  39. {  
  40. public:  
  41.     StackOnly()  
  42.     {  
  43.         cout << "constructor." << endl;  
  44.     }  
  45.     ~StackOnly()  
  46.     {  
  47.         cout << "destructor." << endl;  
  48.     }  
  49. private:  
  50.     void* operator new (size_t);  
  51. };  
  52.   
  53. int main(void)   
  54. {  
  55.     StackOnly s;       //okay   
  56.     StackOnly *p = new StackOnly;      //wrong  
  57.     return 0;  
  58. }  
5、static_cast 和 dynamaic_cast 的用法。
[cpp] view plain copy
  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4. class Base  
  5. {  
  6. public:  
  7.     Base( ) {  }  
  8.     ~Base( ) {  }  
  9.     virtual void Output(int i)  
  10.     {  
  11.         cout<<"Base::Output value is "<<i<<endl;  
  12.     }  
  13. };  
  14.   
  15. class Derived1 : public  Base  
  16. {  
  17. public:  
  18.     Derived1( ) {  }  
  19.     ~Derived1( ) {  }  
  20.   
  21.     virtual void Output(int i)  
  22.     {  
  23.         cout<<"Derived1::Output value is "<<i<<endl;  
  24.     }  
  25.       
  26.     void Output2()  
  27.     {  
  28.         cout<<"Dervied1:Output2"<<endl;  
  29.     }  
  30.   
  31. };  
  32.   
  33. class Dervied2 : public Base  
  34. {  
  35. public:  
  36.     Dervied2() {  }  
  37.     ~Dervied2() {  }  
  38.     virtual void Output(int i)  
  39.     {  
  40.         cout<<"Dervied2::Output value is "<<i<<endl;  
  41.     }  
  42.   
  43.     void Output2()  
  44.     {  
  45.         cout<<"Dervied2:Output2"<<endl;  
  46.     }  
  47. };  
  48.   
  49. int main(void)  
  50. {  
  51.     Base *p = new  Dervied2;  
  52.     Derived1 *p1 = static_cast<Derived1*>(p);  
  53.     if(p1)  
  54.     {  
  55.         p1->Output(1);  
  56.         p1->Output2();  
  57.     }  
  58.     cout<<"=========================\n";  
  59.   
  60.     p1 = dynamic_cast<Derived1*>(p);  
  61.     if(p1)  
  62.     {  
  63.         p1->Output(2);  
  64.         p1->Output2();  
  65.     }  
  66.   
  67.     return 0;  
  68. }  
6、以下代码的输出是什么?
[cpp] view plain copy
  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4. class human  
  5. {  
  6. public:  
  7.     human()  
  8.     { human_num++;}; //默认构造函数  
  9.     static int human_num;     //静态成员  
  10.     ~human()  
  11.     {  
  12.         human_num--;  
  13.         print();  
  14.     }  
  15.     void print()  
  16.     {  
  17.         cout<<"human num is: "<<human_num<<endl;  
  18.     }  
  19. };  
  20.   
  21. int human::human_num = 0;   //类中静态数据成员在外部定义,仅定义一次  
  22.   
  23. human f1(human x)  
  24. {  
  25.     x.print();  
  26.     return x;  
  27. }  
  28.   
  29. int main(void)  
  30. {  
  31.     human h1;          // 调用默认构造函数,human_num变为1  
  32.     h1.print();        // 打印Human_man:1  
  33.     human h2 = f1(h1); // 先调用函数f1(),输出human_num:1,而后输出human_num为0,  
  34.     h2.print();        // 打印输出:human_num:0  
  35.     return 0;  
  36. }  
输出:
1
1
0
0
-1
-2
----------------------------
分析:
human h1;                       // 调用构造函数,---hum_num = 1;
h1.print();                       // 输出:"human is 1"
human h2 = f1(h1);         // 再调用f1(h1)的过程中,由于函数参数是按值传递对象,调用默认的复制构造函数,h2并没有调用定义的构造函数。
0 0
原创粉丝点击