华为机考题1

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

#include <iostream>
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<<find(a,len)<<endl;

}
 int find(int a[],int len)
  {
     int i;
     int count=0;
     for(i=0;i<len;i++)
      {
          if(0==(a[i]&(a[i]-1)))
//与运算之后为0,说明是2的幂
              count++;
      }
      return count;
  }
View Code

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

最容易理解的一种方法:

#include<iostream>
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 < n; i++)
 {
  a[i] = i + 1;
 }
//先编号
 for(i = n; i>= 2; i--)
 {
  s1 = (s1+m-1)%i;
//每报一次出去的人的编号,而后总人数-1,i就是人数,每循环一次就-1
  if(s1 == 0)
  {
   s1 = i;
//一旦数到最后一个元素,s1的值就变成i=元素总数,因为这是求余运算
  }
  w = a[s1-1];//出去的元素值,
//出队人的值
  for(j = s1; j < i; j++)
  {
   a[j-1] = a[j];
//只剩i-1个元素,依次往前序号-1;
  }
  a[i-1] = w;
//出去的元素存在最后一个位置,依次往前排列
 }

 for(int k = n-1; k >= 0; k--)
     cout<<a[k]<<" ";

 cout<<endl;

}
View Code

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

#include <iostream>
using namespace std;

int fun(int num);
int main()
{
    int num;

    cout<<"Please enter a integer:
";
    cin>>num;

    cout<<fun(num)<<endl;

    return 0;
}

int fun(int num)
{
    int count = 0;    
    
    while (num)
    {
        if ((num & 1)==0)
        {
//按位与1进行与,结果为0,表示此位为0,然后原数右移
            count++;
        }
        num = num >> 1;
        
    }

    return count;
}
View Code

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

#include<iostream>
using namespace std;
int func(int a);
main()
{
    int n;
    cout<<"enter:";
    cin>>n;
    cout<<func(n)<<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>>1);
    }
//先把二进制数翻转,存于数组中,
    for(i=0;i<n;i++)
        val=val*2+b[i];
    //val=val+(2^(n-1-i))*b[i];
//每一位的值相加得到
    return val;

}
View Code

5.判断一个字符串中()是否配对

#include<iostream>
using namespace std;
bool match(char a[],int length);
int main()
 { 
   char b[100];
   int len;
   bool m;
   cout<<"enter:"<<endl;
   gets(b);
   len=strlen(b);
   m=match(b,len);
   if(m) cout<<"match"<<endl;
   else cout<<"nonmatch"<<endl;
   return 0;
 }

bool match(char a[],int length)
 { 
    char *p=a;
    int count1=0;
    int count2=0;
   
 while(*p!='')//当没有到尾部时
   { 
       if(*p=='(') count1++;
       if(*p==')') count2++;
       if(count2>count1)
      
           return false;
        //使用指针和一个计数器;。  
       
       p++;
   }
    if(count1==count2) 
        return true;
    else 
        return false;
}
 
View Code

6.链表倒序

Node *Reverse(Node *head)
{
    Node *p1,*p2,*p3;
    if(head==NULL||head->next==NULL)
        return head;
    p1=head;
    p2=p1->next;
    while(p2)
    {
        p3=p2->next;
        p2->next=p1;
        p1=p2;
        p2=p3;
    }
    head->next=NULL;
    head=p1;
    return head;
}

7.查找子字符串个数

#include <iostream>
#include <string>
using namespace std;

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

int main()
{
    char str[100];
    char substr[10];
    cout<<"输入字符串:
";
    gets(str);
    cout<<"输入字串:
";
    gets(substr);
    cout<<fun(str, substr)<<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;
}
原文地址:https://www.cnblogs.com/fickleness/p/3347307.html