LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
楼主: wes109

[算法]Google的面试题

[复制链接]
发表于 2006-11-20 19:14:51 | 显示全部楼层
数学的东西忘的差不多了,不过随便写了个程序,代码如下:
/***************************/
/*
* lsosa.BIT
*/

#include <iostream>

using namespace std;

int countOnes(int n){
        // get the numbers of ones of the n number;
        unsigned int ones = 0;
        unsigned int remainder;

        while( n >= 10){
                remainder = n % 10;
                if ( remainder == 1 ){
                        ones ++;
                }
                n = n / 10;
        }
        if ( n > 0 && n < 10 ){
                if ( n == 1){
                        ones ++;
                }
        }
        return ones;
}

int main(int argc, char ** argv){
        //
        unsigned int sum = 0;                // 前n个数的包含1个数的和;
        unsigned int n = 1;       
        unsigned int i = 0;       
        unsigned int step;        // 每个n包含的1的个数;
        int counts = 10000;                // 查找的符合要求的数字的个数;
       
        while(true){
                //得到n包含1的个数;
                step = countOnes(n);
                // 累加到sum上
                sum += step;
                if ( sum == n ){
                        cout << "the " << i + 1 << " number is " << n <<endl;
                        if ( i++ == counts ){
                                // find 5 ;
                                break;
                        }
                }
                n ++;
        }
       
}

/************************************/

运行效率不敢恭维,写个程序,抛砖引玉吧;

运行效果:

/************************************/

the 1 number is 1
the 2 number is 199981
the 3 number is 199982
the 4 number is 199983
the 5 number is 199984
the 6 number is 199985
the 7 number is 199986
the 8 number is 199987
the 9 number is 199988
the 10 number is 199989
the 11 number is 199990
the 12 number is 200000
the 13 number is 200001
the 14 number is 1599981
the 15 number is 1599982
the 16 number is 1599983
the 17 number is 1599984
the 18 number is 1599985
the 19 number is 1599986
the 20 number is 1599987
the 21 number is 1599988
the 22 number is 1599989
the 23 number is 1599990
the 24 number is 2600000
the 25 number is 2600001
the 26 number is 13199998
the 27 number is 35000000
the 28 number is 35000001
the 29 number is 35199981
the 30 number is 35199982
the 31 number is 35199983
the 32 number is 35199984
the 33 number is 35199985
the 34 number is 35199986
the 35 number is 35199987
the 36 number is 35199988
the 37 number is 35199989
the 38 number is 35199990
the 39 number is 35200000
the 40 number is 35200001
the 41 number is 117463825
the 42 number is 500000000
the 43 number is 500000001
the 44 number is 500199981
the 45 number is 500199982
the 46 number is 500199983
the 47 number is 500199984
the 48 number is 500199985
the 49 number is 500199986
the 50 number is 500199987
the 51 number is 500199988
the 52 number is 500199989
the 53 number is 500199990
the 54 number is 500200000
the 55 number is 500200001
the 56 number is 501599981
the 57 number is 501599982
the 58 number is 501599983
the 59 number is 501599984
the 60 number is 501599985
the 61 number is 501599986
the 62 number is 501599987
the 63 number is 501599988
the 64 number is 501599989
the 65 number is 501599990
the 66 number is 502600000
the 67 number is 502600001
the 68 number is 513199998
the 69 number is 535000000
the 70 number is 535000001
the 71 number is 535199981
the 72 number is 535199982
the 73 number is 535199983
the 74 number is 535199984
the 75 number is 535199985
the 76 number is 535199986
the 77 number is 535199987
the 78 number is 535199988
the 79 number is 535199989
the 80 number is 535199990
the 81 number is 535200000
the 82 number is 535200001
the 83 number is 1111111110
the 84 number is 2971027783

[1]+  Stopped                 ./main

real    38m2.302s
user    0m0.000s
sys     0m0.000s

/*************************************/

cat /proc/cpuinfo

model name      : AMD Athlon(tm) 64 Processor 3000+
回复 支持 反对

使用道具 举报

发表于 2006-11-20 19:59:58 | 显示全部楼层
简单的组合数学问题,鉴定完毕

搭车问你们一个问题:
大家用过diff,也用过win下的comp吧,有没有人发现,对于同一个比较,diff返回的不同处总是不多于win的呢,比如对于如下两个文件A,B
A:     B:
1      4
2      1
3      2
        3
diff返回B文件第一行多一个4,而comp返回每行都不一样,
请问diff是用什么方法来比较的呢
回复 支持 反对

使用道具 举报

发表于 2006-11-20 20:13:41 | 显示全部楼层
这个问题我前两天也注意到了,不过还没有想到什么好办法能够实现diff的这一点;
回复 支持 反对

使用道具 举报

发表于 2006-11-20 20:21:42 | 显示全部楼层
那再仔细想想
回复 支持 反对

使用道具 举报

发表于 2006-11-20 23:56:46 | 显示全部楼层
这个程序还蛮有意思的,写了段代码,效率还不是很好,大家看看有什么改进的地方

#include <iostream>
using namespace std;

int main()
{
    int input;
    cin>>input;
    int *ff=new int[input+1];//用空间保存之前的结果
                             //ff保存的就是数值为i的结果
    int i=1;
    while(i<=input)
    {
       if(i<=9)
       {
          ff=1;
          }
        else
        {
           int temn=i;
           int m=10;
           int id=0;
           while(m<=temn)
              {
                if(temn%m==1)//个位数为1
                     {
                     id++;
                     temn=temn-1;
                     }
                 else
                     {
                     if(temn/m==1)//十位,百位,千位...为1
                          {
                              id++;
                          }  
                      m=m*10;  
                     }
               }
            ff=ff[i-1]+id;            
       }
       i++;
     }
     int result=ff[input];
     cout<<result<<endl;
}
回复 支持 反对

