华为C语言面试试题

  • 格式:doc
  • 大小:102.00 KB
  • 文档页数:23

一、判断题(对的写T,错的写F并说明原因,每小题4分,共20分)

1、有数组定义int a[2][2]={{1},{2,3}};则a[0][1]的值为0。( T )

2、int (*ptr) (),则ptr是一维数组的名字。( F, ptr为一个函数指针 )

3、指针在任何情况下都可进行>,<,>=,<=,==运算。( F,指针比较大小没有任何意义,但是可以比较==与!= )

4、switch(c) 语句中c可以是int ,long,char ,float ,unsigned int 类型。( F,不可以为float )

5、#define print(x) printf("the no, "#x",is ") (T)

二、填空题(共30分)

1、在windows下,写出运行结果,每空2分,共10分。

char str[ ]= "Hello";

char *p=str;

int n=10;

sizeof(str)=( 6 )

sizeof(p)=( 4 )

sizeof(n)=( 4 )

void func(char str[100])

{ }

sizeof(str)=( 4 )

2、void setmemory(char **p, int num)

{ *p=(char *) malloc(num);}

void test(void)

{ char *str=NULL;

setmemory(&str,100);

strcpy(str,"hello");

printf(str);

}

运行test函数有什么结果?( hello )10分

3、设int arr[]={6,7,8,9,10};

int *ptr=arr;

*(ptr++)+=123;

printf("%d,%d",*ptr,*(++ptr));

( 8, 8 ) 10分

二、编程题(第一小题20,第二小题30分)

1、不使用库函数,编写函数int strcmp(char *source, char *dest) 相等返回0,不等返回-1;

#include

#include

int mystrcmp(char *source, char *dest)

{

while(*source == *dest && *source != '\0' && *dest != '\0')

{

source++;

dest++;

} if (*source =='\0' && *dest == '\0')

return 0;

else

return -1;

}

int main()

{

char *str1 = "abcde";

char *str2 = "abcd";

printf("ret = %d", mystrcmp(str1, str2));

return 0;

}

2、写一函数int fun(char *p)判断一字符串是否为回文,是返回1,不是返回0,出错返回-1

#include

#include

int rollback(char *str)

{

int len;

len=strlen(str);

for(int i=0;i

{if(*(str+i) == *(str+len-1-i) )

return 1;

else

return 0;

}

return -1;

}

int main()

{

char *str1 = "abaaba";

printf("ret = %d", rollback(str1));

return 0;

}

写一个程序, 要求功能:求出用1,2,5这三个数不同个数组合的和为100的组合个数。

如:100个1是一个组合,5个1加19个5是一个组合。。。。 请用C++语言写。

答案:最容易想到的算法是:

设x是1的个数,y是2的个数,z是5的个数,number是组合数

注意到0<=x<=100,0<=y<=50,0<=z=20,所以可以编程为:

number=0;

for (x=0; x<=100; x++)

for (y=0; y<=50; y++)

for (z=0; z<=20; z++) if ((x+2*y+5*z)==100)

number++;

cout<

上面这个程序一共要循环100*50*20次,效率实在是太低了

事实上,这个题目是一道明显的数学问题,而不是单纯的编程问题。我的解法如下:

因为x+2y+5z=100

所以x+2y=100-5z,且z<=20 x<=100 y<=50

所以(x+2y)<=100,且(x+5z)是偶数

对z作循环,求x的可能值如下:

z=0, x=100, 98, 96, ... 0

z=1, x=95, 93, ..., 1

z=2, x=90, 88, ..., 0

z=3, x=85, 83, ..., 1

z=4, x=80, 78, ..., 0

......

z=19, x=5, 3, 1

z=20, x=0

因此,组合总数为100以内的偶数+95以内的奇数+90以内的偶数+...+5以内的奇数+1,

即为:(51+48)+(46+43)+(41+38)+(36+33)+(31+28)+(26+23)+(21+18)+(16+13)+(11+8)+(6+3)+1

某个偶数m以内的偶数个数(包括0)可以表示为m/2+1=(m+2)/2

某个奇数m以内的奇数个数也可以表示为(m+2)/2

所以,求总的组合次数可以编程为:

number=0;

for (int m=0;m<=100;m+=5)

{

number+=(m+2)/2;

}

cout<

这个程序,只需要循环21次, 两个变量,就可以得到答案,比上面的那个程序高效了许多

倍----只是因为作了一些简单的数学分析

这再一次证明了:计算机程序=数据结构+算法,而且算法是程序的灵魂,对任何工程问题,当用软件来实现时,必须选取满足当前的资源限制,用户需求限制,开发时间限制等种种限制条件下的最优算法。而绝不能一拿到手,就立刻用最容易想到的算法编出一个程序了事——这不是一个专业的研发人员的行为。

那么,那种最容易想到的算法就完全没有用吗?不,这种算法正好可以用来验证新算法的正确性,在调试阶段,这非常有用。在很多大公司,例如微软,都采用了这种方法:在调试阶段,对一些重要的需要好的算法来实现的程序,而这种好的算法又比较复杂时,同时用容易想到的算法来验证这段程序,如果两种算法得出的结果不一致(而最容易想到的算法保证是正确的),那么说明优化的算法出了问题,需要修改。

可以举例表示为:

#ifdef DEBUG

int simple(); #end if

int optimize();

......

in a function:

{

result=optimize();

ASSERT(result==simple());

}

这样,在调试阶段,如果简单算法和优化算法的结果不一致,就会打出断言。同时,在程

序的发布版本,却不会包含笨重的simple()函数。——任何大型工程软件都需要预先设计良

好的调试手段,而这里提到的就是一种有用的方法。

一个学生的信息是:姓名,学号,性别,年龄等信息,用一个链表,把这些学生信息连在一起, 给出一个age, 在些链表中删除学生年龄等于age的学生信息。

#include "stdio.h"

#include "conio.h"

struct stu{

char name[20];

char sex;

int no;

int age;

struct stu * next;

}*linklist;

struct stu *creatlist(int n)

{

int i;

//h为头结点,p为前一结点,s为当前结点

struct stu *h,*p,*s;

h = (struct stu *)malloc(sizeof(struct stu));

h->next = NULL;

p=h;

for(i=0;i

{

s = (struct stu *)malloc(sizeof(struct stu));

p->next = s;

printf("Please input the information of the student: name sex no age \n");

scanf("%s %c %d %d",s->name,&s->sex,&s->no,&s->age);

s->next = NULL;

p = s;

}

printf("Create successful!");

return(h);

}

void deletelist(struct stu *s,int a)

{