2023年華為面試題_第1頁
2023年華為面試題_第2頁
2023年華為面試題_第3頁
2023年華為面試題_第4頁
2023年華為面試題_第5頁
已閱讀5頁,還剩15頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

用一個(gè)雙向鏈表寫一個(gè)快速排序算法////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

2.自己寫一個(gè)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__,

__(dá)LINE__(dá)都是C里自帶的宏,分別表達(dá)當(dāng)前的文獻(xiàn)名和所有行,而調(diào)用printf函數(shù)的時(shí)候也應(yīng)當(dāng)把assert(expr)中的expr也打印出來(

wanguodu仿佛忘了).而do{}while(0)(呵呵,的確只循環(huán)一次)是為了防止在進(jìn)行宏替換的時(shí)候犯錯(cuò).////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////for(;1;)

?

}

這個(gè)程序有什么問題,會(huì)出現(xiàn)什么結(jié)果?有1,2,....一直到n的無序數(shù)組,求排序算法,并且規(guī)定期間復(fù)雜度為O(n),空間復(fù)雜度O(1),使用互換,并且一次只能互換兩個(gè)數(shù).////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////4.

以下代碼有什么問題?[C++易]

?

struct

Test

?

{

Test(

int

)

{}

Test()

{}

void

fun()

{}

?

};

?

void

main(

void

)

{

?

Test

a(1);

a.fun();

Test

b();

//應(yīng)當(dāng)是Testb;類是結(jié)構(gòu)體的擴(kuò)展,在類中封裝了對(duì)數(shù)據(jù)成員的操作,缺省的成員為私有的,而結(jié)構(gòu)體為公有的,這就是它們的區(qū)別,對(duì)構(gòu)造函數(shù)的調(diào)用,假如沒有參數(shù),是不需要加上括號(hào)的,假如加了括號(hào),就不是定義一個(gè)對(duì)象了,而是聲明了一個(gè)函數(shù),返回該類型,所以上面的Test

b(),事實(shí)上是調(diào)用一個(gè)函數(shù)名為b,返回類型為Test的函數(shù),而不是創(chuàng)建了一個(gè)對(duì)象b,去掉括號(hào)后,就是調(diào)用的沒有形參的構(gòu)造函數(shù)。?

b.fun();

//b不是Test的實(shí)例對(duì)象?

}

?

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

?

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內(nèi)存里的是多少就是多少?

cout

<<

boolalpha

<<

(int)a

==

(int&)a

)

<<

endl;

//

輸出什么?boolalpha表達(dá)什么,

//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(yī)

f

=

1.0f;

?

(int&)f

and

int

(&f)

對(duì)于后者,就是取地址后強(qiáng)制轉(zhuǎn)換為int,應(yīng)當(dāng)沒有問題;

?

但是前者,將1.0f強(qiáng)制轉(zhuǎn)換成int&,int引用類型。我們知道,float(yī)在內(nèi)存中采用的是ieee745方式:

0---00

00

00

00

,1----00

00

80

3F

,2---00

00

00

40

......

也就是說,對(duì)于f=0.0f,則轉(zhuǎn)換后還是0,但是對(duì)于f=1.0f,轉(zhuǎn)換后的結(jié)果為0x3f800000

關(guān)鍵是看清楚這是一個(gè)強(qiáng)制轉(zhuǎn)換,同時(shí)要了解float(yī)類型的存貯格式與int不同////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////六、編寫類String的構(gòu)造函數(shù)、析構(gòu)函數(shù)和賦值函數(shù)(25分)

?

已知類String的原型為:

?

class

String

{

?

public:

String(const

char

*str

NULL);//

普通構(gòu)造函數(shù)

String(const

String

&other);

//

拷貝構(gòu)造函數(shù)

~

String(void);

//

析構(gòu)函數(shù)

?

String

operat(yī)e

=(const

String

&other);//

賦值函數(shù)

privat(yī)e:

?

char

*m_data;//

用于保存字符串

};

請(qǐng)編寫String的上述4個(gè)函數(shù)。//

String的析構(gòu)函數(shù)

?

String::~String(void)

//

3分

?

delete

[]

m_data;

?

//

由于m_data是內(nèi)部數(shù)據(jù)類型,也可以寫成

delete

m_data;

}

//

String的普通構(gòu)造函數(shù)

?

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);

?

}

?

?

//

拷貝構(gòu)造函數(shù)

?

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);

}

