google的一道JAVA面试题!!!
2005-11-29 12:19
405 查看
java代码:
Consider a function which, for a given whole number n, returns the number of ones required when writing out all numbers between 0 and n.
For example, f(13)=6. Notice that f(1)=1. What is the next largest n such that f(n)=n?
翻译过来大体是这样:
有一个整数n,写一个函数f(n),返回0到n之间出现的"1"的个数。比如f(13)=6,现在f(1)=1,问下一个最大的f(n)=n的n是什么?
答案一:
int getCountOfNumber(int number){
int count=0;
int length=("" + number).length();
for(int i=0;i<=length;i++){
int num=number%10;
number=(number-num)/10;
if(num*num==1) count++;
}
return count;
}
计算到:199981 用了203
不过只计算到上边的数值就没多大意思,看看这个:
这个是4000000000以内的结果!:
f(0) = 0
f(1) = 1
f(199981) = 199981
f(199982) = 199982
f(199983) = 199983
f(199984) = 199984
f(199985) = 199985
f(199986) = 199986
f(199987) = 199987
f(199988) = 199988
f(199989) = 199989
f(199990) = 199990
f(200000) = 200000
f(200001) = 200001
f(1599981) = 1599981
f(1599982) = 1599982
f(1599983) = 1599983
f(1599984) = 1599984
f(1599985) = 1599985
f(1599986) = 1599986
f(1599987) = 1599987
f(1599988) = 1599988
f(1599989) = 1599989
f(1599990) = 1599990
f(2600000) = 2600000
f(2600001) = 2600001
f(13199998) = 13199998
f(35000000) = 35000000
f(35000001) = 35000001
f(35199981) = 35199981
f(35199982) = 35199982
f(35199983) = 35199983
f(35199984) = 35199984
f(35199985) = 35199985
f(35199986) = 35199986
f(35199987) = 35199987
f(35199988) = 35199988
f(35199989) = 35199989
f(35199990) = 35199990
f(35200000) = 35200000
f(35200001) = 35200001
f(117463825) = 117463825
f(500000000) = 500000000
f(500000001) = 500000001
f(500199981) = 500199981
f(500199982) = 500199982
f(500199983) = 500199983
f(500199984) = 500199984
f(500199985) = 500199985
f(500199986) = 500199986
f(500199987) = 500199987
f(500199988) = 500199988
f(500199989) = 500199989
f(500199990) = 500199990
f(500200000) = 500200000
f(500200001) = 500200001
f(501599981) = 501599981
f(501599982) = 501599982
f(501599983) = 501599983
f(501599984) = 501599984
f(501599985) = 501599985
f(501599986) = 501599986
f(501599987) = 501599987
f(501599988) = 501599988
f(501599989) = 501599989
f(501599990) = 501599990
f(502600000) = 502600000
f(502600001) = 502600001
f(513199998) = 513199998
f(535000000) = 535000000
f(535000001) = 535000001
f(535199981) = 535199981
f(535199982) = 535199982
f(535199983) = 535199983
f(535199984) = 535199984
f(535199985) = 535199985
f(535199986) = 535199986
f(535199987) = 535199987
f(535199988) = 535199988
f(535199989) = 535199989
f(535199990) = 535199990
f(535200000) = 535200000
f(535200001) = 535200001
f(1111111110) = 1111111110
有人用c写了一个,得出这些结果只用了几十毫秒!
有更好思路的MAIL IT TO ME。 thanks~:) safecn@gmail.com
Consider a function which, for a given whole number n, returns the number of ones required when writing out all numbers between 0 and n.
For example, f(13)=6. Notice that f(1)=1. What is the next largest n such that f(n)=n?
翻译过来大体是这样:
有一个整数n,写一个函数f(n),返回0到n之间出现的"1"的个数。比如f(13)=6,现在f(1)=1,问下一个最大的f(n)=n的n是什么?
答案一:
int getCountOfNumber(int number){
int count=0;
int length=("" + number).length();
for(int i=0;i<=length;i++){
int num=number%10;
number=(number-num)/10;
if(num*num==1) count++;
}
return count;
}
计算到:199981 用了203
不过只计算到上边的数值就没多大意思,看看这个:
这个是4000000000以内的结果!:
f(0) = 0
f(1) = 1
f(199981) = 199981
f(199982) = 199982
f(199983) = 199983
f(199984) = 199984
f(199985) = 199985
f(199986) = 199986
f(199987) = 199987
f(199988) = 199988
f(199989) = 199989
f(199990) = 199990
f(200000) = 200000
f(200001) = 200001
f(1599981) = 1599981
f(1599982) = 1599982
f(1599983) = 1599983
f(1599984) = 1599984
f(1599985) = 1599985
f(1599986) = 1599986
f(1599987) = 1599987
f(1599988) = 1599988
f(1599989) = 1599989
f(1599990) = 1599990
f(2600000) = 2600000
f(2600001) = 2600001
f(13199998) = 13199998
f(35000000) = 35000000
f(35000001) = 35000001
f(35199981) = 35199981
f(35199982) = 35199982
f(35199983) = 35199983
f(35199984) = 35199984
f(35199985) = 35199985
f(35199986) = 35199986
f(35199987) = 35199987
f(35199988) = 35199988
f(35199989) = 35199989
f(35199990) = 35199990
f(35200000) = 35200000
f(35200001) = 35200001
f(117463825) = 117463825
f(500000000) = 500000000
f(500000001) = 500000001
f(500199981) = 500199981
f(500199982) = 500199982
f(500199983) = 500199983
f(500199984) = 500199984
f(500199985) = 500199985
f(500199986) = 500199986
f(500199987) = 500199987
f(500199988) = 500199988
f(500199989) = 500199989
f(500199990) = 500199990
f(500200000) = 500200000
f(500200001) = 500200001
f(501599981) = 501599981
f(501599982) = 501599982
f(501599983) = 501599983
f(501599984) = 501599984
f(501599985) = 501599985
f(501599986) = 501599986
f(501599987) = 501599987
f(501599988) = 501599988
f(501599989) = 501599989
f(501599990) = 501599990
f(502600000) = 502600000
f(502600001) = 502600001
f(513199998) = 513199998
f(535000000) = 535000000
f(535000001) = 535000001
f(535199981) = 535199981
f(535199982) = 535199982
f(535199983) = 535199983
f(535199984) = 535199984
f(535199985) = 535199985
f(535199986) = 535199986
f(535199987) = 535199987
f(535199988) = 535199988
f(535199989) = 535199989
f(535199990) = 535199990
f(535200000) = 535200000
f(535200001) = 535200001
f(1111111110) = 1111111110
有人用c写了一个,得出这些结果只用了几十毫秒!
有更好思路的MAIL IT TO ME。 thanks~:) safecn@gmail.com
相关文章推荐
- google的一道JAVA面试题!!! (转)
- google的一道JAVA面试题!!!
- google的一道JAVA面试题
- 史上最难的一道Java面试题
- 关于java的一道面试题
- 由一道面试题所想到的--Java实例变量初始化
- 一道Google面试题——基数排序思想
- 关于一道java面试题的分析
- 以空间换时间--Google一道关于堆栈操作的面试题
- 公司要招人,我帮经理准备了一道面试题,考Java基础的
- 一道JAVA编程面试题
- 关于一道简单的Java 基础面试题的剖析: short s1=1;s1 = s1 +1会报错吗?
- 一道与java无关的java面试题,你能算清楚吗?
- 关于一道google面试题的java算法
- 一道Java线程面试题。3个线程顺序打印10次ABC.
- 解答Google的一道面试题
- 白话经典算法系列之十 一道有趣的GOOGLE面试题
- Google的一道面试题
- 每日一道算法题:Google面试题:判断一个自然数是否是某个数的平方
- 一道google面试题