南京华为慧通.docx

上传人:b****4 文档编号:12211726 上传时间:2023-04-17 格式:DOCX 页数:27 大小:22.95KB
下载 相关 举报
南京华为慧通.docx_第1页
第1页 / 共27页
南京华为慧通.docx_第2页
第2页 / 共27页
南京华为慧通.docx_第3页
第3页 / 共27页
南京华为慧通.docx_第4页
第4页 / 共27页
南京华为慧通.docx_第5页
第5页 / 共27页
点击查看更多>>
下载资源
资源描述

南京华为慧通.docx

《南京华为慧通.docx》由会员分享,可在线阅读,更多相关《南京华为慧通.docx(27页珍藏版)》请在冰豆网上搜索。

南京华为慧通.docx

南京华为慧通

(慧通)

1写出程序把一个链表中的接点顺序倒排

typedefstructlinknode

{

intdata;

structlinknode*next;

}node;

//将一个链表逆置

node*reverse(node*head)

{

node*p,*q,*r;

p=head;

q=p->next;

while(q!

=NULL)

{

r=q->next;

q->next=p;

p=q;

q=r;

}

head->next=NULL;

head=p;

returnhead;

}

2写出程序删除链表中的所有接点

voiddel_all(node*head)

{

node*p;

while(head!

=NULL)

{

p=head->next;

free(head);

head=p;

}

cout<<"释放空间成功!

"<

}

3两个字符串,s,t;把t字符串插入到s字符串中,s字符串有足够的空间存放t字符串

voidinsert(char*s,char*t,inti)

{

char*q=t;

char*p=s;

if(q==NULL)return;

while(*p!

='\0')

{

p++;

}

while(*q!

=0)

{

*p=*q;

p++;

q++;

}

*p='\0';

}

分析下面的代码:

char*a="hello";

char*b="hello";

if(a==b)

printf("YES");

else

printf("NO");

这个简单的面试题目,我选输出no(对比的应该是指针地址吧),可在VC是YES在C是NO

lz的呢,是一个常量字符串。

位于静态存储区,它在程序生命期内恒定不变。

如果编译器优化的话,会有可能a和b同时指向同一个hello的。

则地址相同。

如果编译器没有优化,那么就是两个不同的地址,则不同

 

 

 

写一个函数,功能:

完成内存之间的拷贝

memcpysourcecode:

   270void*memcpy(void*dst,constvoid*src,unsignedintlen)

   271{

   272   registerchar*d;

   273   registerchar*s;

   27

   275   if(len==0)

   276      returndst;

   277

   278   if(is_overlap(dst,src,len,len))

   279      complain3("memcpy",dst,src,len);

   280

   281   if(dst>src){

   282      d=(char*)dst+len-1;

   283      s=(char*)src+len-1;

   284      while(len>=4){

   285         *d--=*s--;

   286         *d--=*s--;

   287         *d--=*s--;

   288         *d--=*s--;

   289         len-=4;

   290      }

   291      while(len--){

   292         *d--=*s--;

   293      }

   294   }elseif(dst

   295      d=(char*)dst;

   296      s=(char*)src;

   297      while(len>=4){

   298         *d++=*s++;

   299         *d++=*s++;

   300         *d++=*s++;

   301         *d++=*s++;

   302         len-=4;

   303      }

   304      while(len--){

   305         *d++=*s++;

   306      }

   307   }

   308   returndst;

   309}

公司考试这种题目主要考你编写的代码是否考虑到各种情况,是否安全(不会溢出)

各种情况包括:

1、参数是指针,检查指针是否有效

2、检查复制的源目标和目的地是否为同一个,若为同一个,则直接跳出

3、读写权限检查

4、安全检查,是否会溢出

memcpy拷贝一块内存,内存的大小你告诉它

strcpy是字符串拷贝,遇到'\0'结束

/*memcpy───拷贝不重叠的内存块*/ 

voidmemcpy(void*pvTo,void*pvFrom,size_tsize)

{

void*pbTo=(byte*)pvTo;

void*pbFrom=(byte*)pvFrom;

ASSERT(pvTo!

=NULL&&pvFrom!

=NULL);//检查输入指针的有效性

ASSERT(pbTo>=pbFrom+size||pbFrom>=pbTo+size);//检查两个指针指向的内存是否重叠

while(size-->0)

*pbTo++==*pbFrom++;

return(pvTo);

}

华为面试题:

怎么判断链表中是否有环?

boolCircleInList(Link*pHead)