使用道具 举报

发表于 2006-11-22 17:46:45 | 显示全部楼层

  1. #include<stdio.h>
  2. #define __USE_ISOC99
  3. #include<stdlib.h>
  4. #include<string.h>
  5. #define MAXNL  11
  6. long long tenpower[MAXNL]= {1,10, 0};// 10^i
  7. long long  ftenpower[MAXNL]= { 0,1,0};// nnumone(10^i -1)

  8. long long stepnum[MAXNL]={0};
  9. int tens[MAXNL]={0};

  10. long long nnumone(int len)
  11. {
  12. int i;
  13. long long rt=0;
  14. for(i=len-1;i>=0;--i)
  15.   {
  16.    if(tens[i]>1) rt+=tenpower[i]+tens[i]*ftenpower[i];
  17.    else if(tens[i]==1)
  18.     rt+=ftenpower[i]+stepnum[i]+1;
  19.   }
  20. return rt;
  21. }

  22. int settens(long long n)
  23. {
  24. int i, j;
  25. lldiv_t qr;
  26. i=0;
  27. qr.quot=n;
  28. while(qr.quot)
  29.   {
  30.    qr=lldiv(qr.quot, 10L);
  31.    tens[i++]=qr.rem;
  32.   }

  33. for(j=1;j<i;++j)
  34.   {
  35.    stepnum[j]=stepnum[j-1]+tens[j-1]*tenpower[j-1];
  36.   }

  37. return i;
  38. }

  39. int main(int argc, char * argv[])
  40. {
  41. int  i,len=0;
  42. long long rt, n=1, c;

  43. for(i=1;i<MAXNL;++i)
  44.   {
  45.    tenpower[i]=tenpower[i-1]*10;
  46.    ftenpower[i]=tenpower[i-1]*i;
  47.   }

  48. do
  49.   {
  50.    len=settens(n);
  51.    rt=nnumone(len);
  52.    c=n-rt;
  53.    if(c>len)
  54.     {
  55.      n+=c/len+1;
  56.     }
  57.    else if(c<0)
  58.     {
  59.      n+=(-c);
  60.     }
  61.    else{
  62.     if(c==0)
  63.      printf("%lld\n",n);
  64.     ++n;
  65.    }
  66.   }while(len<MAXNL);

  67. return 0;
  68. }
复制代码


this is the result


  1. time ./a.out
  2. 1
  3. 199981
  4. 199982
  5. 199983
  6. 199984
  7. 199985
  8. 199986
  9. 199987
  10. 199988
  11. 199989
  12. 199990
  13. 200000
  14. 200001
  15. 1599981
  16. 1599982
  17. 1599983
  18. 1599984
  19. 1599985
  20. 1599986
  21. 1599987
  22. 1599988
  23. 1599989
  24. 1599990
  25. 2600000
  26. 2600001
  27. 13199998
  28. 35000000
  29. 35000001
  30. 35199981
  31. 35199982
  32. 35199983
  33. 35199984
  34. 35199985
  35. 35199986
  36. 35199987
  37. 35199988
  38. 35199989
  39. 35199990
  40. 35200000
  41. 35200001
  42. 117463825
  43. 500000000
  44. 500000001
  45. 500199981
  46. 500199982
  47. 500199983
  48. 500199984
  49. 500199985
  50. 500199986
  51. 500199987
  52. 500199988
  53. 500199989
  54. 500199990
  55. 500200000
  56. 500200001
  57. 501599981
  58. 501599982
  59. 501599983
  60. 501599984
  61. 501599985
  62. 501599986
  63. 501599987
  64. 501599988
  65. 501599989
  66. 501599990
  67. 502600000
  68. 502600001
  69. 513199998
  70. 535000000
  71. 535000001
  72. 535199981
  73. 535199982
  74. 535199983
  75. 535199984
  76. 535199985
  77. 535199986
  78. 535199987
  79. 535199988
  80. 535199989
  81. 535199990
  82. 535200000
  83. 535200001
  84. 1111111110

  85. real    0m0.006s
  86. user    0m0.008s
  87. sys     0m0.000s
复制代码
回复 支持 反对

使用道具 举报

发表于 2006-11-30 16:25:18 | 显示全部楼层
Post by Iambitious
不是,只是一个学生,而且水平比较烂,其实最近我看了一些大公司的面试的题目,发现很多都是考察算法的题目,其中尤以google,baidu,ms为最,虽然题目对于搞acm竞赛的选手不难,但是对于普通的只是简单的学过数据结构和算法分析的同学来说,在规定的时间内往往给不出正确和高效的算法,个人感觉很有必要去topcoder,pku做做题目,提高一下这方面的水平,否则面试或者笔试的时候比较劣势。


同意,个人感觉这些公司招人都是针对算法的,因此ACM不牛,数学不牛的朋友不要轻易去受鄙视
回复 支持 反对

使用道具 举报

发表于 2006-12-5 13:38:24 | 显示全部楼层
题目非常简单,关键是要优化速度
回复 支持 反对

使用道具 举报

发表于 2006-12-28 09:09:15 | 显示全部楼层
谁有ACM的题目,传一份给我看看,练习一下
lynux (a) 126.com
回复 支持 反对

使用道具 举报

发表于 2006-12-28 12:58:20 | 显示全部楼层
Post by dabasir
谁有ACM的题目,传一份给我看看,练习一下
lynux (a) 126.com


acm.pku.edu.cn
回复 支持 反对

使用道具 举报

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

本版积分规则

快速回复 返回顶部 返回列表