华为笔试题(含答案解析)

来源:互联网 发布:王者荣耀引流源码 编辑:程序博客网 时间:2024/05/19 08:36

1.static有什么用途?(请至少说明两种)

    1)在函数体,一个被声明为静态的变量在这一函数被调用过程中维持其值不变。

    2) 在模块内(但在函数体外),一个被声明为静态的变量可以被模块内所有函数访问,但不能被模块外其它函数访问。它是一个本地的全局变量。

    3)在模块内,一个被声明为静态的函数只可被这一模块内的其它函数调用。那就是,这个函数被限制在声明它的模块的本地范围内使用

 

2.引用与指针有什么区别?

    1) 引用必须被初始化,指针不必。

    2) 引用初始化以后不能被改变,指针可以改变所指的对象。

    3) 不存在指向空值的引用,但是存在指向空值的指针。

 

3.描述实时系统的基本特性

       在特定时间内完成特定的任务,实时性与可靠性。

 

4.全局变量和局部变量在内存中是否有区别?如果有,是什么区别?

      全局变量储存在静态数据库(全局区),局部变量在堆栈。

 

5.什么是平衡二叉树?

      左右子树都是平衡二叉树 且左右子树的深度差值的绝对值不大于1。

 

6.堆栈溢出一般是由什么原因导致的?

      没有回收垃圾资源。

 

7.什么函数不能声明为虚函数?

      constructor函数不能声明为虚函数。

 

8.冒泡排序算法的时间复杂度是什么?

      时间复杂度是O(n^2)。

 

9.写出float x 与“零值”比较的if语句。

      if(x>0.000001&&x<-0.000001)

 

10.Internet采用哪种网络协议?该协议的主要层次结构?

      Tcp/Ip协议

      主要层次结构为: 应用层/传输层/网络层/数据链路层/物理层。

 

11.Internet物理地址和IP地址转换采用什么协议?

      ARP (Address Resolution Protocol)(地址解析协议)

 

12.IP地址的编码分为哪俩部分?

     IP地址由两部分组成,网络号和主机号。不过是要和“子网掩码”按位与上之后才能区分哪些是网络位哪些是主机位。

 

13.用户输入M,N值,从1至N开始顺序循环数数,每数到M输出该数值,直至全部输出。写出C程序。有些,M,N值可能导致不能全部输出

     循环链表,用取余操作做

我的答案:

[cpp] view plaincopyprint?
  1. #include "iostream"   
  2. #include "stdio.h"   
  3. using namespace std;  
  4. struct Node   
  5. {  
  6.     int data;//存储1-N的值  
  7.     bool flag;//标记是否已被输出  
  8.     Node *next;  
  9. };  
  10. //建立链表,循环链表,尾节点指向首节点,表长度为N,节点的数据值为1---N   
  11. Node* init(int N)  
  12. {  
  13.     Node * head=NULL;  
  14.     Node *q=NULL;  
  15.     for (int i=1;i<=N;i++)  
  16.     {  
  17.         if (i==1)  
  18.         {  
  19.             Node *p=(Node *)malloc(sizeof(Node));  
  20.             p->data=i;  
  21.             p->flag=false;  
  22.             head=p;  
  23.             q=p;  
  24.             q->next=head;  
  25.           
  26.         }  
  27.         else  
  28.         {  
  29.             Node *p=(Node *)malloc(sizeof(Node));  
  30.             p->data=i;p->flag=false;  
  31.             q->next=p;  
  32.             q=p;  
  33.             q->next=head;      
  34.         }  
  35.     }  
  36.     return head;  
  37. }  
  38. //打印所建立的循环链表,测试建表成功   
  39. void printLink(Node *head)  
  40. {  
  41.     Node *p=head;  
  42.       
  43.     do{  
  44.         cout<<p->data<<", ";  
  45.         p=p->next;  
  46.     }while (p!=head);  
  47.     cout<<endl;  
  48. }  
  49. //判断是否还有没有打印出来的节点   
  50. int isallflag(Node * head)  
  51. {  
  52.     Node *p=head;  
  53.     int k=0;  
  54.     do{  
  55.         if (p->flag==false)  
  56.         {  
  57.             k=1;  
  58.         }  
  59.         p=p->next;  
  60.           
  61.     }while (p!=head);  
  62.     return k;  
  63. }  
  64. //功能函数,约瑟夫环,打印出一个标记一个点,直至都打印出来   
  65. void fun(Node * head,int m)  
  66. {  
  67.     Node *p=head;  
  68.    int ct=1;  
  69.    while (isallflag(head)==1)  
  70.    {  
  71.         
  72.       
  73.        if (ct==m)  
  74.        {  
  75.            if (p->flag==false)  
  76.            {  
  77.                 p->flag=true;  
  78.                 cout<<p->data<<endl;  
  79.               
  80.            }  
  81.             ct=1;  
  82.             p=p->next;  
  83.              
  84.        }  
  85.        else  
  86.        {  
  87.            ct++;  
  88.            p=p->next;  
  89.        }  
  90.    }  
  91. }  
  92. int main(void)    
  93. {    
  94.     int m=0,n=0;  
  95.     cout<<"input m,n\n";  
  96.     cin>>m;  
  97.     cin>>n;  
  98.     Node *head=init(n);  
  99.     fun(head,m);  
  100.     return 0;    
  101. }    



