校园招聘笔试总结(转载)

1、指向数组的指针

     int (*ptr)[5]=(int (*)[5])100;
     int k=(int)(ptr+1);
     printf("%d\n",(int)(&(*(ptr+1))[2]));  //100+5*4+2*4=128

2、三维数组赋值与取值

     int a[3][4][5];
     int *p=(int *)a;
     for (int i=0;i<60;i++)
        p[i]=i;
     int (*s)[5]=&a[1][0];
     int m=a[1][7][1];  //56
     int n=*(s+1)[3];   //44
     printf("%d\n",a[1][7][1]*(*(s+1)[3]));

3、static和const同时修饰成员函数时,const默认是不起作用的。

    下面代码中,在static和const同时修饰的函数里面,只能访问static变量,并且可以改变该变量的值,说明这种函数是没有this指针的,并且const关键字也是无效的。

[html]  view plain copy
  1. #include "stdafx.h"  
  2. #include "stdlib.h"  
  3. #include <iostream.h>  
  4. #include <string.h>  
  5.   
  6. class Test  
  7. {  
  8. public:  
  9.     const static int func()  
  10.     {  
  11.         i+=100;  
  12.         return i;  
  13.     }  
  14.     static int i;  
  15. };  
  16. int Test::i=0;  
  17.   
  18. int main()  
  19. {  
  20.     Test t;   
  21.     for (int i=0;i<10;i++)  
  22.     {  
  23.         cout<<t.func()<<endl;  
  24.     }  
  25.   
  26.     return 0;  
  27. }  

4、运行下面的代码,会输出什么?

[html]  view plain copy
  1. #include "stdafx.h"  
  2. #include "stdlib.h"  
  3. #include <iostream.h>  
  4. #include <string.h>  
  5.   
  6. class A   
  7. {   
  8. private:   
  9.     int m_value;   
  10.       
  11. public:   
  12.     A(int value)   
  13.     {   
  14.         m_value = value;   
  15.     }   
  16.     void Print1()   
  17.     {   
  18.         printf("hello world");   
  19.     }   
  20.     void Print2()   
  21.     {   
  22.         printf("%d", m_value);   
  23.     }   
  24. };   
  25.   
  26. int main(int argc, char* argv[])   
  27. {   
  28.     A* pA = NULL; //this指针为NULL,当访问类的非static公有变量时,程序出错。  
  29.     pA->Print1();   
  30.     pA->Print2();     
  31.   
  32.     return 0;   
  33. }   

        Print1函数调用正常,输出hello world。因为调用 Print1 时,并不需要 pA 的地址,因为Print1 的函数地址是固定的。编译器会给 Print1 传入一个 this 指针,该指针为NULL,但在 Print1 中该 this 指针并没有用到。

        但是调用Print2函数时由于需要访问this指针以获得非static成员变量m_value的值,而由于pA等于NULL,所有this指针等于NULL,所以访问m_value时出错,导致程序崩溃。

5、假如将上题中的Print2函数定义为vitual类型,结果会是怎样呢?

         Print1 调用正常,打印出 hello world。Print1 的调用情况和上面的题目一样,不在赘述。

        但运行至 Print2 时,程序崩溃。由于 Print2 是虚函数。C++调用虚函数的时候,要根据实例(即this 指针指向的实例)中虚函数表指针得到虚函数表,再从虚函数表中找到函数的地址。由于这一步需要访问实例的地址(即 this 指针),而此时 this 指针为空指针,因此导致内存访问出错。

6、静态成员函数能不能同时也是虚函数? 
        不能。调用静态成员函数不要实例。但调用虚函数需要从一个实例中指向虚函数表的指针以得到函数的地址,因此调用虚函数需要一个实例。两者相互矛盾。

7、获取结构体中变量的相对偏移地址

        &(pA->m_value)的语意是求pA中变量m_value的地址(pA的地址为0加m_value的偏移量8),并不需要访问 pPoint 指向的内存。只要不访问非法的内存,程序就不会出错

看下面一个列子:

[html]  view plain copy
  1. #include "stdafx.h"  
  2. #include "stdlib.h"  
  3. #include <iostream.h>  
  4. #include <string.h>  
  5.   
  6. struct A   
  7. {   
  8.     int m_value;   
  9.     int m_value1;   
  10.     int m_value2;   
  11.     int m_value3;   
  12. };   
  13.   
  14. int main(int argc, char* argv[])   
  15. {   
  16.     A* pA = NULL;     
  17.     cout<<(int)(&(pA->m_value))<<endl;  //0  
  18.     cout<<(int)(&(pA->m_value1))<<endl; //4  
  19.     cout<<(int)(&(pA->m_value2))<<endl; //8  
  20.     cout<<(int)(&(pA->m_value3))<<endl; //12  
  21.   
  22.     //cout<<pA->m_value<<endl; //访问非法内存  
  23.   
  24.     return 0;   
  25. }   