//

賦值函數(shù)

String

&

String::operate

=(const

String

&other)

//

13分

{

//

(1)

檢查自賦值

//

4分

?

if(this

==

&other)

return

*this;

?

?

//

(2)

釋放原有的內(nèi)存資源

//

3分

delete

[]

m_dat(yī)a;

?

//

(3)分派新的內(nèi)存資源,并復(fù)制內(nèi)容

//

3分

int

length

=

strlen(other.m_data);

?

m_data

=

new

char[length+1];

//

若能加

NULL

判斷則更好

strcpy(m_data,

other.m_data);

?

?

//

(4)返回本對(duì)象的引用

//

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);

==

t.a為01,輸出就是1

printf("%d",t.b);

==

-1

t.b為11,輸出就是-1

printf("%d",t.c);

==

-1t.c為1,輸出也是-1

3個(gè)都是有符號(hào)數(shù)int嘛。這是位擴(kuò)展問題,可以查看譚浩強(qiáng)的c程序設(shè)計(jì)關(guān)于位段的部分////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////int

main(){

printf("This

is

line

1\n");

return

0;

printf("This

is

line

2\n");

不添加新函數(shù),不修改main函數(shù),不引入新文獻(xiàn),讓程序輸出:

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);

}

執(zhí)行后輸出結(jié)果是

?

A)

產(chǎn)生語法錯(cuò)B)

隨機(jī)值?C)

A D)

65

?

?

(45)以下程序段中,可以通過調(diào)用函數(shù)fun,使main函數(shù)中的指針變量p指向一個(gè)合法的整型單元的是

?

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<stdlib.h>

D)

#include<stdlib.h>

?

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)以下敘述中不對(duì)的的是

?

A)C語言中的文本文獻(xiàn)以ASCⅡ碼形式存儲(chǔ)數(shù)據(jù)

B)C語言中對(duì)二進(jìn)制文獻(xiàn)的訪問速度比文本文獻(xiàn)快

?

C)C語言中,隨機(jī)讀寫方式不合用于文本文獻(xiàn)

?

D)C語言中,順序讀寫方式不合用于二進(jìn)制文獻(xiàn)

選D,順序讀寫方式可以讀二進(jìn)制文獻(xiàn)?。?//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////有一個(gè)數(shù)組a[1000]存放0--1000;規(guī)定每隔二個(gè)數(shù)刪掉一個(gè)數(shù),到末尾時(shí)循環(huán)至開頭繼續(xù)進(jìn)行,求最后一個(gè)被刪掉的數(shù)的原始下標(biāo)位置。

?

以7個(gè)數(shù)為例:

{0,1,2,3,4,5,6,7}

0-->1-->2(刪除)-->3-->4-->5(刪除)-->6-->7-->0(刪除),如此循環(huán)直到最后一個(gè)數(shù)被刪除。#include<iostream>

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<<p->data;

return

0;

?

}-------------------------------------------------------------------------------------------------------------------------------人家規(guī)定就是用數(shù)組,我程序的答案也是603

?

#include

<iostream.h>

?

const

int

size=1000;

void

main()

?

{

?

int

arr[size];

int

currentSize=size;

//指示當(dāng)前數(shù)組中還剩余有效元素的個(gè)數(shù);

int

count=0;

//計(jì)數(shù)用;

?

for(int

k=0;k<size;k++)

?

arr[k]=k;

for(int

i=0;(i<size)

&&

(currentSize!=1);

i=(i+1)%size)

?

{

?if(arr[i]!=-1)

?{

?

?if(count>=0

&&

count<2)

{count++;}else

if(

count==2)

?

arr[i]=-1;//刪除此時(shí)的元素;

?

currentSize--;//有效元素減一;

?

??count=0;//并將計(jì)數(shù)值歸零;

}

}

}

for(int

j=0;j<size;j++)

?if(arr[j]!=-1)

{

?

? cout<<"the

result

is

:"<<j<<endl;

??break;

}

}////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////有一個(gè)數(shù)組A[nSize],其元素具有多個(gè)0,求一函數(shù)將所有的非零元素移到前面(不分大小,按原位置前移).并返回非零函數(shù)的個(gè)數(shù)i.

#include

"stdafx.h"

?

#include<iostream.h>

?

#include<iomanip.h>

#define

MAX_NUM

20

?

//1解法

//int