14.不能做switch()的参数类型是:

     switch的参数不能为实型。只能是整型和字符型

 

######################################################

 

1.printf的输出问题

[cpp] view plaincopyprint?
  1. <SPAN style="FONT-SIZE: 14px">printf("%d",total);正确  
  2.  printf(total);错误  
  3. printf("hello");正确</SPAN>  



 

2.整数类型的长度

char 1个子节,8位

unsigned short[int]

[signed] short int

short 2个字节,16位

 

[signed] int

unsigned int

int 型在vc里是4个子节,32位,也可能是16位,2个字节

long [int]

unsigned long[int]

long型都是32位,4个字节

float 32 ,4

double 64,8

long double 128,16

char 8,一个字节,存放的实际上是字符的ascii码

3、找出错误并改正

[cpp] view plaincopyprint?
  1. <SPAN style="FONT-SIZE: 14px">char *my_cpy(char*src, int len){  
  2. char dest[1024];  
  3. memcpy(dest, src,len);  
  4. return dest;  
  5. }</SPAN>  



 

上面的函数是否有问题,如果有指出其所在,如果没有,给出函数功能描述。

答案:

1。数组应该初始化

2。memcpy不判断是否越界,所以调用前应该判断是否越界

3。不应该返回rest,因为这个数组是在函数内部申请的,所以函数结束之后就会消失,指针也会变成“野指针”,所以指向非法地址

最后一个比较隐蔽

[cpp] view plaincopyprint?
  1. <SPAN style="FONT-SIZE: 14px">char *memcpy( char*dest, const char *src,int len )  
  2. {  
  3. char* pDest =(char*)dest;  
  4. char* pSrc =(char*)src;  
  5. int pos;  
  6. for(pos=0;pos<len;pos++)  
  7. {  
  8. pDest[pos] =pSrc[pos];  
  9. }  
  10. return(char*)pDest;  
  11. }</SPAN>  



 

存在地问题就是没有判断指针是否非法assert(dest !=NULL || src != NULL); 条件为 FLASE 显示 如果assert的参数为假,则会先打印出错信息,然后调用abort()来终止程序

不调用其他函数,写一个memcpy的函数,函数原型为

[cpp] view plaincopyprint?
  1. void *memcpy(void*dest, void *src, size_t length);  
  2. -----------利用好断言---------  
  3. /* memcpy ───拷贝不重叠的内存块 */  
  4. void memcpy(void*pvTo, void* pvFrom, size_t size)  
  5. {  
  6. void* pbTo =(byte*)pvTo;  
  7. void* pbFrom =(byte*)pvFrom;  
  8. ASSERT(pvTo !=NULL && pvFrom != NULL);  
  9. /* 内存块重叠吗?如果重叠,就使用memmove */  
  10. ASSERT(pbTo>=pbFrom+size|| pbFrom>=pbTo+size);  
  11. while(size-->0)  
  12. *pbTo++ ==*pbFrom++;  
  13. return(pvTo);  
  14. }  



 

-----------------------

常见函数编程:

[cpp] view plaincopyprint?
  1. <SPAN style="FONT-SIZE: 14px">char *strcpy(char*strDest, const char *strSrc)  
  2. {  
  3. ASSERT(strDest !=NULL && strSrc != NULL);  
  4. char *addr =strDest;  
  5. while(*strDest++=*strSrc++)  
  6. NULL; //NULL可以省略,但更有利于编译器发现错误   
  7. }  
  8. return addr;  
  9. }  
  10.    
  11. void *memcpy(void*dest, const void *src, int count)  
  12. {  
  13. ASSERT(dest!= NULL&& src!= NULL);  
  14. for(int i=0; i<cout; i++)  
  15. {  
  16. dest[i] = src[i];  
  17. }  
  18. }  
  19. int strcmp(constchar*str1, const char *str2)  
  20. {  
  21. while (str1 !=NULL && str2 != NULL)  
  22. {  
  23. if(*str1 <*str2) return -1;  
  24. else if(*str1 >*str2) return 1;  
  25. else { str1++;str2++;}  
  26. }  
  27. if(str1 == NULL&& str2 != NULL)  
  28. return -1;  
  29. else if(str1 !=NULL && str2 == NULL)  
  30. return 1;  
  31. else return 0;  
  32. }  
  33.    
  34. //way2: more compact   
  35. int strcmp(constchar*str1, const char *str2)  
  36. {  
  37. int i = strlen(str1 );  
  38. int j;  
  39. for(j=0; j<=i;j++)  
  40. {  
  41. if(str1[j] >str2[j]) return 1; //if str2 terminates, then str2[j]=0, str1[j]>str2[j],return 1;  
  42. else if(str1[j]< str2[j]) return -1;  
  43. else if(str1[j] ==''return 0;  
  44. }  
  45. }  
  46. //way3: optimizeagain.   
  47. int strcmp(constchar * str1, const char * str2 )  
  48. {  
  49. while(1)  
  50. {  
  51. if(*str1 >*str2) return 1;  
  52. else if(*str1 <*str2) return -1;  
  53. else if(*str1 ==''return 0;  
  54. str1++;str2++;  
  55. }  
  56. }</SPAN>  

 

原创粉丝点击