程序员面试经典试题.docx
- 文档编号:26020004
- 上传时间:2023-06-17
- 格式:DOCX
- 页数:7
- 大小:17.28KB
程序员面试经典试题.docx
《程序员面试经典试题.docx》由会员分享,可在线阅读,更多相关《程序员面试经典试题.docx(7页珍藏版)》请在冰豆网上搜索。
程序员面试经典试题
1、现有1000个苹果,10个盒子,问各个盒子内应该分别放入多少个苹果,才能使得用户要买任意1至1000之间的一个苹果数,都可以给他(卖的时候是整个盒子卖,不能拆盒子的包装)。
2、请仔细阅读下面的资料:
1)材料一:
CArray
template
publicCObject
Parameters:
TYPE
Templateparameterspecifyingthetypeofobjectsstoredinthearray.
TYPEisaparameterthatisreturnedbyCArray.
ARG_TYPE
Templateparameterspecifyingtheargumenttypeusedtoaccessobjectsstoredinthearray.
OftenareferencetoTYPE.ARG_TYPEisaparameterthatispassedtoCArray.
Remarks:
TheCArrayclasssupportsarraysthatarearesimilartoCarrays,butcandynamicallyshrinkandgrowasnecessary.
Arrayindexesalwaysstartatposition0.Youcandecidewhethertofixtheupperboundorallowthearraytoexpandwhenyouaddelementspastthecurrentbound.Memoryisallocatedcontiguouslytotheupperbound,evenifsomeelementsarenull.
intCArray:
:
Add(ARG_TYPEnewElement);
ReturnValue:
Theindexoftheaddedelement.
Parameters:
ARG_TYPE
Templateparameterspecifyingthetypeofargumentsreferencingelementsinthisarray.
newElement
Theelementtobeaddedtothisarray.
TYPE&CArray:
:
operator[](intnIndex);
Parameters:
TYPE
Templateparameterspecifyingthetypeofelementsinthisarray.
nIndex
Indexoftheelementtobeaccessed.
Remarks:
Returnsthearrayreferenceofelementatthespecifiedindex.
2)材料二:
CList
template
publicCObject
Parameters:
TYPE
Typeofobjectstoredinthelist.
ARG_TYPE
Typeusedtoreferenceobjectsstoredinthelist.Canbeareference.
Remarks:
TheCListclasssupportsorderedlistsofnonuniqueobjectsaccessiblesequentiallyorbyvalue.
CListlistsbehavelikedoubly-linkedlists.
voidCList:
:
AddTail(ARG_TYPEnewElement);
Parameters:
ARG_TYPE
Templateparameterspecifyingthetypeofthelistelement(canbeareference).
newElement
Theelementtobeaddedtothislist.
Remarks:
Addsanewelementorlistofelementstothetailofthislist.Thelistcanbeemptybeforetheoperation.
3)材料三:
realloc
realloc
Reallocatememoryblocks.
void*realloc(void*memblock,size_tsize);
ReturnValue:
Thereturnvaluepointstoastoragespacethatisguaranteedtobesuitablyalignedforstorageofanytypeofobject.Togetapointertoatypeotherthanvoid,useatypecastonthereturnvalue.
Remarks:
Thesizeargumentgivesthenewsizeoftheblock,inbytes.Thecontentsoftheblockareunchangeduptotheshorterofthenewandoldsizes,althoughthenewblockcanbeinadifferentlocation.Becausethenewblockcanbeinanewmemorylocation,thepointerreturnedbyreallocisnotguaranteedtobethepointerpassedthroughthememblockargument.
4)请指出下面这段代码可能会出现的问题
CList
CArray
voidAddNewValue(constVARIANT&newValue)
{
g_ValueList.AddTail(&g_ValuePool[g_ValuePool.Add(newValue)]);
}
3、有一无符号整型数组,大小为10,初始的数值随机,但在[0,99]之间。
请用C语言写一个过滤程序,令数组内的数据互不相等。
说明:
1.若数组内有相等的数据,可令某一数值加1或减1作出偏移,直至不等为止。
2.数组内的数据只能在[0,99]之间。
3.保持数组内的数据位置不变,即对应下标不变。
4、按要求编写以下函数。
功能:
将给定缓冲区中的#字符移到字符串尾部
函数名称:
ChangeToTail
入口参数:
pSZ指向缓冲区的指针,nSize缓冲区长度
出口:
pSZ所指缓冲区中的#字符被移到缓冲区尾部
返回值:
在出口缓冲区中第一个#的位置,若缓冲区中无#字符则返回-1
说明:
如传入#W#W#W#WW#,10则传出时应转换为WWWWW#####并且返回值为5
intChangeToTail(BYTE*pSZ,UINTnSize)
{
//Todo:
请在此加入您的代码
}
5、在金山,有一个非常经典有趣的游戏,称为杀人游戏。
此游戏角色有:
好人(m人)、坏人(n人)、村长(1人)、裁判(1人)。
角色采用一定方式(如:
抓阄)分配。
村长、裁判两个角色是公开的,而好人、坏人两个角色则只要裁判和本人心知肚明。
其玩法如下:
游戏开始了,裁判说:
“天黑了”,这是所有其他角色都低头闭上眼睛(不准作弊!
)。
然后裁判说:
“坏人开始活动”,此时坏人抬起头,并相互商议,杀死一个好人。
然后裁判说:
“天亮了”,此时所有人抬头,被杀死的那个好人宣布出局。
剩下的人在村长的主持下,开始判断杀人凶手。
每个人可以根据各人的表情反应,判断并提议杀死自己心目中的坏人。
不过最终的裁决权属于村长,综合大家的意见杀死一人。
此时裁判宣布此人出局。
游戏进入下一轮,由天黑到天亮,再有2人出局,如此反复,直到最后好人先被全部杀死,则游戏结束,“邪恶”的一方战胜了“正义”的一方;而另一个结局则是在大家以及村长的英明决断下,坏人被全部杀死,则“正义”的一方战胜了“邪恶”的一方。
现在,我们的问题是,请写出你的思路,并编写一个C/C++语言程序,求出:
在坏人有两个(n=2)的情况下,需要多少个好人(m=?
),才能够使这个游戏比较公平,也就是说,从概率上说,“正义”的一方与“邪恶”的一方胜利的几率最接近于50%。
(此题与下面一题选做一道)
6、在以上的杀人游戏中,还有一个玩法,就是取消村长这个角色,“天亮”后剩余的人每个人投票列出自己心目中最怀疑的2个人,以此决定让谁出局。
试问,在此玩法下,上面的问题又何解?
7.C++程序设计
1).写出以下程序的运行结果:
#include
classBase
{
public:
Base()
{
cout<<"Base()"< } Base(constBase&theBase) { cout<<"Base(constBase&theBase)"< } ~Base() { cout<<"~Base()"< } voidOpen() { OnOpen(); } private: virtualvoidOnOpen()=0; }; classDerived: publicBase { public: Derived() { cout<<"Derived()"< } Derived(constDerived&theDerived) { cout<<"Derived(constDerived&theDerived)"< } ~Derived() { cout<<"~Derived()"< } private: virtualvoidOnOpen() { //这里可能抛出异常 } }; Base*CreateInstance() { returnnewDerived(); } intmain() { Base*pBase=: : CreateInstance(); if(pBase) { pBase->Open(); deletepBase; } return0; } 2).在1)中,类Base和类Derived的实现有没有问题? 如果有,如何修改? 3).说明1)中类Base的Open函数和OnOpen函数的设计目的和意义。 4).使用STL技术修改main()函数中的代码,使之成为异常安全的。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 程序员 面试 经典 试题