{

if(pHead==NULL||pHead->next==NULL)//无节点或只有一个节点并且无自环

return(false);

if(pHead->next==pHead)//自环

return(true);

Link*pTemp1=pHead;//step1

Link*pTemp=pHead->next;//step2

while(pTemp!

=pTemp1&&pTemp!

=NULL&&pTemp->next!

=NULL)

{

pTemp1=pTemp1->next;

pTemp=pTemp->next->next;

}

if(pTemp==pTemp1)

return(true);

return(false);

}

两个字符串,s,t;把t字符串插入到s字符串中,s字符串有足够的空间存放t字符串

voidinsert(char*s,char*t,inti)

{

memcpy(&s[strlen(t)+i],&s[i],strlen(s)-i);

memcpy(&s[i],t,strlen(t));

s[strlen(s)+strlen(t)]='\0';

}

1。

编写一个C函数,该函数在一个字符串中找到可能的最长的子字符串,且该字符串是由同一字符组成的。

char*search(char*cpSource,charch)

{

        char*cpTemp=NULL,*cpDest=NULL;

        intiTemp,iCount=0;

        while(*cpSource)

        {

                if(*cpSource==ch)

                {

                         iTemp=0;

                         cpTemp=cpSource;

                         while(*cpSource==ch)

++iTemp,++cpSource;

                         if(iTemp>iCount)

iCount=iTemp,cpDest=cpTemp;

       if(!

*cpSource)

break;

                }

                ++cpSource;

 }

 returncpDest;

}     

2。

请编写一个C函数,该函数在给定的内存区域搜索给定的字符,并返回该字符所在位置索引值。

intsearch(char*cpSource,intn,charch)

{

        inti;

        for(i=0;i

=ch;++i);

        returni;

}

一个单向链表,不知道头节点,一个指针指向其中的一个节点,问如何删除这个指针指向的节点?

将这个指针指向的next节点值copy到本节点,将next指向next->next,并随后删除原next指向的节点。

#include

voidfoo(intm,intn)

{

   printf("m=%d,n=%d\n",m,n);

}

intmain()

{

   intb=3;

   foo(b+=3,++b);

   printf("b=%d\n",b);

return0;

}

输出:

m=7,n=4,b=7(VC6.0)

这种方式和编译器中得函数调用关系相关即先后入栈顺序。

不过不同

编译器得处理不同。

也是因为C标准中对这种方式说明为未定义,所以

各个编译器厂商都有自己得理解,所以最后产生得结果完全不同。

因为这样,所以遇见这种函数,我们首先要考虑我们得编译器会如何处理

这样得函数,其次看函数得调用方式,不同得调用方式,可能产生不同得

结果。

最后是看编译器优化。

2.写一函数,实现删除字符串str1中含有的字符串str2.

第二个就是利用一个KMP匹配算法找到str2然后删除(用链表实现的话,便捷于数组)

/*雅虎笔试题(字符串操作)

给定字符串A和B,输出A和B中的最大公共子串。

比如A="aocdfe"B="pmcdfa"则输出"cdf"

*/

//Author:

azhen

#include

#include

#include

char*commanstring(charshortstring[],charlongstring[])

{

inti,j;

char*substring=malloc(256);

if(strstr(longstring,shortstring)!

=NULL)             //如果……,那么返回shortstring

returnshortstring; 

for(i=strlen(shortstring)-1;i>0;i--)                //否则,开始循环计算

{

for(j=0;j<=strlen(shortstring)-i;j++){

memcpy(substring,&shortstring[j],i);

substring[i]='\0';

if(strstr(longstring,substring)!

=NULL)

returnsubstring;

}

}

returnNULL;

}

main()

{

char*str1=malloc(256);

char*str2=malloc(256);

char*comman=NULL;

gets(str1);

gets(str2);

if(strlen(str1)>strlen(str2))                        //将短的字符串放前面

comman=commanstring(str2,str1);

else

comman=commanstring(str1,str2);

printf("thelongestcommanstringis:

%s\n",comman);

}

11.写一个函数比较两个字符串str1和str2的大小,若相等返回0,若str1大于

str2返回1,若str1小于str2返回-1

intstrcmp(constchar*src,constchar*dst)

{

       intret=0;

       while(!

(ret=*(unsignedchar*)src-*(unsignedchar*)dst)&&*dst)

{

               ++src;

++dst;

}

       if(ret<0)

               ret=-1;

       elseif(ret>0)

               ret=1;

       return(ret);

}

 

3,求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;

}

 

1.有双向循环链表结点定义为:

structnode

{intdata;

structnode*front,*next;

};

有两个双向循环链表A,B,知道其头指针为:

pHeadA,pHeadB,请写一函数将两链表中data值相同的结点删除

BOOLDeteleNode(Node*pHeader,DataTypeValue)

