免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: forrestgang
打印 上一主题 下一主题

华为面试题(8分钟写出代码) [复制链接]

论坛徽章:
0
341 [报告]
发表于 2012-05-25 07:36 |只看该作者
ccjjhua 发表于 2006-11-13 11:42
把a,b 2数组的元素放到数组3(2n大小) 中进行排序,
a 先取最小的,b先取次小的,
然后根据a,b已取元素和 ...


同意此楼的算法

论坛徽章:
0
342 [报告]
发表于 2012-05-31 18:20 |只看该作者
我觉着这个和用天秤称某样物体的重量是一样的,a[],b[]数组中元素合为一组sum[],sum/2是我们要称的重量,把它放入左盘,然后向右盘放砝码,先放最大的,再放次大的,最后放小的,比如:
3,6,7,9,21
sun/2=46/2=23
所以先放21,有21<23,差值为2,放入最接近2的,即sum[i]-2取最小值,这是需要遍历一下总数组,然后放入3,21+3>23,完毕,剩下的放一堆
刚才21与23差值为2,其实,我们可以根据这个差值来确定遍历时用二分法还是其他的从头查找或倒着查找,以上!!

论坛徽章:
0
343 [报告]
发表于 2012-06-03 16:28 |只看该作者
#include <stdio.h>
#include <STDLIB.H>
#include <time.h>

#define N 10

int f(int *a,int *b);

int main(int argc, char* argv[])
{
        int a[N] ={0};
        int b[N] = {0};
        int c[N + N] = {0};
        int i = 0;

        srand( (unsigned)time( NULL ) );
       
        for (i = 0;i < N;i++)
        {
                a[i] = rand() % 100;
                b[i] = rand() % 100;
                c[i] = a[i];
                c[i + N] = b[i];
        }

        for(i = 0;i < N + N;i++)
        {
                int temp;
                for (int j = i;j < N + N;j++)
                {
                        if (c[i] > c[j])
                        {
                                temp = c[i];
                                c[i] = c[j];
                                c[j] = temp;
                        }
                }
        }
       
        for(i = 0;i < N;i++)
        {
                a[i] = c[i];
                b[i] = c[i + N];
        }

        int r = f(a,b);

        printf("%d\n",r);
       
        for(i = 0;i < N;i++)
        {
                printf("%3d",a[i]);
        }
        printf("\n");
       
        for(i = 0;i < N;i++)
        {
                printf("%3d",b[i]);
        }
        printf("\n");
        return 0;
}

void f1(int &a,int &b)
{
        int temp;
       
        temp = a;
        a = b;
        b = temp;
}

int f(int *a,int *b)
{
        int result;
        int min;
        int aa,bb;
        int i = 0,j = 0;

        aa = 0;
        bb = 0;
        for (i = 0;i < N ;i++)
        {
                aa += a[i];
                bb += b[i];
        }

        min = abs(aa - bb);
        for (i = N - 1;i >= 0;i--)
        {                       
                aa -= a[i];
                bb -= b[i];
                f1(a[i],b[i]);
                aa += a[i];
                bb += b[i];

                result = abs(aa - bb);
                if(min > result)
                {
                        min = result;
                }
                else
                {
                        f1(a[i],b[i]);
                        break;
                }

        }
        return min;
}

论坛徽章:
0
344 [报告]
发表于 2012-06-03 16:30 |只看该作者
a全部给最小的,b全部给最大的。依次对换。

论坛徽章:
1
摩羯座
日期:2014-12-04 12:25:23
345 [报告]
发表于 2012-06-03 21:42 |只看该作者
把所有的数字从大到小排序,然后最大的分给数组a, 第二个分给数组b,然后后面的数字根据俩个数组的大小分,第三个数字分给数组b,再比较a,b大小, b大就把第4个数字给a,b小就把第四个数字给b(判断b的数目是不是==n,等于n就把后面的数字都给a), 后面的数字就根据第四个数字这样分配。 没代码

论坛徽章:
0
346 [报告]
发表于 2012-06-22 03:04 |只看该作者
如果能给大家带来好处也不错。

论坛徽章:
0
347 [报告]
发表于 2012-06-22 15:31 |只看该作者
回复 341# 长生不老的萝卜
恩,这个方法不错


   

论坛徽章:
0
348 [报告]
发表于 2012-06-22 20:24 |只看该作者
回复 341# 长生不老的萝卜


错误:)
2 2 4 4 6 6

你的方法:先放6,再放6,剩下是2 2 4 4,但是要求是每个组个数一样,所以还要再放一个,就算最小也是2,两组为6 6 2 , 4 4 2, 结果错误。

