LinuxSir.cn,穿越时空的Linuxsir!

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

[算法]Google的面试题

[复制链接]
发表于 2006-12-29 18:00:23 | 显示全部楼层

  1. #include <stdio.h>
  2. int main()
  3. {       
  4.         long f(long m);
  5.         long i=1,y=1;
  6.         do
  7.         {
  8.                 if (i==f(i)) printf("the %ld number is:%ld\n",y++,i);
  9.                 i++;
  10.         }
  11.         while(1);
  12.         return 0;
  13. }

  14. long f(long m)
  15. {
  16.         long s,sum,m_c,count;
  17.         sum=0;
  18.         s=0;
  19.         count=1;
  20.         do
  21.         {       
  22.                 m_c=m%10;
  23.                 if(m_c==0)sum=sum+(m/10)*count;
  24.                 else if(m_c==1)sum=sum+(m/10)*count+s+1;
  25.                 else sum=sum+((m/10)+1)*count;
  26.                 s=s+m_c*count;
  27.                 count=count*10;
  28.                 m=m/10;
  29.         }
  30.         while(m);
  31.         return sum;
  32. }

复制代码


初学 我也写了一个 怎么看运行时间?
回复 支持 反对

使用道具 举报

发表于 2006-12-29 20:51:48 | 显示全部楼层
POINTER的程序看不太明白?F(N)在哪里?
怎么这么快? 我机器运行个HELLO WORLD都要0.01+
回复 支持 反对

使用道具 举报

发表于 2006-12-30 09:34:30 | 显示全部楼层
  1. #include <stdio.h>
  2. int main()
  3. {       
  4.         long f(long m);
  5.         long i=1,y=1,k;
  6.         do
  7.         {
  8.                 k=f(i)-i;
  9.                 if (k==0) printf("the %ld number is:%ld\n",y++,i++);
  10.                 else if (k>0) i=i+k;
  11.                 else i=i-k;
  12.         }
  13.         while(i<1111111111);
  14.         return 0;
  15. }
  16. long f(long m)
  17. {
  18.         long s,sum,m_c,count;
  19.         sum=0;s=0;count=1;
  20.         do
  21.         {       
  22.                 m_c=m%10;
  23.                 if(m_c==0)sum=sum+(m/10)*count;
  24.                 else if(m_c==1)sum=sum+(m/10)*count+s+1;
  25.                 else sum=sum+((m/10)+1)*count;
  26.                 s=s+m_c*count;
  27.                 count=count*10;
  28.                 m=m/10;
  29.         }
  30.         while(m);
  31.         return sum;
  32. }
复制代码

简单改了一下 递进方式 快是快了 不过得出的结果却漏了1,2个数  等高人讲讲

估计是递进的方法有问题 增加速度也是在这里如果直接I++要13M

Piii500 w2k cygwin

$ time ./a.exe
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:35000000
the 27 number is:35000001
the 28 number is:35199981
the 29 number is:35199982
the 30 number is:35199983
the 31 number is:35199984
the 32 number is:35199985
the 33 number is:35199986
the 34 number is:35199987
the 35 number is:35199988
the 36 number is:35199989
the 37 number is:35199990
the 38 number is:35200000
the 39 number is:35200001
the 40 number is:500000000
the 41 number is:500000001
the 42 number is:500199981
the 43 number is:500199982
the 44 number is:500199983
the 45 number is:500199984
the 46 number is:500199985
the 47 number is:500199986
the 48 number is:500199987
the 49 number is:500199988
the 50 number is:500199989
the 51 number is:500199990
the 52 number is:500200000
the 53 number is:500200001
the 54 number is:501599981
the 55 number is:501599982
the 56 number is:501599983
the 57 number is:501599984
the 58 number is:501599985
the 59 number is:501599986
the 60 number is:501599987
the 61 number is:501599988
the 62 number is:501599989
the 63 number is:501599990
the 64 number is:502600000
the 65 number is:502600001
the 66 number is:535000000
the 67 number is:535000001
the 68 number is:535199981
the 69 number is:535199982
the 70 number is:535199983
the 71 number is:535199984
the 72 number is:535199985
the 73 number is:535199986
the 74 number is:535199987
the 75 number is:535199988
the 76 number is:535199989
the 77 number is:535199990
the 78 number is:535200000
the 79 number is:535200001

real    0m0.161s
user    0m0.070s
sys     0m0.080s
回复 支持 反对

使用道具 举报

发表于 2007-1-22 10:39:43 | 显示全部楼层
各位说的话,我都看不懂,唉~~,自卑~~
回复 支持 反对

使用道具 举报

发表于 2007-2-15 18:35:22 | 显示全部楼层
Number N
0+1+...+log(N)=N
N=?
回复 支持 反对

使用道具 举报

发表于 2007-2-17 12:21:26 | 显示全部楼层
不好意思地说一下,高中信息学竞赛和大学的acm/icpc经常做这种题……
回复 支持 反对

使用道具 举报

发表于 2007-2-27 13:12:05 | 显示全部楼层
为什么f(13)=6?
1,2,3,4,5,6,7,8,9,10,11,12,13
明明只有5个1...
回复 支持 反对

使用道具 举报

发表于 2007-2-27 16:19:09 | 显示全部楼层
Post by easycat
为什么f(13)=6?
1,2,3,4,5,6,7,8,9,10,11,12,13
明明只有5个1...

你再数数吧:beat
回复 支持 反对

使用道具 举报

发表于 2007-2-27 21:50:59 | 显示全部楼层
hoho 11 是算2 啊
我还以为1 呢 ..
回复 支持 反对

使用道具 举报

发表于 2007-3-4 16:31:51 | 显示全部楼层
Post by dabasir
谁有ACM的题目,传一份给我看看,练习一下
lynux (a) 126.com

acm的oj太多了,比如:
http://acm.pku.edu.cn/
http://acm.uva.es/problemset/
http://acm.zju.edu.cn/
http://acm.timus.ru/

我个人喜欢做后两个
回复 支持 反对

使用道具 举报

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

本版积分规则

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