---
title: "华为历年机试题"
author: "wangleide414"
date: "2012-09-17T21:30:29Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2012-09-17T21:30:29Z"
id: 1680058
url: "https://wangleide414.iteye.com/blog/1680058"
---

1.找出一个数组中满足2^N的元素

#include &lt;iostream&gt;

using namespace std;

int find(int a\[],int len);

void main()

{

int a\[]={1,2,3,5,7,8,16};

int len=sizeof(a)/sizeof(int);

cout&lt;&lt;find(a,len)&lt;&lt;endl;

}

int find(int a\[],int len)

{

int i;

int count=0;

for(i=0;i&lt;len;i++)

{

if(0==(a\[i]&(a\[i]-1)))

count++;

}

return count;

}

 

#include &lt;iostream&gt;

using namespace std;

void func(int n, int m, int s, int a\[]);

void main()

{

int a\[9]={0};

func(9,3,1,a);

for(int i=8;i&gt;=0;i--)

cout&lt;&lt;a\[i]&lt;&lt;" ";

cout&lt;&lt;endl;

}

void func(int n, int m, int s, int a\[])

{

int s1;

int w;

s1=s;

for(int k=0;k&lt;n;k++)

{

a\[k]=k+1;

}

for(int i=n;i&gt;=2;i--)

{

s1=(s1+m-1)%i;

if(s1==0)

s1=i;

w=a\[s1-1];

for(int j=s1;j&lt;i;j++)

a\[j-1]=a\[j];

a\[i-1]=w;

}

}

 

2.报数：共n个人 从1编号，设从第s个人报号，报到m出队

#include&lt;iostream&gt;

using namespace std;

void Joseph(int n, int m, int s);

int main()

{

Joseph(9,3,1);

return 0;

}

void Joseph(int n, int m, int s)

{

int i,j,w;

int s1 = s;

int a\[100] = {0};

for(i = 0; i &lt; n; i++) //把n个人的序号放入数组a\[]中；

{

a\[i] = i + 1;

}

for(i = n; i&gt;= 2; i--)

{

s1 = (s1+m-1)%i; //s1每次出圈人的位置

if(s1 == 0) //如果s1等于0，则说明要开始报数的人是最后一个人

{

s1 = i; //把此时变量i的值赋给s1

}

w = a\[s1-1]; //把每次出圈人的序号赋给w

for(j = s1; j &lt; i; j++)

{

a\[j-1] = a\[j];

}

a\[i-1] = w; //把每次出圈人的序号赋给倒数第i个位置上

}

for(int k = n-1; k &gt;= 0; k--)

cout&lt;&lt;a\[k]&lt;&lt;" ";

cout&lt;&lt;endl;

}

3.统计一个数二进制表达中0的个数（首位1之前0不计）

#include &lt;iostream&gt;

using namespace std;

int fun(int num);

int main()

{

int num;

cout&lt;&lt;"Please enter a integer:\\n";

cin&gt;&gt;num;

cout&lt;&lt;fun(num)&lt;&lt;endl;

return 0;

}

int fun(int num)

{

int count = 0;

int i = 0;

while (num)

{

if (num & 1)

{

count++;

}

num = num &gt;&gt; 1;

i++;

}

return (i-count);

}

 

4.镜像反转二进制表达式，并输出十进制值

#include&lt;iostream&gt;

using namespace std;

int func(int a);

main()

{

int n;

cout&lt;&lt;"enter:";

cin&gt;&gt;n;

cout&lt;&lt;func(n)&lt;&lt;endl;

}

int func(int a)

{

int val=0;

int temp;

int i;

int n=0;

int b\[100];

while(a!=0)

{

temp=(a&1);

b\[n++]=temp;

a=(a&gt;&gt;1);

}

for(i=0;i&lt;n;i++)

val=val\*2+b\[i];

return val;

}

5.判断一个字符串中（）是否配对

#include&lt;iostream&gt;

using namespace std;

bool match(char a\[],int length);

int main()

{

char b\[100];

int len;

bool m;

cout&lt;&lt;"enter:"&lt;&lt;endl;

gets(b);

len=strlen(b);

m=match(b,len);

if(m) cout&lt;&lt;"match"&lt;&lt;endl;

else cout&lt;&lt;"nonmatch"&lt;&lt;endl;

return 0;

}

bool match(char a\[],int length)

{

char \*p=a;

int count1=0;

int count2=0;

while(\*p!='\\0')

{

if(\*p=='(') count1++;

if(\*p==')') count2++;

if(count2&gt;count1)

return false;

p++;

}

if(count1==count2)

return true;

else

return false;

}

