1.已知两个链表head1和head2各自有序,请把它们合并成一个有序链表,要求用递归方法进行。
- #include <iostream>
- #include <ctime>
- using namespace std;
- struct node
- {
- int value;
- node * next;
- };
- node * make_link();
- void display(node *);
- void sort(node *);
- node * merge(node *,node *);
- int main()
- {
- srand(time(NULL));
- node* head1 = make_link();
- display(head1);
- sort(head1);
- node* head2 = make_link();
- display(head2);
- sort(head2);
- node *head = merge(head1,head2);
- display(head);
- return 0;
- }
- node * make_link()
- {
- node *head = new node();
- node *cur = head;
- cur->value = rand() % 10 + 1;
- for(int i= 1; i < 10; i++)
- {
- cur->next = new node();
- cur = cur->next;
- cur->value = rand() % 10 + 1;
- // cout << cur->value << " ";
- }
- return head;
- }
- void display(node *head)
- {
- node *cur = head;
- while(cur)
- {
- cout << cur->value << " " ;
- cur = cur->next;
- }
- cout << endl;
- }
- void sort(node *head)
- {
- //cout << "sorting" << endl;
- node *cur = head;
- while(cur)
- {
- node *min = cur;
- node *cur2 = cur->next;
- while(cur2)
- {
- if(cur2->value < min->value)
- {
- min = cur2;
- }
- cur2 = cur2->next;
- }
- int tem = cur->value;
- cur->value = min->value;
- min->value = tem;
- cur = cur->next;
- }
- }
- node * merge(node *h1,node *h2)
- {
- if(!h1)
- return h2;
- else if(!h2)
- return h1;
- else
- {
- if(h1->value < h2->value)
- {
- h1->next = merge(h1->next,h2);
- return h1;
- }
- else{
- h2->next = merge(h1,h2->next);
- return h2;
- }
- }
- }
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++)
- //答案来自百度
- #include<stdio.h>
- #include<conio.h>
- main()
- {
- int a[5],h[5],i; //h数组代表4个人说的是真话还是假话,a数组表示a,b,c,d
- for (i=1;i<=4;i++)
- {
- h[1]=h[2]=h[3]=h[4]=1; //默认4个人都是真话
- a[1]=a[2]=a[3]=a[4]=0; //默认4个人都不是小偷
- h[i]=0; //选择其中1人说假话
- if (h[4]==1) {h[3]=0;} else {h[3]=1;} //因为这个是对其他真假话的判断,会影响下面的判断,所以要放最前面
- if (h[1]==1) {a[1]=0;} else {a[1]=1;}
- if (h[2]==1) {a[3]=1;} else {a[3]=0;}
- if (h[3]==1) {a[4]=1;} else {a[4]=0;}
- if ((a[1]+a[2]+a[3]+a[4])==1) //等于1就是只有1个小偷,其他都是0(不是小偷)
- {
- for (i=1;i<=4;i++)
- {
- if (a[i]==1) {printf("%d\n",i);} //输出小偷
- }
- }
- }
- getch();
- return 0;
- }
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.链表逆转
- <span style="font-size:10px;">#include <iostream>
- using namespace std;
- struct node
- {
- int value;
- node *link;
- };
- void invert(node *&head)
- {
- node *p = head, *q = 0, *r = 0;
- while(p)
- {
- r = q;
- q = p;
- p = p->link;
- q->link = r;
- }
- head = q;
- }
- int main()
- {
- node *head = new node();
- head->value = 1;
- node *tem = head->link = new node();
- tem->value = 2;
- tem->link = new node();
- tem->link->value = 3;
- invert(head);
- cout << "test";
- }
- </span>
7.求输出
(宏展开:你是什么我就展开成什么)
- #include <iostream>
- using namespace std;
- #define probe(x) (x * x)
- int main()
- {
- int i = 3;
- int j = probe(i++);
- int k = probe(++i);
- cout << j << k;
- }
- #include <iostream>
- using namespace std;
- #define probe(x) (x * x)
- int main()
- {
- int i = 3;
- int j = probe(1+2);
- cout << j;
- }
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. 声明外部成员函数、成员数据。
- #include <iostream>
- using namespace std;
- void test(int a)
- {
- cout << "ex" << endl;
- }
- int main()
- {
- void test();
- //test(1); //出错
- extern void test(int a);
- test(1);
- }
- void test()
- {
- cout << "in" << endl;
- }
- //声明了某个变量,之后使用的就是这个变量
- #include <iostream>
- using namespace std;
- int count = 3;
- int main(void)
- {
- int i, sum, count = 2;
- for(i=0,sum=0; i<count; i+=2,count++)
- {
- static int count = 4;
- count++;
- if(i%2 == 0)
- {
- extern int count;
- count++;
- sum += count;
- }
- sum += count;
- }
- printf("%d %d\n",count, sum);
- return 0;
- }
10.下面关于构造函数和析构函数的描述,错误的是_________。
A) 析构函数中调用虚函数采用静态联编
B) 对虚析构函数的调用可以采用动态联编
C) 当基类的析构函数是虚函数时,其派生类的析构函数也一定是虚函数 (?只有虚函数才可以动态绑定)
D) 构造函数可以声明为虚函数