2010实习生笔试题

1.已知两个链表head1和head2各自有序,请把它们合并成一个有序链表,要求用递归方法进行。

  1. #include <iostream>  
  2. #include <ctime>  
  3.   
  4. using namespace std;  
  5.   
  6.   
  7. struct node  
  8. {  
  9.     int value;  
  10.     node * next;  
  11. };  
  12.   
  13. node * make_link();  
  14. void display(node *);  
  15. void sort(node *);  
  16. node * merge(node *,node *);  
  17.   
  18. int main()  
  19. {  
  20.     srand(time(NULL));  
  21.     node* head1 = make_link();  
  22.     display(head1);  
  23.     sort(head1);  
  24.   
  25.     node* head2 = make_link();  
  26.     display(head2);  
  27.     sort(head2);  
  28.   
  29.     node *head = merge(head1,head2);  
  30.   
  31.     display(head);  
  32.     return 0;  
  33. }  
  34.   
  35. node * make_link()  
  36. {  
  37.       
  38.     node *head = new node();  
  39.     node *cur = head;  
  40.     cur->value = rand() % 10 + 1;  
  41.     for(int i= 1; i < 10; i++)  
  42.     {  
  43.         cur->next = new node();  
  44.         cur = cur->next;  
  45.         cur->value = rand() % 10 + 1;  
  46.     //  cout << cur->value << " ";  
  47.           
  48.     }  
  49.     return head;  
  50. }  
  51. void display(node *head)  
  52. {  
  53.     node *cur = head;  
  54.     while(cur)  
  55.     {  
  56.         cout << cur->value << " " ;  
  57.   
  58.         cur = cur->next;  
  59.     }  
  60.     cout << endl;  
  61. }  
  62.   
  63. void sort(node *head)  
  64. {  
  65.     //cout << "sorting" << endl;  
  66.     node *cur = head;  
  67.   
  68.     while(cur)  
  69.     {  
  70.         node *min = cur;  
  71.         node *cur2 = cur->next;  
  72.         while(cur2)  
  73.         {  
  74.             if(cur2->value < min->value)  
  75.             {  
  76.                 min = cur2;   
  77.             }  
  78.             cur2 = cur2->next;  
  79.         }  
  80.         int tem = cur->value;  
  81.         cur->value = min->value;  
  82.         min->value = tem;  
  83.         cur = cur->next;  
  84.     }  
  85.   
  86.   
  87. }  
  88. node * merge(node *h1,node *h2)  
  89. {  
  90.     if(!h1)  
  91.         return h2;  
  92.     else if(!h2)  
  93.         return h1;  
  94.     else  
  95.     {  
  96.         if(h1->value < h2->value)  
  97.         {  
  98.             h1->next = merge(h1->next,h2);  
  99.             return h1;  
  100.         }  
  101.         else{  
  102.             h2->next = merge(h1,h2->next);  
  103.             return h2;  
  104.         }  
  105.     }  
  106. }  

2.
给定一个二叉树,节点结构为struct Node{Node *Left,Node *Right,Node *Next;},其中Left、Right为左右节点指针,Next暂为空指针,写出函数,输入一个头结点指针,使得二叉树被层遍历(Level-By-Level Transversal)。用next指针链接并返回头结点指针。算法时间复杂度要求为O(n),空间复杂度要求为O(1)。

3.

逻辑推理题(谁是小偷)警察局抓了 a b c d四名偷窃嫌疑犯,其中一个是小偷,
a 说:我不是小偷
b 说:c是小偷
c说: 小偷肯定是d
d说: c冤枉人
已知四人中有三个人说了真话,一人说了假话。问到底谁是小偷?
请编写一段代码得出结果。(c或c++)

  1. //答案来自百度  
  2. #include<stdio.h>  
  3. #include<conio.h>  
  4. main()  
  5. {  
  6.     int a[5],h[5],i; //h数组代表4个人说的是真话还是假话,a数组表示a,b,c,d  
  7.     for (i=1;i<=4;i++)  
  8.     {  
  9.         h[1]=h[2]=h[3]=h[4]=1; //默认4个人都是真话  
  10.         a[1]=a[2]=a[3]=a[4]=0; //默认4个人都不是小偷  
  11.         h[i]=0; //选择其中1人说假话  
  12.         if (h[4]==1) {h[3]=0;} else {h[3]=1;} //因为这个是对其他真假话的判断,会影响下面的判断,所以要放最前面  
  13.         if (h[1]==1) {a[1]=0;} else {a[1]=1;}  
  14.         if (h[2]==1) {a[3]=1;} else {a[3]=0;}  
  15.         if (h[3]==1) {a[4]=1;} else {a[4]=0;}  
  16.         if ((a[1]+a[2]+a[3]+a[4])==1) //等于1就是只有1个小偷,其他都是0(不是小偷)   
  17.         {  
  18.             for (i=1;i<=4;i++)  
  19.             {  
  20.                 if (a[i]==1) {printf("%d\n",i);} //输出小偷  
  21.             }  
  22.         }   
  23.     }  
  24.     getch();  
  25.     return 0;  
  26. }  






