华为机试.docx
《华为机试.docx》由会员分享,可在线阅读,更多相关《华为机试.docx(22页珍藏版)》请在冰豆网上搜索。
华为机试
1、句子逆序。
例如“Iamaboy”,逆序排放后为“boyaamI”
#include
usingnamespacestd;
voidRS(char*bp,char*ep)
{
while(bp{
chartem=*bp;
*bp=*ep;
*ep=tem;
bp++;
ep--;
}
}
char*Reverse(char*s)
{
intlen=strlen(s);
char*es=s+len-1;
RS(s,es);
char*p1=s;
char*p2=s;
while(*p2!
='\0')
{
while(*p2!
='\0'&&*p2!
='')
p2++;
RS(p1,p2-1);
if(*p2==''&&*p2!
='\0')
{
p2++;
p1=p2;
}
}
returns;
}
intmain()
{
charstr[500];
gets(str);
cout<}
2、数字颠倒。
如输入为100,则输出为001
#include
#include
usingnamespacestd;
voidmain()
{
intnum;
charstr[32];
inti;
cin>>num;
itoa(num,str,10);
for(i=strlen(str)-1;i>=0;i--)
{
cout<}
}
3、输入一个int型整数,按照从右向左的阅读顺序,返回一个不含重复数字的新的整数。
#include
usingnamespacestd;
voidgetNoRepeatNumber(intnum)
{
charbuffer[20];
charc[20];
inti,j=0,k,lenbuffer,flag;
memset(buffer,0,20);
itoa(num,buffer,10);
lenbuffer=strlen(buffer);
c[0]=buffer[lenbuffer-1];
for(i=lenbuffer-2;i>=0;i--)
{
flag=1;
for(k=0;k{
if(buffer[i]==c[k])
flag=0;
}
if(flag)
{
j=j+1;
c[j]=buffer[i];
}
}
j++;
c[j]='\0';
for(k=0;c[k]!
='\0';k++)
cout<cout<}
intmain()
{
intnum;
cin>>num;
getNoRepeatNumber(num);
return0;
}
4、接受一个十六进制的数值字符串,输出该数值的十进制字符串。
例如输入0XA,输出10。
#include
usingnamespacestd;
intmain()
{
chara[10];
gets(a);
intlen=strlen(a);
intsum=0,quan=1;
for(inti=len-1;i>1;i--)
{
if(a[i]>='0'&&a[i]<='9')
{
sum=sum+(a[i]-48)*quan;
quan=quan*16;
}
elseif(a[i]>='A'&&a[i]<='F')
{
sum+=(a[i]-55)*quan;
quan*=16;
}
elseif(a[i]>='a'&&a[i]<='f')
{
sum+=(a[i]-87)*quan;
quan*=16;
}
}
cout<}
5、输入一个正整数,按照从小到大的顺序输出它的所有质数的因子(如180的质数因子为22335)
#include
usingnamespacestd;
voidgetResult(longintinput)
{
for(inti=2;i<=input;i++)
{
while((0==input%i)&&input!
=0)
{
cout<
input=input/i;
}
}
}
intmain()
{
inta;
cin>>a;
getResult(a);
return0;
}
6、输入A和B的最小公倍数。
输入57,输出35。
#include
usingnamespacestd;
intgdc(intx,inty)
{
return(!
y)?
x:
gdc(y,x%y);
}
intmain()
{
inta,b;
cin>>a;
cin>>b;
cout<<(a*b)/gdc(a,b)<return0;
}
7、匹配字符串。
#include
#include
usingnamespacestd;
boolCompareString(char*shortstr,char*longstr)
{
intshortLength;
intlongLength;
shortLength=strlen(shortstr);
longLength=strlen(longstr);
if(longLength<=0||shortLength<=0)
{
returnfalse;
}
for(inti=0;i{
for(intj=0;j{
if((*(shortstr+i))==(*(longstr+j)))
{
break;
}
}
if(j>=longLength)
{
returnfalse;
}
}
returntrue;
}
intmain()
{
char*shortstr=newchar[100];
char*longstr=newchar[100];
boolresult;
cin>>shortstr;
cin>>longstr;
result=CompareString(shortstr,longstr);
cout<return0;
}
8、输出重复的英文字符
在字符串中,将重复(重复次数可以两次以上)的英文字符(字符包括a~z、A~Z)挑选出来输出,不重复的不输出。
输入:
AACCDDAA
输出:
ACD
C语言:
#include
#include
#include
#defineMAX_IN_SIZE1000
intprint_repeat_chars(char*pszString)
{
charcExistChar[52];
intnExistCharCount=0;
intnApperTimes[52];
inti,nStrLen=0;
chartemp;
memset(nApperTimes,0,sizeof(nApperTimes));
nStrLen=strlen(pszString);
for(i=0;i{
temp=*(pszString+i);
if(temp>='A'&&temp<='Z')
{
if(nApperTimes[temp-'A']==0)
{
cExistChar[nExistCharCount++]=temp;
}
nApperTimes[temp-'A']++;
}
elseif(temp>='a'&&temp<='z')
{
if(nApperTimes[temp-'a'+26]==0)
{
cExistChar[nExistCharCount++]=temp;
}
nApperTimes[temp-'a'+26]++;
}
}
for(i=0;i{
temp=cExistChar[i];
if(temp>='A'&&temp<='Z')
{
if(nApperTimes[temp-'A']>1)
printf("%c",temp);
}
elseif(temp>='a'&&temp<='z')
{
if(nApperTimes[temp-'a'+26]>1)
printf("%c",temp);
}
}
return0;
}
intmain(void)
{
charszInString[MAX_IN_SIZE];
scanf("%s",szInString);
print_repeat_chars(szInString);
return0;
}
9、笨笨熊搬家打包篇
森林里的笨笨熊今天可开心啦——他买了新房子,乔迁新喜要搬家了。
因此,笨笨熊请了许多好朋友来帮忙搬家,并准备了很多小纸盒用来装需要搬的物品,不过,这些纸盒的容积都是相同的,并且最多只能装两个物品。
但是,为了不打扰太多的朋友,笨笨熊想了个“聪明”办法:
让每个纸盒使用效率最高(注:
只要纸盒容积大于物品的体积之和就认为可以装下;物品体积不会大于纸盒容积),这样需要的纸盒最少。
为了帮助笨笨熊提前通知朋友,请你根据笨笨熊的办法,帮忙算出:
需要纸盒的最少数目是多少?
输入:
整数V——纸盒的容积;整数N——物品的总数目N;
输出:
整数M——需要纸盒的最少数目;
C语言
#include
#include
#defineITEMS_COUNT_MAX100
intmy_sort_bubble(int*pSortList,intnListSize)
{
inti,j;
boolbIfContinue=true;
for(i=0;i{
bIfContinue=false;
for(j=nListSize-1;j>i;j--)
{
if(pSortList[j]>pSortList[j-1])
{
bIfContinue=true;
pSortList[j]=pSortList[j]+pSortList[j-1];
pSortList[j-1]=pSortList[j]-pSortList[j-1];
pSortList[j]=pSortList[j]-pSortList[j-1];
}
}
}
return0;
}
intcalc_least_box_count(intnVolume_of_box,intnNum_of_items,int*pnVolume_of_items)
{
inti,j;
if(nNum_of_items<=1)
returnnNum_of_items;
my_sort_bubble(pnVolume_of_items,nNum_of_items);
if(pnVolume_of_items[0]>nVolume_of_box)
return-1;
for(i=0;pnVolume_of_items[i]==nVolume_of_box;i++);
j=nNum_of_items-1;
for(;i{
if(pnVolume_of_items[i]+pnVolume_of_items[j]<=nVolume_of_box)
j--;
}
if(i==j)
returni+1;
returni;//i>j
}
intmain(void)
{
intnVolume_of_box;
intnNum_of_items;
int*pnVolume_of_items;
inti;
scanf("%d",&nVolume_of_box);
scanf("%d",&nNum_of_items);
pnVolume_of_items=(int*)malloc(nNum_of_items*sizeof(int));
for(i=0;iscanf("%d",&pnVolume_of_items[i]);
printf("%d",calc_least_box_count(nVolume_of_box,nNum_of_items,pnVolume_of_items));
return0;
}
10、实现两个合法时间相加。
样例输入:
00:
00:
0000:
00:
01样例输出:
00:
00:
01
intcharToInt(char*ch)
{
intlen=strlen(ch);
intk=10;
intsum=0;
for(inti=0;i{
sum=sum*k+(ch[i]-'0');
}
returnsum;
}
intcountM(char*ch)
{
intlen=strlen(ch);
intcount=0;
intk=0;
intsum=0;
charb[3][3];
for(inti=0;iif(ch[i]<='9'&&ch[i]>='0'){
b[count][k]=ch[i];
k++;
}else{
b[count][k]='\0';
++count;
k=0;
}
}
b[count][k]='\0';
sum=charToInt(b[0])*60*60+charToInt(b[1])*60+charToInt(b[2]);
deleteb;
returnsum;
}
intmain(){
char*ch1="23:
12:
56";
char*ch2="23:
12:
56";
//countM(ch);
intnumber1=countM(ch1);
intnumber2=countM(ch2);
intsum=number1+number2;
inth=0,m=0,s=0;
s=sum%60;
m=(sum/60)%60;
h=(sum/60/60)%24%60;
charch[255];
if(h<10){
cout<<"0"<}else{
cout<}
cout<<":
";
if(m<10){
cout<<"0"<}else{
cout<}
cout<<":
";
if(s<10){
cout<<"0"<
}else{
cout<
}
cout<return0;
}
11、逆序链表输出。
样例输入:
1,2,3,4,5样例输出:
5,4,3,2,1
C语言
#include
#include
typedefstructtagListNode
{
intvalue;
structtagListNode*next;
}ListNode;
voidnew_node_create(ListNode**ppstNode,intnValue)
{
*ppstNode=(ListNode*)malloc(sizeof(ListNode));
(*ppstNode)->value=nValue;
(*ppstNode)->next=NULL;
}
voidnew_node_append(ListNode**ppstTail,char*pValue)
{
ListNode*pstNow;
new_node_create(&pstNow,atoi(pValue));
(*ppstTail)->next=pstNow;
*ppstTail=pstNow;
}
voidlist_create(ListNode**ppstHead)
{
ListNode*pstTail;
charszInput[100];
char*p;
inti;
scanf("%s",szInput);
pstTail=*ppstHead;
p=szInput;
for(i=0;szInput[i]!
='\0';i++)
{
if(szInput[i]==',')
{
szInput[i]='\0';
new_node_append(&pstTail,p);
p=szInput+i+1;
}
}
new_node_append(&pstTail,p);
}
voidlist_reverse(ListNode**ppstHead)
{
ListNode*pstNow,*pstPrev,*pstNext;
pstNow=(*ppstHead)->next;
pstPrev=*ppstHead;
while(pstNow!
=NULL)
{
pstNext=pstNow->next;
if(pstPrev==*ppstHead)
pstNow->next=NULL;
else
pstNow->next=pstPrev;
pstPrev=pstNow;
pstNow=pstNext;
}
(*ppstHead)->next=pstPrev;
}
voidlist_print(ListNode**ppstHead)
{
ListNode*pstNow;
pstNow=(*ppstHead)->next;
while(pstNow->next!
=NULL)
{
printf("%d,",pstNow->value);
pstNow=pstNow->next;
}
printf("%d",pstNow->value);
}
voidlist_free(ListNode**ppstHead)
{
ListNode*pstNow,*pstNext;
pstNow=(*ppstHead)->next;
while(pstNow!
=NULL)
{
pstNext=pstNow->next;
free(pstNow);
pstNow=pstNext;
}
}
intmain(void)
{
ListNode*pstHead;
new_node_create(&pstHead,0);
list_create(&pstHead);
list_reverse(&pstHead);
list_print(&pstHead);
list_free(&pstHead);
return0;
}
12、心有灵犀一点通。
样例输入:
3
362452
样例输出:
2
C语言:
#include
#include
#include
#defineLATTICE_MAX_NUM1000
intscore_calc_man(int*pLatticeList,intnPos)
{
inti,nScore=0;
for(i=0;i<=nPos;i++)
nScore=nScore+=pLatticeList[i];
returnnScore;
}
intscore_calc_lady(int*pLatticeList,intnPos,intnListSzie)
{
inti,nScore=0;
for(i=nPos;inScore=nScore+=pLatticeList[i];
returnnScore;
}
intprivity_calc(int*pLatticeList,intnListSzie)
{
intnManScoreList[LATTICE_MAX_NUM];
intnLadyScoreList[LATTICE_MAX_NUM];
inti,j,nCount=0;
for(i=0;i{
nManScoreList[i]=score_calc_man(pLatticeList,i);
nLadyScoreList[i]=score_calc_lady(pLatticeList,i,nListSzie);
}
for(i=0;i{
for(j=0;j{
if(nLadyScoreList[j]==nManScoreList[i])
nCount++;
}
}
returnnCount;
}
intmain(void)
{
intnCoupleCount;
in