8、下面代码输出什么?

[html]  view plain copy
  1. #include "stdafx.h"  
  2. #include "stdlib.h"  
  3. #include <iostream.h>  
  4. #include <string.h>  
  5.   
  6. class A   
  7. {   
  8. public:   
  9.     A()   
  10.     {   
  11.         Print();         }   
  12.     virtual void Print()   
  13.     {   
  14.         printf("A is constructed.\n");   
  15.     }   
  16. };   
  17.   
  18. class B: public A   
  19. {   
  20. public:   
  21.     B()   
  22.     {   
  23.         Print();   
  24.     }   
  25.       
  26.     virtual void Print()   
  27.     {   
  28.         printf("B is constructed.\n");   
  29.     }   
  30. };   
  31.   
  32. int main()   
  33. {   
  34.     A* pA = new B();   
  35.     delete pA;   
  36.       
  37.     return 0;   
  38. }   

        先后打印出两行:A is constructed. B is constructed。

        调用 B 的构造函数时,先会调用 B 的基类及A 的构造函数。然后在A 的构造函数里调用 Print。由于此时实例的类型 B 的部分还没有构造好,本质上它只是 A 的一个实例,他的虚函数表指针指向的是类型 A 的虚函数表。因此此时调用的Print 是 A::Print,而不是 B::Print。接着调用类型 B 的构造函数,并调用 Print。此时已经开始构造 B,因此此时调用的 Print是 B::Print。

9、成员变量的初始化顺序

        在 C++中,成员变量的初始化顺序与变量在类型中的申明顺序相同,而与它们在构造函数的初始化列表中的顺序无关。

10、C++类中复制构造函数的形参只能是引用

        假如复制构造函数中传入的参数是传值,把形参拷贝到实参会调用复制构造函数。因此如果允许复制构造函数传值,那么会形成永无休止的递归并造成栈溢出。因此 C++的标准不允许复制构造函数传值参数,而必须是传引用或者常量引用。

11、将某一位清零

 int a=31;
 int n=4;
 a&=~(1<<n);
 cout<<a<<endl;  //15
12、整数按照字符串格式输出,会得到什么结果呢?

 int i=0;
 printf("i=%s",i);  //i=<null>

 int i=10;
 printf("i=%s",i);  //程序崩溃

13、运算符优先级(~、+、<<)

 int i=1+2<<3;     //i= (1+2)<<3;
 printf("i=%d",i);  //24

14、下面输出

[html]  view plain copy
  1. #include "stdafx.h"  
  2. #include "stdlib.h"  
  3. #include <iostream.h>  
  4. #include <string.h>  
  5.   
  6. typedef struct Test  
  7. {  
  8.     char a;  
  9.     short b;  
  10. }test;  
  11.   
  12. int main()   
  13. {   
  14.     test a={0,0};  
  15.     test b={0};  
  16.     test c;  
  17.     memset(&c,0x00,sizeof(a));  
  18.     cout<<a.a<<" "<<a.b<<endl;  
  19.     cout<<a.a<<" "<<b.b<<endl;  
  20.     cout<<c.a<<" "<<c.b<<endl;  
  21.   
  22.     return 0;   
  23. }   

15、在64位系统下,int类型与指针占几个字节

        int类型依然是4个字节;

        指针类型占8个字节。

16、关于TTL(生存时间)

        指定数据包被路由器丢弃之前允许通过的网段(路由器、服务器)数量,最大值255。

        当数据包传送到一个路由器之后,TTL就自动减1,如果减到0了还是没有传送到目的主机,那么就自动丢失。

17、内存对齐宏

        #pragma pack(push,n)

18、hash算法

        哈希算法将任意长度的二进制值映射为固定长度的较小二进制值,这个小的二进制值称为哈希值。哈希值是一段数据唯一且极其紧凑的数值表示形式。如果散列一段明文而且哪怕只更改该段落的一个字母,随后的哈希都将产生不同的值。要找到散列为同一个值的两个不同的输入,在计算上是不可能的,所以数据的哈希值可以检验数据的完整性。

越是简单的程序设计,越要多一个心眼,从时间复杂度、空间复杂度去优化程序设计,同时保证程序良好的阅读和可扩展性。

