程序员面试题编程题.docx

上传人:b****4 文档编号:24904652 上传时间:2023-06-02 格式:DOCX 页数:70 大小:2.22MB
下载 相关 举报
程序员面试题编程题.docx_第1页
第1页 / 共70页
程序员面试题编程题.docx_第2页
第2页 / 共70页
程序员面试题编程题.docx_第3页
第3页 / 共70页
程序员面试题编程题.docx_第4页
第4页 / 共70页
程序员面试题编程题.docx_第5页
第5页 / 共70页
点击查看更多>>
下载资源
资源描述

程序员面试题编程题.docx

《程序员面试题编程题.docx》由会员分享,可在线阅读,更多相关《程序员面试题编程题.docx(70页珍藏版)》请在冰豆网上搜索。

程序员面试题编程题.docx

程序员面试题编程题

1.利用递归调用手段编程计算N!

答:

#include

usingnamespacestd;

intt;

intn;

intFind(intn)

{

if(n==1)

{

return1;

}

else

{

t=Find(n-1)*n;

returnt;

}

}

intmain(void)

{

cin>>n;

intt=Find(n);

cout<

="<

return0;

}

2.写一个八进制转换为十进制的问题的程序。

(设八进制数只有两位数的)

#include

usingnamespacestd;

unsignedintoct2dec(unsignedintoct)

{

returnoct/10*8+oct%10;

}

intmain(void)

{

unsignedintoct=0;

unsignedintdec=0;

cout<<”pleaseinputaoctetnumber(Besurethenumberyouinputisbeginwitha‘0’):

”<<’\n’;

cin>>oct;

dec=oct2dec(oct);

cout<

return0;

}

3.写出二分查找的代码.

intbinary_search(int*arr,intkey,intn)