7.查找子字符串个数

#include &lt;iostream&gt;

#include &lt;string&gt;

using namespace std;

int fun(char \*str, char \*substr);

int main()

{

char str\[100];

char substr\[10];

cout&lt;&lt;"输入字符串：\\n";

gets(str);

cout&lt;&lt;"输入字串：\\n";

gets(substr);

cout&lt;&lt;fun(str, substr)&lt;&lt;endl;

return 0;

}

int fun(char \*str, char \*substr)

{

int count = 0;

while (\*str)

{

char \*p = str;

char \*q = substr;

while (\*q)

{

if (\*p == \*q)

{

p++;

q++;

}

else

{

break;

}

}

if (\*q == 0)

{

str = str + strlen(substr);

count++;

}

else

{

str++;

}

}

return count;

}

8.关于数组的循环移位

#include &lt;iostream&gt;

using namespace std;

void func(int \*p, int n, int k);

void main()

{

int a\[]={1,2,3,4,5};

int i;

func(a,5,2);

for(i=0;i&lt;5;i++)

cout&lt;&lt;a\[i]&lt;&lt;" ";

cout&lt;&lt;endl;

}

void func(int \*p ,int n, int k)

{

int temp;

int i;

k=k%n;

if(k&gt;=0)

{

while(k)

{

temp=p\[n-1];

for(i=n-1;i&gt;0;i--)

p\[i]=p\[i-1];

p\[0]=temp;

k--;

}

}

else if(k&lt;0)

{

k=k\*(-1);

while(k)

{

temp=p\[0];

for(i=1;i&lt;n;i++)

p\[i-1]=p\[i];

p\[n-1]=temp;

k--;

}

}

}

 

链表

#include &lt;iostream&gt;

#include &lt;stdio.h&gt;

#include &lt;string.h&gt;

#include &lt;conio.h&gt;

using namespace std;

typedef struct student

{

int data;

struct student \*next;

}node;

/* 链表的创建\*/

node \*creat()

{

node \*head,\*p,\*s;

int x;

int cycle=1;

head=(node\*)malloc(sizeof(node));

p=head;

while(cycle)

{

cout&lt;&lt;"please enter a number"&lt;&lt;endl;

cin&gt;&gt;x;

if(x!=0)

{

s=(node\*)malloc(sizeof(node));

s-&gt;data=x;

p-&gt;next=s;

p=s; //前一个节点的地址

}

else

cycle=0;

}

head=head-&gt;next;

p-&gt;next=NULL;

return (head);

}

void reverse(node \*\*head)

{

node \*p1,\*p2,\*p3;

//if(\*head==NULL||\*head-&gt;next==NULL) return \*head;

p1=\*head;

p2=p1-&gt;next;

while(p2)

{ p3=p2-&gt;next;

p2-&gt;next=p1;

p1=p2;

p2=p3;

}

(\*head)-&gt;next=NULL;

\*head=p1;

//return \*head;

}

void main()

{

node \*p;

node \*head;

//int n;

head=(node\*)malloc(sizeof(node));

head=creat();

//n=length(head);

p=head;

cout&lt;&lt;"原始链表是：";

if(head!=NULL) //原始链表的打印

while(p!=NULL)

{

cout&lt;&lt;p-&gt;data&lt;&lt;" ";

p=p-&gt;next;

}

cout&lt;&lt;endl;

node \*\*q = &head;

reverse(q);

p=head;

// cout&lt;&lt;"链表长度为："&lt;&lt;n;

/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/

cout&lt;&lt;"逆置后链表是：";

if(head!=NULL) //逆置后链表的打印

while(p!=NULL)

{

cout&lt;&lt;p-&gt;data&lt;&lt;" ";

p=p-&gt;next;

}

cout&lt;&lt;endl;

}

#include &lt;iostream&gt;

#include &lt;stdio.h&gt;

#include &lt;string.h&gt;

#include &lt;conio.h&gt;

using namespace std;

typedef struct student

{

int data;

struct student \*next;

}node;

/* 链表的创建\*/

node \*creat()

{

node \*head,\*p,\*s;

int x;

int cycle=1;

head=(node\*)malloc(sizeof(node));

p=head;

while(cycle)

{

cout&lt;&lt;"please enter a number"&lt;&lt;endl;

cin&gt;&gt;x;

if(x!=0)

{

s=(node\*)malloc(sizeof(node));

s-&gt;data=x;

p-&gt;next=s;

p=s;

}

else

cycle=0;

}

head=head-&gt;next;

p-&gt;next=NULL;

return (head);

}

/\*链表的测长\*/

int length(node \*head)