{

if(pHeader==NULL)return;

BOOLbRet=FALSE;

Node*pNode=pHead;

while(pNode!

=NULL)

{

if(pNode->data==Value)

{

if(pNode->front==NULL)

{

pHeader=pNode->next;

pHeader->front=NULL;

}

else

{

if(pNode->next!

=NULL)

{

pNode->next->front=pNode->front;

}

pNode->front->next=pNode->next;

}

Node*pNextNode=pNode->next;

deletepNode;

pNode=pNextNode;

bRet=TRUE;

//不要break或return,删除所有

}

else

{

pNode=pNode->next;

}

}

returnbRet;

}

voidDE(Node*pHeadA,Node*pHeadB)

{

if(pHeadA==NULL||pHeadB==NULL)

{

return;

}

Node*pNode=pHeadA;

while(pNode!

=NULL)

{

if(DeteleNode(pHeadB,pNode->data))

{

if(pNode->front==NULL)

{

pHeadA=pNode->next;

pHeadA->front=NULL;

}

else

{

pNode->front->next=pNode->next;

if(pNode->next!

=NULL)

{

pNode->next->front=pNode->front;

}

}

Node*pNextNode=pNode->next;

deletepNode;

pNode=pNextNode;

}

else

{

pNode=pNode->next;

}

}

}

2.编程实现:

找出两个字符串中最大公共子字符串,如"abccade","dgcadde"的最大子串为"cad"

intGetCommon(char*s1,char*s2,char**r1,char**r2)

{

intlen1=strlen(s1);

intlen2=strlen(s2);

intmaxlen=0;

for(inti=0;i

{

for(intj=0;j

{

if(s1[i]==s2[j])

{

intas=i,bs=j,count=1;

while(as+1

count++;

if(count>maxlen)

{

maxlen=count;

*r1=s1+i;

*r2=s2+j;

}

}

}

}

3.编程实现:

把十进制数(long型)分别以二进制和十六进制形式输出,不能使用printf系列库函数

char*test3(longnum){

char*buffer=(char*)malloc(11);

buffer[0]='0';

buffer[1]='x';

buffer[10]='\0';

char*temp=buffer+2;

for(inti=0;i<8;i++){

temp[i]=(char)(num<<4*i>>28);

temp[i]=temp[i]>=0?

temp[i]:

temp[i]+16;

temp[i]=temp[i]<10?

temp[i]+48:

temp[i]+55;

}

returnbuffer;

}

 

输入N,打印N*N矩阵

比如N=3,打印:

1 2 3

8 9 4

7 6 5

N=4,打印:

1  2  3  4

12 13 14 5

11 16 15 6

10 9  8  7

解答:

1#defineN15

ints[N][N];

voidmain()

{

intk=0,i=0,j=0;

inta=1;

for(;k<(N+1)/2;k++)

{

while(j

while(i

while(j>k-1)s[i][j--]=a++;i--;j++;

while(i>k)  s[i--][j]=a++;i++;j++;

}

for(i=0;i

{

for(j=0;j

cout<

cout<

}

}

2defineMAX_N 100

intmatrix[MAX_N][MAX_N];

/*

 *(x,y):

第一个元素的坐标

 *start:

第一个元素的值

 *n:

矩阵的大小

 */

voidSetMatrix(intx,inty,intstart,intn){

   inti,j;

   if(n<=0)   //递归结束条件

       return;

   if(n==1){ //矩阵大小为1时

       matrix[x][y]=start;

       return;

   }

   for(i=x;i

       matrix[y][i]=start++;

   for(j=y;j

       matrix[j][x+n-1]=start++;

   for(i=x+n-1;i>x;i--)    //底部

       matrix[y+n-1][i]=start++;

   for(j=y+n-1;j>y;j--)    //左部

       matrix[j][x]=start++;

   SetMatrix(x+1,y+1,start,n-2);  //递归

}

voidmain(){

  inti,j;

  intn;

  scanf("%d",&n);

  SetMatrix(0,0,1,n);

  

  //打印螺旋矩阵

  for(i=0;i

     for(j=0;j

printf("%4d",matrix[i][j]);

     printf("\n");

  }

}

斐波拉契数列递归实现的方法如下:

 int Funct(intn)

{

  if(n==0)return1;

  if(n==1)return1;

  retrurn Funct(n-1)+Funct(n-2);

}

请问,如何不使用递归,来实现上述函数?

请教各位高手!

解答:

int Funct(intn) // n为非负整数

{

  inta=0;

  intb=1;

  intc;

  if(n==0)c=1;

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

当前位置:首页 > 工程科技 > 能源化工

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

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