华为面试题.docx
《华为面试题.docx》由会员分享,可在线阅读,更多相关《华为面试题.docx(26页珍藏版)》请在冰点文库上搜索。
华为面试题
1.用一个双向链表写一个快速排序算法
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2. 2.自己写一个assert宏的定义
#ifdef _DEBUG
#define assert(expr) \
do {\
if(expr) \
{\
printf("Assertion%s failed in %s, line %d\n", __FILE__, __LINE__);\
exit(0);\
} \
}while(0);
#else
#define assert(expr)
#endif
__FILE__, __LINE__都是C里自带的宏,分别表示当前的文件名和所有行,而调用printf函数的时候也应该把assert(expr)中的expr也打印出来( wanguodu好像忘了).而do{}while(0)(呵呵,确实只循环一次)是为了防止在进行宏替换的时候出错.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
for(;1;)
{
}
这个程序有什么问题,会出现什么结果?
有1,2,....一直到n的无序数组,求排序算法,并且要求时间复杂度为O(n),空间复杂度O
(1),使用交换,而且一次只能交换两个数.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
4. 以下代码有什么问题?
[C++易]
struct Test
{
Test( int ) {}
Test() {}
void fun() {}
};
void main( void )
{
Test a
(1);
a.fun();
Test b(); //应该是Testb;
类是结构体的扩展,在类中封装了对数据成员的操作,缺省的成员为私有的,而结构体为公有的,这就是它们的区别,对构造函数的调用,如果没有参数,是不需要加上括号的,如果加了括号,就不是定义一个对象了,而是声明了一个函数,返回该类型,所以上面的Test b(),实际上是调用一个函数名为b,返回类型为Test的函数,而不是创建了一个对象b,去掉括号后,就是调用的没有形参的构造函数。
b.fun(); //b不是Test的实例对象
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
5. 以下代码有什么问题?
[C++易]
cout << (true?
1:
"1") << endl; //类型不同, 必须保证1和"1" 这两部分返回的类型一致
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
3. 以下两条输出语句分别输出什么?
[C++难]
float a = 1.0f;
cout << (int)a << endl; //1
cout << (int&)a << endl; //a内存里的是多少就是多少
cout << boolalpha << ( (int)a == (int&)a ) << endl;
// 输出什么?
boolalpha表示什么, //boolalpha输出bool字母,false
float b = 0.0f; // 用什么头包含?
cout << (int)b << endl; //0
cout << (int&)b << endl; //0
cout << boolalpha << ( (int)b == (int&)b ) << endl;
// 输出什么?
fasle
float f = 1.0f;
(int&)f and int (&f)
对于后者,就是取地址后强制转换为int,应该没有问题;
但是前者,将1.0f强制转换成int&,int引用类型。
我们知道,float在内存中采用的是ieee745方式:
0---00 00 00 00 ,1----00 00 80 3F ,2---00 00 00 40 ......
也就是说,对于f=0.0f,则转换后还是0,但是对于f=1.0f,转换后的结果为0x3f800000
关键是看清楚这是一个强制转换,同时要了解float类型的存贮格式与int不同
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
六、编写类String的构造函数、析构函数和赋值函数(25分)
已知类String的原型为:
class String
{
public:
String(const char *str = NULL);// 普通构造函数
String(const String &other); // 拷贝构造函数
~ String(void); // 析构函数
String & operate =(const String &other);// 赋值函数
private:
char *m_data;// 用于保存字符串
};
请编写String的上述4个函数。
// String的析构函数
String:
:
~String(void) // 3分
{
delete [] m_data;
// 由于m_data是内部数据类型,也可以写成 delete m_data;
}
// String的普通构造函数
String:
:
String(const char *str) // 6分
{
if(str==NULL)
{
m_data = new char[1]; // 若能加 NULL 判断则更好
*m_data = ‘\0’;
}
else
{
int length = strlen(str);
m_data = new char[length+1]; // 若能加 NULL 判断则更好
strcpy(m_data, str);
}
}
// 拷贝构造函数
String:
:
String(const String &other) // 3分
{
int length = strlen(other.m_data);
m_data = new char[length+1]; // 若能加 NULL 判断则更好
strcpy(m_data, other.m_data);
}
// 赋值函数
String & String:
:
operate =(const String &other) // 13分
{
//
(1) 检查自赋值 // 4分
if(this == &other)
return *this;
//
(2) 释放原有的内存资源 // 3分
delete [] m_data;
// (3)分配新的内存资源,并复制内容 // 3分
int length = strlen(other.m_data);
m_data = new char[length+1]; // 若能加 NULL 判断则更好
strcpy(m_data, other.m_data);
// (4)返回本对象的引用 // 3分
return *this;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
typedef struct
{
int a:
2;
int b:
2;
int c:
1;
}test;
test t;
t.a = 1; printf("%d",t.a);
t.b = 3; printf("%d",t.b);
t.c = 1; printf("%d",t.c);
printf("%d",t.a); == 1 t.a为01,输出就是1
printf("%d",t.b); == -1 t.b为11,输出就是-1
printf("%d",t.c); == -1t.c为1,输出也是-1
3个都是有符号数int嘛。
这是位扩展问题,可以查看谭浩强的c程序设计关于位段的部分
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main()
{
printf("This is line 1\n");
return 0;
printf("This is line 2\n");
}
不添加新函数,不修改main函数,不引入新文件,让程序输出:
This is line 2
#define printf(A) if(strcmp(A,"This is line 1\n")==0) printf("This is line 2\n")
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
有以下程序
main()
{union {unsigned intn;
unsigned charc;
}u1;
ul.c=`A`;
printf("%c\n",u1.n);
}
执行后输出结果是
A) 产生语法错B) 随机值C) AD) 65
(45)以下程序段中,能够通过调用函数fun,使main函数中的指针变量p指向一个合法的整型单元的是
A) main()B) main
{{int*p;
int*p; fun(&p);
fun(p); }
}
int fun(int *p) int fun(int **p)
{ int s; p=&s;} { int s;*p=&s;}
C) #include D) #include
main() main()
{ { int *p;
int *p; fun(p);
fun(&p) ;}
}
int fun(int **p)int fun(int *p)
{ *p=(int *)malloc
(2);}{p=(int *)malloc(sizeo(int));}
答案C
(49)以下叙述中不正确的是
A)C语言中的文本文件以ASCⅡ码形式存储数据
B)C语言中对二进制文件的访问速度比文本文件快
C)C语言中,随机读写方式不适用于文本文件
D)C语言中,顺序读写方式不适用于二进制文件
选D,顺序读写方式可以读二进制文件啊
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
有一个数组a[1000]存放0--1000;要求每隔二个数删掉一个数,到末尾时循环至开头继续进行,求最后一个被删掉的数的原始下标位置。
以7个数为例:
{0,1,2,3,4,5,6,7} 0-->1-->2(删除)-->3-->4-->5(删除)-->6-->7-->0(删除),如此循环直到最后一个数被删除。
#include
using namespace std;
#define null 0
struct node
{
int data;
node* next;
};
int main()
{
node* head=new node;
head->data=0;
head->next=null;
node* p=head;
for(int i=1;i<1000;i++)
{
node* tmp=new node;
tmp->data=i;
tmp->next=null;
head->next=tmp;
head=head->next;
}
head->next=p;
while(p!
=p->next)
{
p->next->next=p->next->next->next;
p=p->next->next;
}
cout<data;
return 0;
}
-------------------------------------------------------------------------------------------------------------------------------
人家要求就是用数组,我程序的答案也是603
#include
const int size=1000;
void main()
{
int arr[size];
int currentSize=size; //指示当前数组中还剩余有效元素的个数;
int count=0; //计数用;
for(int k=0;k arr[k]=k;
for(int i=0;(i=1); i=(i+1)%size)
{
if(arr[i]!
=-1)
{
if(count>=0 && count<2) {count++;}
else if( count==2)
{
arr[i]=-1;//删除此时的元素;
currentSize--;//有效元素减一;
count=0;//并将计数值归零;
}
}
}
for(int j=0;j {
if(arr[j]!
=-1)
{
cout<<"the result is :
"< break;
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
有一个数组A[nSize],其元素含有多个0,求一函数将所有的非零元素移到前面(不分大小,按原位置前移).并返回非零函数的个数i.
#include "stdafx.h"
#include
#include
#define MAX_NUM 20
//1解法
//int move(int array[],int nsize){
//int num=0;
//for(int i=0;i //if(array[i]!
=0){
//array[num]=array[i];
//if(num!
=i)
//array[i]=0;
//num++;
//}
//}
//return num;
//}
//2解法
/