{

int n=0;

node \*p;

p=head;

while(p!=NULL)

{

p=p-&gt;next;

n++;

}

return n;

}

/\*链表的排序\*/

node \*sort(node* head, int n)

{

int i;

int j;

int temp;

node \*p;

if(head==NULL||head-&gt;next==NULL)

return (head);

p=head;

for(i=1;i&lt;n;i++)

{

p=head;

for(j=0;j&lt;n-i;j++)

{

if(p-&gt;data&gt;p-&gt;next-&gt;data)

{

temp=p-&gt;next-&gt;data;

p-&gt;next-&gt;data=p-&gt;data;

p-&gt;data=temp;

}

p=p-&gt;next;

}

}

return (head);

}

/\*链表的逆置\*/

node \*reverse(node* head)

{

node \*p1;

node \*p2;

node \*p3;

if(head==NULL||head-&gt;next==NULL)

return head;

p1=head;

p2=p1-&gt;next;

while(p2)

{

p3=p2-&gt;next;

p2-&gt;next=p1;

p1=p2;

p2=p3;

}

head-&gt;next=NULL;

head=p1;

return head;

}

/\*链表插入一个节点\*/

node \*insert(node* head,int num)

{

node \*p0,\*p1,\*p2;

p1=head;

p0=(node\*)malloc(sizeof(node));

p0-&gt;data=num;

while((p0-&gt;data)&gt;(p1-&gt;data)&&p1-&gt;next!=NULL)

{

p2=p1;

p1=p1-&gt;next;

}

if(p0-&gt;data&lt;=p1-&gt;data)

{

if(head==p1) //插入的是头结点

{

p0-&gt;next=p1;

head=p0;

}

else

{

p2-&gt;next=p0; //插入的是中间结点

p0-&gt;next=p1;

}

}

else //插入的是尾结点

{

p1-&gt;next=p0;

p0-&gt;next=NULL;

}

return (head);

}

/\*链表删除一个节点\*/

node \*del(node* head,int num)

{

node \*p1,\*p2;

p1=head;

while(num!=p1-&gt;data&&p1-&gt;next!=NULL)

{

p2=p1;

p1=p1-&gt;next;

}

if(num==p1-&gt;data)

{

if(head==p1)

{

head=p1-&gt;next;

free(p1);

}

else

p2-&gt;next=p1-&gt;next;

}

else

cout&lt;&lt;num&lt;&lt;"couldn't be found"&lt;&lt;endl;

return (head);

}

void main()

{

node \*p;

node \*head;

int n;

head=(node\*)malloc(sizeof(node));

head=creat();

n=length(head);

p=head;

cout&lt;&lt;"原始链表是：";

if(head!=NULL) //原始链表的打印

while(p!=NULL)

{

cout&lt;&lt;p-&gt;data&lt;&lt;" ";

p=p-&gt;next;

}

cout&lt;&lt;endl;

head=reverse(head);

p=head;

cout&lt;&lt;"链表长度为："&lt;&lt;n;

/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/

cout&lt;&lt;"逆置后链表是：";

if(head!=NULL) //逆置后链表的打印

while(p!=NULL)

{

cout&lt;&lt;p-&gt;data&lt;&lt;" ";

p=p-&gt;next;

}

cout&lt;&lt;endl;

/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/

head=sort(head,n);

p=head;

cout&lt;&lt;"排序后链表：";

if(head!=NULL) //排序后链表的打印

while(p!=NULL)

{

cout&lt;&lt;p-&gt;data&lt;&lt;" ";

p=p-&gt;next;

}

cout&lt;&lt;endl;

/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/

int a;

cout&lt;&lt;"insert a number:"&lt;&lt;endl;

cin&gt;&gt;a;

head=insert(head,a);

p=head;

cout&lt;&lt;"插入后链表是：";

if(head!=NULL) //插入后链表的打印

while(p!=NULL)

{

cout&lt;&lt;p-&gt;data&lt;&lt;" ";

p=p-&gt;next;

}

cout&lt;&lt;endl;

/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/

int b;

cout&lt;&lt;"delete number:"&lt;&lt;endl;

cin&gt;&gt;b;

head=del(head,b);

p=head;

cout&lt;&lt;"删除后链表是：";

if(head!=NULL) //删除后链表的打印

while(p!=NULL)

{

cout&lt;&lt;p-&gt;data&lt;&lt;" ";

p=p-&gt;next;

}

cout&lt;&lt;endl;

}

 

9.两个大数相加

//超大整数加法运算

//#include "stdafx.h"

#include &lt;string&gt;

#include&lt;iostream&gt;

using namespace std;