论坛徽章:
0
349 [报告]
发表于 2012-06-23 16:51 |只看该作者
本帖最后由 江南书匠 于 2012-06-23 16:54 编辑

不才,也小写了一段代码,耗时远不止8分钟
思路和前面各位差不多,也没想到更好的方法,时间复杂度O(n),空间复杂度O(n).
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <ctime>
  4. #include <cmath>

  5. using namespace std;
  6. #define N 16                // The length of the array

  7. int intCmp(const void *p, const void *q)
  8. {
  9.         return (*(int *)p - *(int *)q);
  10. }

  11. void findTheLeastGap(int *pArr1, int *pArr2)
  12. {
  13.         int *pArr3 = new int[2 * N];
  14.         int i, j1, j2;

  15.         qsort((void *)pArr1,(size_t)N, sizeof(int), intCmp);   // sort the array
  16.         qsort((void *)pArr2, (size_t)N, sizeof(int), intCmp);

  17.         for (i = 0, j1 = 0, j2 = 0; i < 2 * N; i++)                  // merge the arrays to the temp array
  18.         {
  19.                 if ((j1 == N)
  20.                         || ((j2 != N) && (pArr1[j1] > pArr2[j2])))
  21.                 {
  22.                         pArr3[i] = pArr2[j2];
  23.                         j2++;
  24.                 }
  25.                 else
  26.                 {
  27.                         pArr3[i] = pArr1[j1];
  28.                         j1++;
  29.                 }
  30.         }

  31.         int cnt1 = 0, cnt2 = 0;

  32.         for (i = 0, j1 = 0, j2 = 0; (i < 2 * N) && (j1 <N) && (j2 < N); i+=2)     // adjust the array one by one
  33.         {
  34.                 if (cnt1 == cnt2)
  35.                 {
  36.                         pArr1[j1++] = pArr3[i];
  37.                         pArr2[j2++] = pArr3[i + 1];
  38.                         cnt1 += pArr3[i];
  39.                         cnt2 += pArr3[i + 1];
  40.                 }
  41.                 else if (((cnt1 > cnt2) && (pArr3[i] > pArr3[i + 1]))
  42.                                 || ((cnt1 < cnt2) && (pArr3[i] < pArr3[i + 1])))
  43.                 {
  44.                                 pArr1[j1++] = pArr3[i + 1];
  45.                                 pArr2[j2++] = pArr3[i];
  46.                                 cnt1 += pArr3[i + 1];
  47.                                 cnt2 += pArr3[i];
  48.                 }
  49.                 else
  50.                 {
  51.                         pArr1[j1++] = pArr3[i];
  52.                         pArr2[j2++] = pArr3[i + 1];
  53.                         cnt1 += pArr3[i];
  54.                         cnt2 += pArr3[i + 1];
  55.                 }
  56.         }
  57.        
  58.         cout<<"The least gap is "<<abs(cnt1 - cnt2)<<endl;

  59.         delete []pArr3;
  60. }

  61. int main()
  62. {
  63.     srand((unsigned)time(NULL));        // seed for generating the array
  64.        
  65.         int *pArr1 = new int[N];
  66.         int *pArr2 = new int[N];

  67.         for (int i = 0; i < N; i++)
  68.         {
  69.                 pArr1[i] = rand() & 0xff;
  70.         }
  71.        
  72.         for (int i = 0; i < N; i++)
  73.         {
  74.                 pArr2[i] = rand() & 0xff;
  75.         }

  76.         cout<<"The initial array: "<<endl;

  77.         cout<<"Array1:"<<endl;
  78.         for(int i = 0; i < N; i++)
  79.                 cout<<pArr1[i]<<" ";
  80.         cout<<endl;
  81.        
  82.         cout<<"Array2:"<<endl;
  83.         for(int i = 0; i < N; i++)
  84.                 cout<<pArr2[i]<<" ";
  85.         cout<<endl;

  86.         findTheLeastGap(pArr1, pArr2);

  87.         cout<<"After adjustment: "<<endl;

  88.         cout<<"Array1:"<<endl;
  89.         for(int i = 0; i < N; i++)
  90.                 cout<<pArr1[i]<<" ";
  91.         cout<<endl;

  92.         cout<<"Array2:"<<endl;
  93.         for(int i = 0; i < N; i++)
  94.                 cout<<pArr2[i]<<" ";
  95.         cout<<endl;

  96.         delete []pArr1;
  97.         delete []pArr2;
  98.        

  99.         system("pause");
  100.     return 0;
  101. }
复制代码

论坛徽章:
0
350 [报告]
发表于 2012-07-08 14:48 |只看该作者
回复 89# hawk2012


   正解!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP