最新c++面试127题看你会多少3.docx
- 文档编号:2427701
- 上传时间:2022-10-29
- 格式:DOCX
- 页数:10
- 大小:19.67KB
最新c++面试127题看你会多少3.docx
《最新c++面试127题看你会多少3.docx》由会员分享,可在线阅读,更多相关《最新c++面试127题看你会多少3.docx(10页珍藏版)》请在冰豆网上搜索。
最新c++面试127题看你会多少3
41.文件中有一组整数,要求排序后输出到另一个文件中
答案:
#include
#include
usingnamespacestd;
voidOrder(vector
{
intcount=data.size();
inttag=false;//设置是否需要继续冒泡的标志位
for(inti=0;i { for(intj=0;j { if(data[j]>data[j+1]) { tag=true; inttemp=data[j]; data[j]=data[j+1]; data[j+1]=temp; } } if(! tag) break; } } voidmain(void) { vector ifstreamin("c: \data.txt"); if(! in) { cout<<"fileerror! "; exit (1); } inttemp; while(! in.eof()) { in>>temp; data.push_back(temp); } in.close();//关闭输入文件流 Order(data); ofstreamout("c: \result.txt"); if(! out) { cout<<"fileerror! "; exit (1); } for(i=0;i out< out.close();//关闭输出文件流 } 42.链表题: 一个链表的结点结构structNode { intdata; Node*next; }; typedefstructNodeNode; (1)已知链表的头结点head,写一个函数把这个链表逆序(Intel) Node*ReverseList(Node*head)//链表逆序 { if(head==NULL||head->next==NULL) returnhead; Node*p1=head; Node*p2=p1->next; Node*p3=p2->next; p1->next=NULL; while(p3! =NULL) { p2->next=p1; p1=p2; p2=p3; p3=p3->next; } p2->next=p1; head=p2; returnhead; } (2)已知两个链表head1和head2各自有序,请把它们合并成一个链表依然有序。 (保留所有结点,即便大小相同) Node*Merge(Node*head1,Node*head2) { if(head1==NULL) returnhead2; if(head2==NULL) returnhead1; Node*head=NULL; Node*p1=NULL; Node*p2=NULL; if(head1->data { head=head1; p1=head1->next; p2=head2; } else { head=head2; p2=head2->next; p1=head1; } Node*pcurrent=head; while(p1! =NULL&&p2! =NULL) { if(p1->data<=p2->data) { pcurrent->next=p1; pcurrent=p1; p1=p1->next; } else { pcurrent->next=p2; pcurrent=p2; p2=p2->next; } } if(p1! =NULL) pcurrent->next=p1; if(p2! =NULL) pcurrent->next=p2; returnhead; } (3)已知两个链表head1和head2各自有序,请把它们合并成一个链表依然有序,这次要求用递归方法进行。 (Autodesk) 答案: Node*MergeRecursive(Node*head1,Node*head2) { if(head1==NULL) returnhead2; if(head2==NULL) returnhead1; Node*head=NULL; if(head1->data { head=head1; head->next=MergeRecursive(head1->next,head2); } else { head=head2; head->next=MergeRecursive(head1,head2->next); } returnhead; ---------- 41.分析一下这段程序的输出(Autodesk) classB { public: B() { cout<<"defaultconstructor"< } ~B() { cout<<"destructed"< } B(inti): data(i) //B(int)worksasaconverter(int->instanceof B) { cout<<"constructedbyparameter"< } private: intdata; }; BPlay(Bb) { returnb; } (1) results: intmain(intargc,char*argv[]) constructedbyparameter5 { destructed B(5)形参析构 Bt1=Play(5);Bt2=Play(t1); destructed t1形参析构 return0; destructed t2 注意顺序! } destructed t1 (2) results: intmain(intargc,char*argv[]) constructedbyparameter5 { destructed B(5)形参析构 Bt1=Play(5);Bt2=Play(10); constructedbyparameter10 return0; destructed B(10)形参析构 } destructed t2 注意顺序! destructed t1 43.写一个函数找出一个整数数组中,第二大的数(microsoft)答案: constintMINNUMBER=-32767; intfind_sec_max(intdata[],intcount) { intmaxnumber=data[0]; intsec_max=MINNUMBER; for(inti=1;i { if(data>maxnumber) { sec_max=maxnumber; maxnumber=data; } else { if(data>sec_max) sec_max=data; } } returnsec_max; } 44.写一个在一个字符串(n)中寻找一个子串(m)第一个位置的函数。 KMP算法效率最好,时间复杂度是O(n+m),详见: 46.多重继承的内存分配问题: 比如有classA: publicclassB,publicclassC{}那么A的内存结构大致是怎么样的? 这个是compiler-dependent的,不同的实现其细节可能不同。 如果不考虑有虚函数、虚继承的话就相当简单;否则的话,相当复杂。 可以参考《深入探索C++对象模型》,或者: 47.如何判断一个单链表是有环的? (注意不能用标志位,最多只能用两个额外指针)structnode{charval;node*next;} boolcheck(constnode*head){}//returnfalse: 无环;true: 有环一种O(n)的办法就是(搞两个指针,一个每次递增一步,一个每次递增两步,如果有环的话两者必然重合,反之亦然): boolcheck(constnode*head) { if(head==NULL) returnfalse; node*low=head,*fast=head->next; while(fast! =NULL&&fast->next! =NULL) { low=low->next; fast=fast->next->next; if(low==fast)returntrue; } returnfalse; } 48.指针找错题分析这些面试题,本身包含很强的趣味性;而作为一名研发人员,通过对这些面试题的深入剖析则可进一步增强自身的内功。 2.找错题试题1: 以下是引用片段: voidtest1() //数组越界 { charstring[10]; char*str1="0123456789"; strcpy(string,str1); } 试题2: 以下是引用片段:
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 最新 c+ 面试 127 多少