今天参加北京华为笔试的两道编程题(第一次在csdn写东西)

 

 

        今天参加了北京华为的c语言笔试,别的题都没记住,就记住了最后两个编程题,题目不是很难,这里写一下,供大家参考。

 

        题目1:用最简单的方法验证一个数是不是2的n此方。

 

        我的答案:

 

        #include "stdafx.h"
    

        void n_n(int s)
        {
             int a;
             a = s;
             while(1)
             {
                      if(0 == a%2)
                      {
                                   a = a/2;
                                   if(a ==1)
                                   {
                                                printf("s = %d is 2*n*n /n",s);
                                                break;
                                    }
                      }
                      else
                      {
                                       printf("s = %d is not 2*n*n /n",s);
                                       break;
                      }
             }

        }

    

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

             int sa =1023;
             int sb = 1024;

 

             n_n(sa);
             n_n(sb);
 
             return 0;
        }

 

        题目2:已知数组a[10],b[10],比较选出相同的元素保存在c[10]中,b[10]中和a[10]不同的元素保存在d[10]中。

    

        我的答案:(这个题就是完全用指针在做的,呵呵)

 

        #include "stdafx.h"
        

        void same(int* a,int* b,int* c);
        void diff(int* a,int* b,int* d);

 

        void same(int* a,int* b,int* c)
        {
         int i,j;
         for(i=0;i<10;i++)
                  for(j=0;j<10;j++)
                  {
                           if(*(a+i) == *(b+j))
                           {
                                    *c = *(a+i);
                                    c++;
                           } 
                  }
        }

 

        void diff(int* a,int* b,int* d)
        {
             int i,j,k;
             k = 0;
             for(i=0;i<10;i++)
             {
                      k=0;
                      for(j=0;j<10;j++)
                      {
                                   if( *(a+j) != *(b+i)  )
                                   {
                                            k++;
                                   }
                                   if(k==10)
                                    break;
                      }
                      if(k == 10)
                      {
                                   *d = *(b+i);
                                   d++;
                      }
             } 
        }

 

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

 

                 int a[10] = {0,1,2,3,4,5,6,7,8,9};
                 int b[10] = {2,5,6,7,9,12,11,23,456,75};
                 int c[10],d[10];
                 int i;

             

                same(a,b,c);
                diff(a,b,d);

                

                for(i=0;i<10;i++)
                 {
                          printf("c[%d] = %d /n",i,c[i]);
                 }

                 printf("/n/n");
                 for(i=0;i<10;i++)
                 {
                          printf("d[%d] = %d /n",i,d[i]);
                 }
 
                 return 0;
        }

 

 

 

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值