4.inline函数的作用是

内联函数在每个调用点上被展开 因此 这样做可以消除函数调用相关的额外消耗



5.

某32位系统下, C++程序,请计算sizeof 的值(5分).
char str[] = "http://www.xxxxx.com"
char *p = str ;
int n = 10;
请计算
sizeof (str ) = ?(1)
sizeof ( p ) = ?(2)
sizeof ( n ) = ?(3)
void Foo ( char str[100]){
请计算
sizeof( str ) = ?(4)
}
void *p = malloc( 100 );
请计算
sizeof ( p ) = ?(5)
答:(1)17 (2)4 (3) 4 (4)4 (5)4


6.链表逆转

  1. <span style="font-size:10px;">#include <iostream>  
  2. using namespace std;  
  3.   
  4. struct node  
  5. {  
  6.     int value;  
  7.     node *link;  
  8. };  
  9.   
  10. void invert(node *&head)  
  11. {  
  12.     node *p = head, *q = 0, *r = 0;  
  13.     while(p)  
  14.     {  
  15.         r = q;  
  16.         q = p;  
  17.         p = p->link;  
  18.         q->link = r;  
  19.     }  
  20.     head = q;  
  21. }  
  22.   
  23. int main()  
  24. {  
  25.     node *head = new node();  
  26.     head->value = 1;  
  27.     node *tem = head->link = new node();  
  28.     tem->value = 2;  
  29.     tem->link = new node();  
  30.     tem->link->value = 3;  
  31.     invert(head);  
  32.     cout << "test";  
  33.   
  34. }  
  35. </span>  


7.求输出

(宏展开:你是什么我就展开成什么)

  1. #include <iostream>  
  2. using namespace std;  
  3. #define probe(x) (x * x)  
  4. int main()  
  5. {  
  6.     int i = 3;  
  7.     int j = probe(i++);  
  8.     int k = probe(++i);  
  9.     cout << j << k;  
  10.   
  11. }  

  1. #include <iostream>  
  2. using namespace std;  
  3. #define probe(x) (x * x)  
  4. int main()  
  5. {  
  6.     int i = 3;  
  7.     int j = probe(1+2);  
  8.     cout << j;  
  9. }  

8.11、 下面对枚举类型的描述正确的是_________。
A)枚举类型的定义为:enum {Monday,Tuesday,Wednesday,Thunday,Friday}Day;
B)在C++语言中,用户自定义的枚举类型的第一个常量的默认值是1
C)可以定义如下枚举类型:enum {Monday,Tuesday,Wednesday=5,mursday,Friday=5};
D)以上说法都不正确


9.extern 的作用

extern关键字的作用是什么? (D)

A. 声明外部链接 B. 声明外部头文件引用

C. 声明使用扩展C++语句 D. 声明外部成员函数、成员数据。

  1. #include <iostream>  
  2. using namespace std;  
  3. void test(int a)  
  4. {  
  5.     cout << "ex" << endl;  
  6. }  
  7. int main()  
  8. {  
  9.     void test();  
  10.     //test(1); //出错  
  11.     extern void test(int a);  
  12.     test(1);  
  13. }  
  14.   
  15. void test()  
  16. {  
  17.     cout << "in" << endl;  
  18. }  
  1. //声明了某个变量,之后使用的就是这个变量  
  2. #include <iostream>  
  3. using namespace std;  
  4. int count = 3;  
  5. int main(void)  
  6. {  
  7.     int i, sum, count = 2;  
  8.     for(i=0,sum=0; i<count; i+=2,count++)  
  9.     {  
  10.         static int count = 4;  
  11.         count++;  
  12.         if(i%2 == 0)  
  13.         {  
  14.             extern int count;  
  15.             count++;  
  16.             sum += count;  
  17.         }  
  18.         sum += count;  
  19.     }  
  20.     printf("%d %d\n",count, sum);  
  21.     return 0;  
  22. }  

10.下面关于构造函数和析构函数的描述,错误的是_________。 
 A) 析构函数中调用虚函数采用静态联编 
 B) 对虚析构函数的调用可以采用动态联编 
 C) 当基类的析构函数是虚函数时,其派生类的析构函数也一定是虚函数 (?只有虚函数才可以动态绑定)
 D) 构造函数可以声明为虚函数 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值