当前位置:文档之家› 程序员面试宝典(C++例题)

程序员面试宝典(C++例题)

{
while(pnode!=NULL)
{
if(pnode->child!=NULL){
node* p0=pnode->child,*p1=p0;
while(p1->next!=NULL) p1 = p1->next;
node*pp1 = pnode->next;
pnode->next = p0; p1->next = pp1;
(2)从首字母开始,判断其是否还有相同字母,类似排序,O(n2)
5.4删除特定字符
对remove建立hash数组,字符为key;遍历str字符串,如果字符在remove中则不拷贝,否则拷贝前移。效率在O(n+m)。
5.5颠倒单词的出现顺序
对字符串进行反向遍历,找到空格则复制单词。
5.6整数/字符串之间的转换
对这个链表进行扁平化,使全体节点都出现在一个只有一个层次的双向链表里。已知条件只有原多层次双向链表的第一层次的头指针和尾指针。下面是各节点的C++语言struct定义:
struct node{
node *next;
node *prev;
node *child;
int value;
} ;
void ExpandList(node* pnode)
9.4导火索
ANS: burn wire1 from 2 endpoint and burn wire2 from 1 endpoint meanwhile,
When the wire1 burn out, burn wire2 the other endpoint.
It will spend 45 minutes when the wire2 burn out.
stl::pair<stl::set<node*>::iterator, bool> pl;
pl = pset.insert(p);
while(p->next!=NULL){
p=p->next;
pl = pset.insert(p);
if(pl.second==false) return true;
7.12生产者/消费者问题
第8章与技术、测量、排序有关的智力题
8.1开锁
ANS: take an example of 10, emulate the open and close, and then find the rule.
You will find that all the opened number is a square number, so the opened number is:
class Stack{
struct Node{
Type idata;
Node* pnext;
};
public:
Stack():isize(0),phead(NULL){}
~Stack(){
while(phead!=NULL){
Node*p = phead;
phead = phead->pnext;
Node* p = phead;
phead = phead->pnext;
delete p;
--isize;
ret = true;
}
return ret;
}
int Size(){
return isize;
}
private:
int isize;
Node* phead;
};
template<class Type>
9.5躲火车
ANS:
第10章计算机基础知识
10.3 C++和Java
10.4头文件
10.5 C存储类别
10.6 Friend类
10.7类与结构的区别
10.8父类与子类
10.9参数传递
10.10宏与Inline函数
10.11继承
10.12面向对象的程序设计
10.13与线程有关的程序设计问题
10.14废弃内存的自动回收
Delete函数只有一个输入参数,他就是那个将被删除的元素。InsertAfter函数由两个输入参数,第二个输入参数给出了新元素的取值,它将被插入到第一个输入参数所指定的元素的后面。当需要把新元素插入到链表的开头作为新的头元素时,函数InsertAfter的第一个输入参数(即被声明为element类型的那个输入参数)将被设置为NULL。如果执行成功,这两个函数将返回“1”;如果不成功,将返回“0”。
tout = phead->idata;
Node* p = phead;
phead = phead->pnext;
delete p;
--isize;
ret = true;
}
return ret;
}
int Size(){
return isize;
}
private:
int isize;
Node* phead;
element *head, *tail;
int Delete(element* elem)
{
int ret = 0;
if(elem==NULL) return ret;
if(elem==head)
{
if(head==tail)
head=(tail=NULL);
else
head = head->next;
};
3.5链表的尾指针
说明:有一个单向链表,它的元素全都是些整数。head和tail分别指向该链表第一个元素(即头元素)和最后一个元素(即尾元素)的全局性指针。请实现调用接口如下所示的两个C语言函数:
int Delete(element *elem);
int InsertAfter(element *elem, int data);
delete p1;
ret = 1;
}
return ret;
}
element *head,*tail;
int InsertAfter(element* elem, int data)
{
int ret = 0;
element *p = new element;
p->data = data;
p->next = NULL;
{
free(head);
head = head->next;
}
解答:
void RemoveHead(node *&head)
{
if(head==NULL) return;
node* p = head;
head = head->next;
free(p);
}
3.7链表中的倒数第m个元素
说明:给定一个单向链表,请设计一个既节省时间又节省空间的算法来找出该链表中的倒数第m个元素。实现这个算法,并为可能出现的特例情况安排好处理措施。“倒数第m个元素”是这样规定的:当m=0时,链表的最后一个元素(尾元素)将被返回。
8.3过桥
ANS: (2+1)(1)(5+10)(2)(2+1)==17
8.4找石头
ANS: 3 3 2
第9章与图形和空间有关的智力题
9.1船和码头
ANS:绳子rope
9.2数方块
ANS: 3*3*3-1*1*1=8
ANS: 4*4*4-2*2*2 = 56
9.3狐狸与鸭子
ANS: the duck could fly.
解答:
node* FindInvM(node* head, int m)
{
if(head==NULL) return NULL;
node* pm=head,*p=head;
int ipm=0;
while(p->next!=NULL)
{
p = p->next;
if(ipm<m) ++ipm;
else pm = pm->next;
if(elem==NULL)
{
if(head==NULL)
head = (tail=p);
else
{
p->next = head;
head = p;
}
ret = 1;
return ret;
}
else
{
element* p1=head;
while(p1!=NULL)
{
if(p1==elem) break;
1,4,9,16,25,36,49,64,81,100.
8.2三个开关
ANS: you need to touch the lamp. Firstly open the lamp for 1minute, then closed it, and open the next one. You should go to the room and touch the closed lamp to determine which the first warm lamp is.
}
void Push(int i){
Node* p = new Node;
p->idata = i;
p->pnext = phead;
phead = p;
++isize;
}
bool Pop(int& iout){
bool ret = false;
if(isize>0){
iout = phead->idata;
delete p;
相关主题