#define ln 100 //数字长度

int main(int argc, char* argv\[])

{

int la = 0, lb = 0;

int A\[ln];

int B\[ln];

for(int l=0; l&lt;ln; l++)

{

A\[l]=0;

B\[l]=0;

}

char a\[ln];

cout &lt;&lt; "输入一个高精度数(小于100位）作被加数:" &lt;&lt; endl;

cin.getline(a, ln);

la =strlen(a);

for (int i=0; i&lt;la; i++)

{

A\[i] = int(a\[la-1-i])-48;

}

char b\[ln];

cout &lt;&lt; "输入另一个高精度数(小于100位）作加数:" &lt;&lt; endl;

cin.getline(b, ln);

lb =strlen(b);

for (i=0; i&lt;lb; i++)

{

B\[i] = int(b\[lb-1-i])-48;

}

int lc;

if (la&gt;lb)

{

lc = la;

}

else

lc = lb;

for (int j=0; j&lt;lc; j++)

{

A\[j] = A\[j]+B\[j];

A\[j+1] = A\[j+1]+A\[j]/10;

A\[j] = A\[j]%10;

}

while (A\[lc-1]&gt;10)

{

A\[lc]=A\[lc-1]/10;

lc += 1;

}

cout &lt;&lt; "相加结果：" &lt;&lt; endl;

for (j=lc-1; j&gt;=0; j--)

{

cout &lt;&lt; A\[j];

}

cout &lt;&lt; endl;

return 0;

}

 

10.将字符串中的所有字母都替换成该字母的下一个字母

#include &lt;iostream&gt;

using namespace std;

#include &lt;ctype.h&gt;

#include &lt;stdio.h&gt;

#include &lt;string.h&gt;

void func(char \*p);

void main()

{

char str1\[20];

printf("enter:");

gets(str1);

func(str1);

puts(str1);

}

void func(char \*p)

{

char ch;

while(\*p)

{ ch=\*p;

if(isalpha(\*p)&&(\*p!='z')&&(\*p!='Z'))

\*p=ch+1;

else if(\*p='z')

\*p='a';

else if(\*p='Z')

\*p='A';

p++;

}

}

11．回文判断

#include&lt;iostream&gt;

using namespace std;

bool func(int m);

void main()

{

int m;

cout&lt;&lt;"enter a number:"&lt;&lt;endl;

cin&gt;&gt;m;

cout&lt;&lt;func(m)&lt;&lt;endl;

}

bool func(int m)

{

int i,n=0;

i=m;

while(i)

{

n=n\*10+i%10;

i/=10;

}

if(m==n)

return true;

return false;

}

#include&lt;iostream&gt;

using namespace std;

#include&lt;string.h&gt;

bool is\_huiwen(char a\[],int length)

{

const char \*src=a;

const char \*end;

end=src+length-1;

while(src&lt;end)

{ if(\*src==\*end)

{ src++;end--;}

else return false;

}

return true;

}

int main()

{ int len;

char c\[10];

cout&lt;&lt;"enter:"&lt;&lt;endl;

cin&gt;&gt;c;

len=strlen(c);

bool h;

h=is\_huiwen(c,len);

if(h) cout&lt;&lt;"hui\_wen"&lt;&lt;endl;

else cout&lt;&lt;"non\_hui\_wen"&lt;&lt;endl;

return 0;

}

12.将一个“1234”的字符串转化为1234整型

#include &lt;iostream&gt;

#include &lt;stdio.h&gt;

#include &lt;string.h&gt;

using namespace std;

int func(char a\[]);

void main()

{

char a\[]={'1','2','3','4','\\0'};

cout&lt;&lt;func(a)&lt;&lt;endl;

}

int func(char a\[])

{

int i=0;

int sum=0;

while(a\[i]!='\\0')

{

sum=sum\*10+(a\[i]-'0');

i++;

}

return sum;

}

13.求一个二维数组每列的最小值

#include &lt;iostream&gt;

using namespace std;

void fun(int input\[3]\[4], const int m, const int n, int output\[4]);

int main()

{

int input\[3]\[4] = { {21,48,86,92},

{10,23,12,69},

{46,78,49,13}};

int output\[4];

fun(input, 3, 4, output);

cout&lt;&lt;"原二维数组是："&lt;&lt;endl;

for(int i=0; i&lt;3; i++)

{

for(int j=0; j&lt;4; j++)

{

cout&lt;&lt;input\[i]\[j]&lt;&lt;" ";

}

cout&lt;&lt;endl;

}

cout&lt;&lt;"每列最小值是："&lt;&lt;endl;

for (int k=0; k&lt;4; k++)

{

cout&lt;&lt;output\[k]&lt;&lt;" ";

}

cout&lt;&lt;endl;

return 0;

}