{

intlow=0;

inthigh=n-1;

intmid;

while(low<=high)

{

mid=(high+low)/2;

if(arr[mid]>key)

high=mid-1;

elseif(arr[mid]

low=mid+1;

else

returnmid;

}

return-1;

}

*4.实现strcpy函数(*出现次数相当频繁

char*strcpy(char*dest,constchar*src)

{

assert((dest!

=NULL)&&(src!

=NULL));

char*address=dest;

while((*dest++=*src++)!

='\0');

returnaddress;

}

 

char*strcpy(char*strDest,constchar*strSrc)

  {

  char*strDestCopy=strDest;//[3]

  if((strDest==NULL)||(strSrc==NULL))//[1]

  throw"Invalidargument(s)";//[2]

  while((*strDest++=*strSrc++)!

='\0');//[4]

  returnstrDestCopy;

}

 

*5实现strcmp函数

#include

#include

#include

usingnamespacestd;

intstrcmp(constchar*str1,constchar*str2)

{

assert((str1!

=NULL)&&(str2!

=NULL));

intret=0;

/*

   转化为unsignedchar是为了防止溢出

   例如(char)0x7f-(char)0x80=255,(unsignedchar)0x7f-(unsignedchar)0x80=-1

*/

while(!

(ret=*(unsignedchar*)str1-*(unsignedchar*)str2)&&**str2!

='\0'&&*str1!

='\0')

{

str1++;

str2++;

}

if(ret>0)

ret=1;

elseif(ret<0)

ret=-1;

returnret;

}

intmain(void)

{

cout<

return0;

}

*6string类的编写

#include

usingnamespacestd;

classString

{

public:

String(constchar*str=NULL);//普通构造函数

String(constString&other);//拷贝构造函数

~String(void);//析构函数

String&operator=(constString&other);//赋值函数

String&operate+(constString&s1,constString&s2);//加法函数

private:

char*m_data;

};

//普通构造函数

String:

:

String(constchar*str)

{

if(str==NULL)

{

m_data=newchar[1];

*m_data='\0';

}

else

{

intlenth=strlen(str);

m_data=newchar[lenth+1];

strcpy(m_data,str);

}

}

//拷贝构造函数

String:

:

String(constString&other)

{

intlenth=strlen(other.m_data);

m_data=newchar[lenth+1];

strcpy(m_data,other.m_data);

}

//析构函数

String:

:

~String()

{

delete[]m_data;

}

//赋值函数

String&String:

:

operator=(constString&other)

{

//检查自赋值

if(this=&other)

{

return*this;

}

//释放原有资源

delete[]m_data;

//分配新的内存资源,并赋值

intlen=strlen(otehr.m_data);

m_data=newchar[len+1];

strcpy(m_data,other.m_data);

//返回本对象的引用

return*this;

}

//加法函数

String&operator+(constString&s1,constString&s2)

{

Stringtemp;

deletetemp.data;//temp.data是仅含‘\0’的字符串

temp.data=newchar[strlen(s1.data)+strlen(s2.data)+1];

strcpy(temp.data,s1.data);//data为char*型的数据成员。

strcat(temp.data,s2.data);

returntemp;

}

//便于理解上述代码,写出main函数

intmain(void)

{

Stringstr_a=newString();//调用构造函数

Stringstr_b=newString(“abc”);

Stringstr_c=str_b;//调用拷贝构造函数

str_c=str_a;/赋值

Stringsrt_d;

str_d=str_c+str_b;

}

7.写出在母串中查找子串出现次数的代码。

思路:

从第一个字符开始查找,然后逐个比较两个字符串是否相同,直到子字符串结束都相同则表明找到一次,num+1,然后第二个字符开始。

重复

intcount(char*str,char*substr)

{

char*str1;

char*str2;

intnum=0;

while(*str!

='\0')

{

str1=str;

str2=substr;

while(*str2==*str1&&(*str2!

='\0')&&(*str1!

='\0'))

{

str2++;

str1++;

}

if(*str2=='\0')

num++;

str++;

}

returnnum;

}

*8.写函数完成内存的拷贝

一个内存拷贝函数的实现体

void*memcpy(void*pvTo,constvoid*pvFrom,size_tsize)

{

assert((pvTo!

=NULL)&&(pvFrom!

=NULL));

byte*pbTo=(byte*)pvTo;//防止地址被改变

byte*pbFrom=(byte*)pvFrom;

while(size-->0)

*pbTo++=*pbForm++;

returnpvTo;

}

*9.将一个数字字符串转换为数字."1234"-->1234

#include

usingnamespacestd;

intf(char*s)

{

intk=0;

while(*s!

=‘\0’)

{

k=10*k+(*s++)-'0';//k每乘以1次10,则位数左移一位(如个位变成10位)

}

returnk;

}

intmain()

{

intdigit=f("4567");

cout<

cin.get();

}

10.求1000!

的末尾有几个0

(用素数相乘的方法来做,如72=2*2*2*3*3);求出1->1000里,能被5整除的数的个数n1,能被25整除的数的个数n2,能被125整除的数的个数n3,能被625整除的数的个数

n4.1000!

末尾的零的个数=n1+n2+n3+n4;

#include

#defineNUM1000

intfind5(intnum)

{

intret=0;

while(num%5==0)

{

num/=5;

ret++;

}

returnret;

}

intmain()

{

intresult=0;

inti;

for(i=5;i<=NUM;i+=5)

{

result+=find5(i);

}

printf("thetotalzeronumberis%d\n",result);

return0;

}

11.写一个函数找出一个整数数组中,第二大的数。

constintMINNUMBER=-32767;

intfind_sec_max(intdata[],intcount)

{

intmaxnumber=data[0];

intsec_max=MINNUMBER;

for(inti=1;i

{

if(data[i]>maxnumber)

{

sec_max=maxnumber;

maxnumber=data[i];

}

else

{

if(data[i]>sec_max)

sec_max=data[i];

}

returnsec_max;

}

12.用递归算法判断数组a[N]是否为一个递增数组。

递归的方法,记录当前最大的,并且判断当前的是否比这个还大,大则继续,否则返回false结束:

boolfun(inta[],intn)

{

if(n==1)

{

returntrue;

}

if(n==2)

{

returna[n-1]>=a[n-2];

}

returnfun(a,n-1)&&(a[n-1]>=a[n-2]);//递归的同时要保证上一次的条件成立

}

13.选择排序

算法思想简单描述:

在要排序的一组数中,选出最小的一个数与第一个位置的数交换;然后在剩下的数当中再找最小的与第二个位置的数交换,如此循环到倒数第二个数和最后一个数比较为止。

选择排序是不稳定的。

算法复杂度O(n2)--[n的平方]【外层循环n-1趟,内层(第i+1趟)循环n-i-1次】【对比冒泡排序:

外层循环n-1趟,内层(第i趟)循环n-i-1次】

voidselect_sort(int*x,intn)

{

inti,j,min,t;

for(i=0;i

共n-1次*/

{

min=i;/*假设当前下标为i的数最小(从0开始,逐步加1),比较后再调整*/

for(j=i+1;j

{

if(*(x+j)<*(x+min))

{

min=j;/*如果后面的数比前面的小,则记下它的下标*/

}

}

if(min!

=i)/*如果min在循环中改变了,就需要交换数据*/

{

t=*(x+i);

*(x+i)=*(x+min);

*(x+min)=t;

}

}

}

14直接插入排序

算法思想简单描述:

在要排序的一组数中,假设前面(n-1)[n>=2]个数已经是排好顺序的,现在要把第n个数插到前面的有序数中,使得这n个数也是排好顺序的。

如此反复循环,直到全部排好顺序。

直接插入排序是稳定的。

算法时间复杂度O(n2)--[n的平方]【直接插入,冒泡,选择排序的时间复杂度一样】

voidinsert_sort(int*x,intn)

{

inti,j,t;

for(i=1;i

1~n共n-1次

{

/*暂存下标为i的数。

注意:

下标从1开始,原因就是开始时,第一个数即下标为0的数,前面没有任何数,单单一个,认为它是排好顺序的,方便下面的for循环下标的书写。

*/

t=*(x+i);

for(j=i-1;j>=0&&t<*(x+j);j--)/*注意:

j=i-1,j--,这里就是在下标为i的数后面有序列中找插入位置。

【这里的后面指的是右边】*/

{

*(x+j+1)=*(x+j);/*如果满足条件就往后挪。

最坏的情况就是t比下标为0的数都小,它要放在最前面,j==-1,退出循环*/

}

*(x+j)=t;/*找到下标为i的数的放置位置【改变参考点】*/

}

}

看到这里

15冒泡排序

16.文件中有一组整数,要求排序后输出到另一个文件中。

#include

#include

usingnamespacestd;

voidOrder(vector&data)//bubblesort

{

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;

}

}

intmain(void)

{

vectordata;

ifstreamin("c:

\\data.txt");

if(!

in)

{

cout<<"fileerror!

";

exit

(1);

}

inttemp;

while(!

in.eof())

{

cin>>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();//关闭输出文件流

}

}

17.输入一个字符串,将其逆序后输出。

(使用C++,不建议用伪码)

#include

usingnamespacestd;

voidmain()

{

chara[50];

memset(a,0,sizeof(a));

inti=0,j;

chart;

cin.getline(a,50,'\n');

for(i=0,j=strlen(a)-1;i

{

t=a[i];

a[i]=a[j];

a[j]=t;

}

cout<

}

18.链表题

1)编程实现一个单链表的建立。

#include

#include

#include

#include

usingnamespacestd;

typedefstructstudent

{

intdata;

structstudent*next;

}node;

node*Create(void)

{

node*head=NULL;

node*p=NULL;

node*s=NULL;

intx,cycle=1;

head=(node*)malloc(sizeof(node));

p=head;

printf("pleaseintputthedata:

");

scanf("%d",&x);

p->data=x;

p->next=NULL;

while(cycle)

{

printf("pleaseintputthedata:

");

scanf("%d",&x);

if(x!

=0)

{

s=(node*)malloc(sizeof(node));

s->data=x;

printf("\n%d",s->data);

p->next=s;

p=s;

}

else

{

cycle=0;

}

}

p->next=NULL;

printf("\nyyy%d",head->data);

returnhead;

}

intmain(void)

{

Create();

return0;

}

2)实现单链表的测长[双链表的测长也是一样]

intCaculateLenth(structstudent*head)

{

structstudent*p=NULL;

p=head;

intn=0;

while(p!

=NULL)

{

n++;

p=p->next;

}

returnn;

}

3)编程实现单链表的打印[双链表也是一样的]

voidShow(structstudent*head)

{

structstudent*p=NULL;

p=head;

intn=0;

while(p!

=NULL)

{

cout<data<

p=p->next;

}

}

4)编程实现单链表删除节点

解题思路:

先遍历比较值,找到要删除节点后再判断是否为头节点,还是后面的节点【删除中间的节点和删除末尾的节点是一个样的】。

node*Delete(structstudent*head,intnum)

{

structstudent*p1=NULL;

structstudent*p2=NULL;

p1=head;

while(num!

=p1->data&&p1->next!

=NULL)

{

p2=p1;

p1=p1->next;

}

if(num==p1->data)

{

if(p1==head)//删除的是头节点

{

head=p1->next;

free(head);

}

else//删除的是头结点后面的节点

{

p2->next=p1->next;

free(p1);

}

}

else

{

cout<<"thenumhasn'tbeenfound"<

}

returnhead;

}

5)编程实现单链表的插入

解析:

如果插入在节点以前,则p0的next指向p1,投节点指向p0,如下图所示

如果插入尾节点,如下图所示。

解题思路:

先遍历对比关键值,找到后再判断在第一个节点前插入还是中间插入还是最后面插入。

node*Insert(node*head,intnum)

{

node*p0,*p1,*p2;

p1=head;

p0=(node*)malloc(sizeof(node));

p0->data=num;

while(p0->data>p1->data&&p

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 院校资料

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1