move(int

array[],int

nsize){

//int

num=0;

//for(int

i=0;i<nsize;i++){

//if(array[i]!=0){

?

//array[num]=array[i];

?

//if(num!=i)

//array[i]=0;

?

//num++;

//}

?

//}

?

//return

num;

//}

//2解法

?

//int

move(int

array[],int

nsize){

?

//int

temp[20];

?

//for(int

i=0;i<nsize;i++){

//temp[i]=array[i];

?

//array[i]=0;

//}

//int

num=0;

?

//for(int

j=0;j<nsize;j++){

//if(temp[j]!=0){

?

//array[num]=temp[j];

//

num++;

//}

//}

?

//return

num;

?

//}

//3解法

?

//int

move(int

array[],int

nsize){

?

//int

num=0;

?

//for(int

i=0;i<nsize;i++){

//if(array[i]==0){

?

//for(int

j=i+1;j<nsize;j++){

//if(array[j]?。?){

?

//int

temp=array[i];

//array[i]=array[j];

//

array[j]=temp;

?

//num++;

//break;

//}

//}

//}

?

//}

?

//return

num;

//}

//4解法

int

move(int

array[],int

nsize){

int

num=0,temp=0;

for(int

i=0;i<nsize;i++){

if(array[i]!=0)

num++;

?

}

?

for(int

j=0;j<nsize;j++){

if(array[j]?。?){

?

array[temp]=array[j];

?

temp++;

}

?

for(temp;temp<nsize;temp++)

array[temp]=0;

return

num;

?

}

int

main(int

argc,

char*

argv[])

int

a[MAX_NUM]={0,10,0,0,451,321,0,0,7,8,9,0,21,2,0,5,0,44,22,0,};

?

for(int

i=0;

i<MAX_NUM;

i++)

cout<<setw(4)<<a[i];

?

cout<<endl;

?

int

nZero

move(a,MAX_NUM);

cout

<<nZero<<endl;

for(i=0;

i<MAX_NUM;

i++)

cout<<setw(4)<<a[i];

cout<<endl;

return

0;

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////char

Result,arg1=8;

?

Result

=

arg1

<<

4;

Result的值是?-128--------------------------------------------------------------------------------------#define

DIV(a,b)

a/b

int

arg1

=

7,arg2

=

5;

?

float

value;

value

(float)(DIV(arg1*arg2,arg1-arg2)/2);

?

value值?3--------------------------------------------------------------------------------------

union

FLAG

?

struct

?

{

?

char

Model1;

?

char

Model2;

?

}

?

long

usVal;

?

}myFlag;

?

myFlag占用內(nèi)存大小為幾位?

4

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

char

a[6]

"abcde";

*(a+5)=?‘\0’--------------------------------------------------------------------------------------

unsigned

char

a[5];

?

unsigned

char

*p,*q;

?

a[0]

1;

?

a[1]

=

2;

a[2]

=

3;

a[3]

=

4;

?

a[4]

=

5;

?

p

=

a;

q

=

&a[3];

?

a[q-p]=?4--------------------------------------------------------------------------------------與表達(dá)式value

=

arg1<<2

+1結(jié)果等同的是?

c

a:value

=

(

arg1<<2)+1

?

b:value

=

arg1*4

+1

c:value

=

arg1<<(2

+1)

?

d:value

=

arg1*3--------------------------------------------------------------------------------------

switch(c)中c的類型不能是?

d?

a:char

b:long

?

c:unsigned

d:double--------------------------------------------------------------------------------------enum

string

x1,

?

x2,

?

x3

10,

x4,

x5

?

};

?

enum

string

x

=

x5;

x

=

?12--------------------------------------------------------------------------------------下面函數(shù)要實(shí)現(xiàn)打印hello

world的功能,哪里有錯(cuò)誤?

void

*

GetMemory(void)

?

char

str[]

"hello

world";

?

return

str;

返回棧內(nèi)存

}

?

void

Test(void)

?

{

?

char

*str

=

NULL;

?

str

=

(char*)GetMemory();

?

printf(str);

--------------------------------------------------------------------------------------下面哪里有錯(cuò)誤?

?

#define

BUFSIZE

256

?

char

Buf[BUFSIZE]

void

main()

?

{

?

int

len

=

300;

unsigned

char

*pInput;

pInput

(unsigned

char*)malloc(len);

?

sprintf(pInput,"%s","hello

eorld");

memcpy(Buf,pInput,len);

它溢出

}

上邊的定義少了個(gè);

?

memcpy參數(shù)錯(cuò)誤

--------------------------------------------------------------------------------------for(i=1;i++<4;)后,i=?5--------------------------------------------------------------------------------------請(qǐng)?jiān)谌粩?shù)的正整數(shù)中尋找符合下條件的:

完全平方數(shù),又有2個(gè)數(shù)字相同

?

如144,676

void

main()

{

?

int

a;

int

b;

?

int

x,y,z;

?

a=sqrt(1000);

?

for(int

i=10;i<a;i++)

?

{

b=i*i;

?

z=b;

?

x=z%10;

?

z=z/10;

?

y=z%10;

?

z=z/10;

?

if(x==y||x==z||y==z)

?

cout<<b<<endl;

?

}--------------------------------------------------------------------------------------雙向非循環(huán)鏈表的結(jié)果定義和一個(gè)操作函數(shù)如下,添空:

?

struct

student

{

?

char

name[64];

int

num;

?

int

age;

struct

student

prev;

struct

student

next;

?

};

struct

student

*

head

NULL;

?

int

append(char

*name,int

num,int

age)

?

{

?

struct

student

*

temp1;

?

struct

student

*

temp2;

?

temp1

=

(struct

student

*)malloc(sizeof(struct

student));

if(NULL

==

temp1)

{

?

printf("out

of

memory\n");

?

return

-1;

}

strcpy(temp1->name,name);

?

temp1->num

=

num;

temp1->age

age;

temp1->next

NULL:

if(NULL

==

head)

?

{

temp1->prev

?;

head

=

?;

}

?

else

{

?

temp2

=

head;

?

while(temp2->next!=NULL)

?

temp2

=

temp2->next;

temp1->prev

=

?;

?

temp2

->next

=

?;

return

0;

}temp1->prev=NULL;

head=temp1;

?

temp1->prev=temp2;

temp2->next=temp1;////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////char

型,bool型,float型的變量和零值比較的c++語句分別是什么

?

?

比如

?

int

i

;

?

if(i==0)或if(i!=0)////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////實(shí)數(shù)數(shù)列:

一個(gè)實(shí)數(shù)數(shù)列共有N項(xiàng),已知:

?

Ai=(Ai-1-Ai+1)/2+d

?

其中,1<i<N

N<60

(其中的Ai,Ai-1,Ai+1中,i是下標(biāo)。

?

使用鍵盤輸入N

d

A1

AN

m,求出Am,并輸出。////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////1.

Are

the

following

definitions

valid?

Why

or

why

not?

?

const

std::string

hello

=

"Hello";

const

std::string

message

=

hello

+

",

world"

+

"!";

?

2..

Are

the

following

definitions

valid?

Why

or

why

not?

?

?

const

std::string

exclam

=

"!";

?

const

std::string

message

=

"Hello"

+

",

world"

exclam;

?

2是錯(cuò)誤的

編譯器報(bào)錯(cuò):invalid

operands

of

types

`const

char[6]'

and

`const

char[8]'

to

b

?

inary

`operat(yī)or+'

1.中hello+",world"后(hello

+

",

world")就變成是"string"類型了,所以(hello+",

world")

+

"!"....;就是"string"類型

string::operator

+(const

string&)

對(duì)(hello

+

",

world")

與"!"

進(jìn)行字符串連結(jié)操作

2.中

"Hello"

+

",

world"

中間的"+"是標(biāo)準(zhǔn)運(yùn)算符,所以解決不了兩個(gè)char

*的相加操作由于"+"操作符的重載版本是:

?

string::operator+(char*)

所以,string類對(duì)象在前,char*在后是可以的,反過來就不行了!////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////將一個(gè)單鏈表反序,只有一個(gè)鏈表頭節(jié)點(diǎn)head,尚有兩個(gè)指向節(jié)點(diǎn)元素類型的指針p和q,不許申請(qǐng)新的節(jié)點(diǎn)及指針變量什么的.用c或c++實(shí)現(xiàn)算法.////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////有17枚硬幣

分3次

?

第一次分其中的1/2

第二次分其中的1/3

第三次分其中的1/9

怎么分吧?

1/2

:

1/3

:1/9

=

9:6:2加1枚湊成18,第一次分9枚,還剩9枚;然后分6枚,還剩3枚;第三次分2枚,還剩1枚。搞好這一枚物歸原主。////////////////////////////////////////////////////////////////////////////////////

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論