void fun(int input\[3]\[4], const int m, const int n, int output\[4])

{

int i = 0;

int j = 0;

for (i=0; i&lt;n; i++)

{

output\[i] = input\[0]\[i];

for (j=0; j&lt;m; j++)

{

if (output\[i] &gt; input\[j]\[i])

{

output\[i] = input\[j]\[i];

}

}

}

}

14.连续字符统计（如AABCCCD：A2B1C3D1）

#include &lt;iostream&gt;

#include &lt;stdio.h&gt;

#include &lt;string.h&gt;

using namespace std;

void func(char str\[],int len);

void main()

{

char str\[20];

int len;

cout&lt;&lt;"enter:";

gets(str);

len=strlen(str);

func(str,len);

}

void func(char str\[], int len)

{

int count=1;

int i;

for(i=0;i&lt;len;i++)

{

if(str\[i]==str\[i+1])

count++;

else

{

cout&lt;&lt;str\[i]&lt;&lt;count;

count=1;

}

}

cout&lt;&lt;endl;

}

15.找出一个字符串中是否包含相同的子字符串（要求子串长度大于等于2）

#include &lt;iostream&gt;

#include &lt;string&gt;

using namespace std;

int fun(char* str, int n)

{

char \*temp = new char\[n+2];

char \*str2 = str;

int sum = 0;

int outsum = 0;

char* t;

for(int i = 0; i &lt; n; i++)

{

for(int j = 2; j &lt;= n-i; j++)

{

sum = 0;

str2 = str;

memset(temp,0,(n+2)\*sizeof(char));

strncpy(temp, str+i, j);

while((t = strstr(str2,temp)) != NULL)

{

sum++;

str2 = t+j;

}

if( sum &gt; outsum)

{

outsum = sum;

}

}

}

if(outsum == 1)

return 0;

return outsum;

}

int main()

{

char strstr\[1000];

memset(strstr,0,sizeof(strstr));

char \*s = strstr;

cin &gt;&gt; s;

int n =strlen(s);

int outsum;

outsum = fun(s,n);

cout &lt;&lt; outsum &lt;&lt; endl;

system("pause");

return 0;

}

16．已知：yi er san si wu liu qi ba jiu 分别对应123456789，对一段只含有这几种字符的字符串进行转

换，转换成相应的数字

如：yiersansan：1233

#include &lt;iostream&gt;

#include &lt;string&gt;

using namespace std;

char* sss\[9] = {"yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};

int fun(char* str)

{

int i;

int sum = 0;

int d = 0;

i = 0;

int j;

while(str\[i] != '\\0')

{

if(str\[i] == 'y' || str\[i] == 'e'|| str\[i] == 'w' || str\[i] == 'q' || str\[i] ==

'b')

d = 2;

else if(str\[i] == 'l' || str\[i] == 'j')

d = 3;

else if(str\[i] == 's')

{

if(str\[i+1] == 'a')

d = 3;

else d = 2;

}

for(int k = 0; k &lt; 9; k++)

if(strncmp(str+i,sss\[k],d) == 0)

j = k+1;

sum = 10\*sum + j;

i = i+d;

}

return sum;

}

int main()

{

char strstr\[1000];

memset(strstr,0,sizeof(strstr));

char \*s = strstr;

cin &gt;&gt; s;

int outsum;

outsum = fun(s);

cout &lt;&lt; outsum &lt;&lt; endl;

system("pause");

return 0;

}

 

17.删除字符串中字符个数最少的字符

#include &lt;iostream&gt;

#include &lt;string&gt;

using namespace std;

char* fun(char* str, int n)

{

int hash\[256] = {0};

char \*result = new char\[n+1];

memset(result,0,(n+1)\*sizeof(char));

for(int i = 0; i &lt; n; i++)

{

hash\[str\[i]]++;

}

int min = 0x7fffffff;

for(int i = 0; i &lt; 256; i++)

{

if(hash\[i] &lt; min && hash\[i] != 0)

min = hash\[i];

}

for(int i = 0,j = 0; i &lt; n; i++)

{

if(hash\[str\[i]] != min)

{

result\[j++] = str\[i];

}

}

return result;

}

int main()

{

char strstr\[1000];

memset(strstr,0,sizeof(strstr));

char \*s = strstr;

cin &gt;&gt; s;

char \*re = NULL;

re = fun(s,strlen(s));

cout &lt;&lt; re &lt;&lt; endl;

system("pause");

return 0;

}