四道面试题

来源:互联网 发布:中国工业总产值数据 编辑:程序博客网 时间:2024/04/29 22:00

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

sum = w[1];//小于Ak的元素的权重之和for(k=2;k<=n;k++){if(sum>1/2)return -1;          //没有找到符合要求的元素Akif(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。return A[k];sum += w[k];}

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

代码如下:

#include "iostream"#include "algorithm"using namespace std;int main(void){    int a[] = { 1, 5, 3, 7, 4, 2, 6 };    int b[100];    int size = sizeof( a ) / sizeof( a[0] );    int mid = size / 2;    nth_element( a, a+mid, a+size );    int index1, index2, index3 = 0;    for( index1 = 0, index2 = mid+1; index1 < mid; ++index1, ++index2 )    {        b[index3++] = a[index1];        if( index2 < size )            b[index3++] = a[index2];    }    b[index3] = a[mid];    for( int i1 = 0; i1 < size; ++i1 )    {        cout << b[i1] << " ";    }system("pause");    return 0;}

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

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

#include<iostream>using namespace std;// 只在堆上的类// 私有化析构函数,通过一个public函数来进行实际的析构。class HeapOnly{public: HeapOnly(){cout << "constructor." << endl;}void destroy () const{delete this;}private:~HeapOnly(){}};int main(void){HeapOnly *p = new HeapOnly;p->destroy(); // HeapOnly h; return 0;}#include<iostream>using namespace std;// 私有重载new即可,限制了不能建立在new出的堆上class StackOnly{public:StackOnly(){cout << "constructor." << endl;}~StackOnly(){cout << "destructor." << endl;}private:void* operator new (size_t);};int main(void) {StackOnly s;       //okay StackOnly *p = new StackOnly;      //wrongreturn 0;}
5、static_cast 和 dynamaic_cast 的用法。
#include<iostream>using namespace std;class Base{public:Base( ) {  }~Base( ) {  }virtual void Output(int i){cout<<"Base::Output value is "<<i<<endl;}};class Derived1 : public  Base{public:Derived1( ) {  }~Derived1( ) {  }virtual void Output(int i){cout<<"Derived1::Output value is "<<i<<endl;}void Output2(){cout<<"Dervied1:Output2"<<endl;}};class Dervied2 : public Base{public:Dervied2() {  }~Dervied2() {  }virtual void Output(int i){cout<<"Dervied2::Output value is "<<i<<endl;}void Output2(){cout<<"Dervied2:Output2"<<endl;}};int main(void){Base *p = new  Dervied2;Derived1 *p1 = static_cast<Derived1*>(p);if(p1){p1->Output(1);p1->Output2();}cout<<"=========================\n";p1 = dynamic_cast<Derived1*>(p);if(p1){p1->Output(2);p1->Output2();}return 0;}
6、以下代码的输出是什么?
#include<iostream>using namespace std;class human{public:human(){ human_num++;}; //默认构造函数static int human_num;     //静态成员~human(){human_num--;print();}void print(){cout<<"human num is: "<<human_num<<endl;}};int human::human_num = 0;   //类中静态数据成员在外部定义,仅定义一次human f1(human x){x.print();return x;}int main(void){human h1;          // 调用默认构造函数,human_num变为1h1.print();        // 打印Human_man:1human h2 = f1(h1); // 先调用函数f1(),输出human_num:1,而后输出human_num为0,h2.print();        // 打印输出:human_num:0return 0;}
输出:
1
1
0
0
-1
-2
----------------------------
分析:
human h1;                       // 调用构造函数,---hum_num = 1;
h1.print();                       // 输出:"human is 1"
human h2 = f1(h1);         // 再调用f1(h1)的过程中,由于函数参数是按值传递对象,调用默认的复制构造函数,h2并没有调用定义的构造函数。