19、请使用虚析构函数

下面的程序由于未使用虚析构函数,因此在析构对象时,仅析构了对象的基类部分,而没有析构派生类部分。

[html]  view plain copy
  1. #include "stdafx.h"  
  2. #include "stdlib.h"  
  3. #include <iostream>  
  4. #include <string>  
  5.   
  6. class A   
  7. {   
  8. public:   
  9.     A()   
  10.     {   
  11.         std::cout << "A is created." << std::endl;   
  12.     }     
  13.     ~A()   
  14.     {   
  15.         std::cout << "A is deleted." << std::endl;   
  16.     }   
  17. };   
  18.   
  19. class B : public A {   
  20. public:   
  21.     B()   
  22.     {   
  23.         std::cout << "B is created." << std::endl;   
  24.     }     
  25.     ~B()   
  26.     {   
  27.         std::cout << "B is deleted." << std::endl;   
  28.     }   
  29. };   
  30.   
  31. int main(int argc, char* argv[])   
  32. {   
  33.     A* pA = new B();   
  34.     delete pA;   
  35.       
  36.     return 0;   
  37. }  

20、求两个整数的和,不得使用+、-、*、/

先看一个例子,5 的二进制是 101,17 的二进制 10001。试着把计算分成三步:

        第一步各位相加但不计进位,得到的结果是 10100(最后一位两个数都是 1,相加的结果是二进制的 10。这一步不计进位,因此结果仍然是 0);

        第二步记下进位。在这个例子中只在最后一位相加时产生一个进位,结果是二进制的 10;

        第三步把前两步的结果相加,得到的结果是 10110,正好是 22。由此可见三步走的策略对二进制也是管用的。 
接下来我们试着把二进制上的加法用位运算来替代。

        第一步不考虑进位,对每一位相加。0 加 0与 1 加 1 的结果都 0,0 加 1 与 1 加 0 的结果都是 1。我们可以注意到,这和异或的结果是一样的。对异或而言,0 和 0、1 和 1 异或的结果是 0,而 0 和 1、1 和 0 的异或结果是 1。

        接着考虑第二步进位,对 0加 0、0 加 1、1 加 0 而言,都不会产生进位,只有 1 加 1 时,会向前产生一个进位。此时我们可以想象成是两个数先做位与运算,然后再向左移动一位。只有两个数都是 1 的时候,位与得到的结果是 1,其余都是 0。

        第三步把前两个步骤的结果相加。如果我们定义一个函数 AddWithoutArithmetic,第三步就相当于输入前两步骤的结果来递归调用自己。

        根据上面,不难写出下面的代码:

[html]  view plain copy
  1. #include "stdafx.h"  
  2. #include "stdlib.h"  
  3. #include <iostream>  
  4. #include <string>  
  5. using namespace std;  
  6.   
  7. int AddWithoutArithmetic(int num1, int num2)   
  8. {   
  9.     if(num2 == 0)   
  10.         return num1;   
  11.       
  12.     int sum = num1 ^ num2;             //相加  
  13.     int carry = (num1 & num2) << 1;    //进位  
  14.       
  15.     return AddWithoutArithmetic(sum, carry);  //递归运算  
  16. }   
  17.   
  18. int main(int argc, char* argv[])   
  19. {   
  20.     int x=10,y=20;  
  21.     cout<<AddWithoutArithmetic(x,y)<<endl;  
  22.   
  23.     return 0;   
  24. }   

21、某公司有几万名员工,请完成一个时间复杂度为 O(n)的算法对该公司员工的年龄作排序,可使用O(1)的辅助空间。

        思路: 题目特别强调是对一个公司的员工的年龄作排序。员工的数目虽然有几万人,但这几万员工的
年龄却只有几十种可能。上班早的人一般也要等到将近二十岁才上班,一般人再晚到了六七十岁也不得不 由于年龄总共只有几十种可能,我们可以很方便地统计出每一个年龄里有多少名员工。举个简单的例子,假设总共有 5 个员工,他们的年龄分别是 25、24、26、24、25。我们统计出他们的年龄,24岁的有两个,25 岁的也有两个,26 岁的一个。那么我们根据年龄排序的结果就是:24、24、25、25、26,即在表示年龄的数组里写出两个 24、两个 25 和一个 26。其实这就是技术排序的原理

        故将计数排序稍加改进,得到下面程序:

[html]  view plain copy
  1. #include "stdafx.h"  
  2. #include "stdlib.h"  
  3. #include <iostream>  
  4. #include <string>  
  5. using namespace std;  
  6.   
  7.   
  8. const int oldeAge=150;  
  9. void SortAges(int *ages,int n)  
  10. {  
  11.     int timesOfAges[oldeAge];  
  12.     //辅助数组初始化  
  13.     for (int i=0;i<oldeAge;i++)  
  14.         timesOfAges[i]=0;  
  15.     //使用辅助数组计数  
  16.     for (i=0;i<n;i++)  
  17.     {  
  18.         if(ages[i]>150)  
  19.             ages[i]=149;  
  20.         timesOfAges[ages[i]]++;  
  21.     }  
  22.     //遍历辅助数组,将元素按排序顺序写回原数组  
  23.     int index=0;  
  24.     for (i=0;i<oldeAge;i++)  
  25.     {  
  26.         while(timesOfAges[i]>0)  
  27.         {  
  28.             ages[index]=i;  
  29.             index++;  
  30.             timesOfAges[i]--;  
  31.         }  
  32.     }  
  33. }  
  34.   
  35. int main(int argc, char* argv[])   
  36. {   
  37.     int ages[]={13,43,35,7,58,6,32,9,65,22,47,45,7,47,78,5,32,68,67,54,34,43,56,45};  
  38.     SortAges(ages,sizeof(ages)/4);  
  39.     for (int i=0;i<sizeof(ages)/4;i++)  
  40.     {  
  41.         cout<<ages[i]<<" ";  
  42.     }  
  43.     cout<<endl;  
  44.   
  45.     return 0;   
  46. }   

22、判断二叉树是不是平衡二叉树

        首先我们需要求二叉树的深度。求出二叉树的深度之后,我们很容易就能想到一个思路:在遍历树的每个结点的时候,调用函数TreeDepth得到它的左右子树的深度。如果每个结点的左右子树的深度相差都不超过 1,按照定义它就是一棵平衡的二叉树。

        这种思路对应的代码如下:

[html]  view plain copy
  1. bool IsBalanced(BinaryTreeNode* pRoot)   
  2. {   
  3.     if(pRoot == NULL)   
  4.         return true;   
  5.       
  6.     int left = TreeDepth(pRoot->m_pLeft);   
  7.     int right = TreeDepth(pRoot->m_pRight);   
  8.     int diff = left - right;   
  9.     if(diff > 1 || diff < -1)   
  10.         return false;   
  11.       
  12.     return IsBalanced(pRoot->m_pLeft) && IsBalanced(pRoot->m_pRight);   
  13. }   

23、输入一个字符串,输出该字符串中字符的所有组合。

        举个例子,如果输入 abc,它的组合有 a、b、c、ab、ac、bc、abc。

        假设我们想在长度为 n 的字符串中求 m 个字符的组合。我们先从头扫描字符串的第一个字符。针对第一个字符,我们有两种选择:一是把这个字符放到组合中去,接下来我们需要在剩下的 n-1 个字符中选取 m-1 个字符;二是不把这个字符放到组合中去,接下来我们需要在剩下的 n-1 个字符中选择 m 个字符。这两种选择都很容易用递归实现。下面是这种思路的参考代码:

[html]  view plain copy
  1. #include "stdafx.h"  
  2. #include "stdlib.h"  
  3. #include <iostream>  
  4. #include <string>  
  5. #include <vector>  
  6. using namespace std;  
  7.   
  8. void Combination(char* string, int number, vector<char>& result)   
  9. {   
  10.     if(number == 0)   
  11.     {   
  12.         vector<char>::iterator iter = result.begin();   
  13.         for(; iter < result.end(); ++ iter)   
  14.             printf("%c", *iter);   
  15.         printf("\n");   
  16.           
  17.         return;   
  18.     }   
  19.       
  20.     if(*string == '\0')   
  21.         return;   
  22.       
  23.     result.push_back(*string);       
  24.     Combination(string + 1, number - 1, result);   
  25.     result.pop_back();   
  26.       
  27.     Combination(string + 1, number, result);   
  28. }   
  29.   
  30. void Combination(char* string)   
  31. {   
  32.     if(string == NULL)   
  33.         return;   
  34.     
  35.     int length = strlen(string);   
  36.     vector<char> result;   
  37.     for(int i = 1; i <= length; ++ i)   
  38.     {   
  39.         Combination(string, i, result);   
  40.     }   
  41. }   
  42.     
  43. int main(int argc, char* argv[])   
  44. {   
  45.     Combination("abcd");  
  46.   
  47.     return 0;   
  48. }   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值