有一个整数n,写一个函数f(n),返回0到n之间出现的"1"的个数。比如f(13)=6,现在f(1)=1,问有哪些n能满足f(n)=n?
例如:f(13)=6, 因为1,2,3,4,5,6,7,8,9,10,11,12,13.数数1的个数,正好是6.
试题编号134 由 ╱/罒呍唲 于2008-10-09 上传 查看本题答案
参考答案
用的是剪枝:
1. #include "stdafx.h"
2.
3. #include <windows.h>
4. #include <stdlib.h>
5.
6. int f(int n);
7. int count1(int n);
8. int cal(unsigned int number,int nwei,int count1,unsigned int ncount);
9.
10. int gTable[10];
11. const unsigned int gMAX = 4000000000L;
12.
13. int main(int argc, char* argv[])
14. {
15. int i;
16. unsigned int n=1;
17. unsigned int ncount = 0;
18. int nwei = 0;
19. int ncount1;
20.
21. /*if(argc>1)
22. {
23. n = atoi(argv[1]);
24. ncount = f(n);
25. printf("f(%d) = %d\n",n,ncount);
26. }*/
27.
28. int beginTime=GetTickCount();
29. //init gTable
30. for(i=0;i<10;++i)
31. {
32. n *= 10;
33. gTable[i] = f(n-1);
34. }
35.
36. n=0;
37. nwei = 0;
38. ncount1 = 0;
39. while(n<gMAX)
40. {
41. unsigned int temp;
42.
43. temp = 1;
44.
45. ncount =cal(n,nwei,ncount1,ncount);
46. for(i=0;i<nwei;++i)
47. temp *= 10;
48. n += temp;
49. if( (n/temp)/10 == 1)
50. ++nwei;
51. ncount1 = count1(n);
52. }
53.
54. int endTime=GetTickCount();
55. endTime-=beginTime;
56.
57. printf("time: %d ms\n",endTime);
58. return 0;
59. }
60.
61.
62. int f(int n)
63. {
64. int ret = 0;
65. int ntemp=n;
66. int ntemp2=1;
67. int i=1;
68. while(ntemp)
69. {
70. ret += (((ntemp-1)/10)+1) * i;
71. if( (ntemp%10) == 1 )
72. {
73. ret -= i;
74. ret += ntemp2;
75. }
76. ntemp = ntemp/10;
77. i*=10;
78. ntemp2 = n%i+1;
79. }
80. return ret;
81. }
82.
83. int count1(int n)
84. {
85. int count = 0;
86. while(n)
87. {
88. if( (n%10) == 1)
89. ++count;
90. n /= 10;
91. }
92. return count;
93. }
94.
95. int cal(unsigned int number,int nwei,int count1,unsigned int ncount)
96. {
97. int i,n=1;
98. unsigned int maxcount;
99. if(nwei==0)
100. {
101. ncount += count1;
102. if(number == ncount)
103. {
104. printf("f(%d) = %d \n",number,number);
105. }
106. return ncount;
107. }
108. for(i=0;i<nwei;++i)
109. n *= 10;
110. maxcount = ncount + gTable[nwei-1];
111. maxcount += count1*n;
112. if(ncount > (number + (n-1)) )
113. {
114. return maxcount;
115. }
116. if(maxcount < number)
117. {
118. return maxcount;
119. }
120. n /= 10;
121. for(i=0;i<10;++i)
122. {
123. if(i==1)
124. ncount = cal(number+i*n,nwei-1,count1+1,ncount);
125. else
126. ncount = cal(number+i*n,nwei-1,count1,ncount);
127. }
128. return ncount;
129. }
来自:pupi 一道java面试题
碰到一个java题目,要求1000!(1000*999*998...*2*1)的值。
试题编号12 由 ╱/罒呍唲 于2008-09-23 上传 查看本题答案
参考答案
BigDecimal big = new BigDecimal(1);
for(int i=1; i<1001; i++) {
big = big.multiply(new BigDecimal(i));
}
big.setScale(100);
System.out.println(big);
来自:SpringArt 一著名软件公司的java笔试算法题!
原题如下:用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列,如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连.
解决思路:强化题目,用1、2、2、3、4、5这六个数字排列“递增”序列。其他要求不变。
试题编号11 由 ╱/罒呍唲 于2008-09-23 上传 查看本题答案
参考答案
import java.util.Iterator;
import java.util.TreeSet;
public class TestQuestion {
private String[] b = new String[]{"1", "2", "2", "3", "4", "5"};
private int n = b.length;
private boolean[] visited = new boolean[n];
private int[][] a = new int[n][n];
private String result = "";
private TreeSet set = new TreeSet();
public static void main(String[] args) {
new TestQuestion().start();
}
private void start() {
// Initial the map a[][]
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j) {
a[i][j] = 0;
} else {
a[i][j] = 1;
}
}
}
// 3 and 5 can not be the neighbor.
a[3][5] = 0;
a[5][3] = 0;
// Begin to depth search.
for (int i = 0; i < n; i++) {
this.depthFirstSearch(i);
}
// Print result treeset.
Iterator it = set.iterator();
while (it.hasNext()) {
String string = (String) it.next();
// "4" can not be the third position.
if (string.indexOf("4") != 2) {
System.out.println(string);
}
}
}
private void depthFirstSearch(int startIndex) {
visited[startIndex] = true;
result = result + b[startIndex];
if (result.length() == n) {
// Filt the duplicate value.
set.add(result);
}
for(int j = 0; j < n; j++) {
if (a[startIndex][j] == 1 && visited[j] == false) {
depthFirstSearch(j);
} else {
continue;
}
}
// restore the result value and visited value after listing a node.
result = result.substring(0, result.length() -1);
visited[startIndex] = false;
}
}
* 3,5不能相连:实际要求这个连通图的结点3,5之间不能连通, 可在构造图结构时就满足改条件,然后再遍历图。
代码中请注意这几行:
// 3 and 5 can not be the neighbor.
a[3][5] = 0;
a[5][3] = 0;
Anonymous Inner Class (匿名内部类) 是否可以extends(继承)其它类,是否可以implements(实现)interface(接口)?
试题编号8 由 guiliangzhongbo 于2008-07-22 上传 查看本题答案
参考答案
匿名的内部类是没有名字的内部类。不能extends(继承) 其它类,但一个内部类可以作为一个接口,由另一个内部类实现。
谈谈final, finally, finalize的区别
试题编号7 由 guiliangzhongbo 于2008-07-22 上传 查看本题答案
参考答案
final—修饰符(关键字)如果一个类被声明为final,意味着它不能再派生出新的子类,不能作为父类被继承。因此一个类不能既被声明为 abstract的,又被声明为final的。将变量或方法声明为final,可以保证它们在使用中不被改变。被声明为final的变量必须在声明时给定初值,而在以后的引用中只能读取,不可修改。被声明为final的方法也同样只能使用,不能重载。
finally—再异常处理时提供 finally 块来执行任何清除操作。如果抛出一个异常,那么相匹配的 catch 子句就会执行,然后控制就会进入 finally 块(如果有的话)。
finalize—方法名。Java 技术允许使用 finalize() 方法在垃圾收集器将对象从内存中清除出去之前做必要的清理工作。这个方法是由垃圾收集器在确定这个对象没有被引用时对这个对象调用的。它是在 Object 类中定义的,因此所有的类都继承了它。子类覆盖 finalize() 方法以整理系统资源或者执行其他清理工作。finalize() 方法是在垃圾收集器删除对象之前对这个对象调用的。
Error与Exception有什么区别?
试题编号6 由 guiliangzhongbo 于2008-07-22 上传 查看本题答案
参考答案
Error表示系统级的错误和程序不必处理的异常,
Exception表示需要捕捉或者需要程序进行处理的异常。
4棵树,怎么栽让他们两棵树之间的距离相等
试题编号5 由 liang.zeng 于2008-07-02 上传 查看本题答案
参考答案
4个等边三角形
Collection 和 Collections的区别?
试题编号4 由 duyange 于2008-05-15 上传 查看本题答案
参考答案
Collection是集合类的上级接口,继承与他的接口主要有Set 和List.
Collections是针对集合类的一个帮助类,他提供一系列静态方法实现对各种集合的搜索、排序、线程安全化等操作。
abstract的method是否可同时是static,是否可同时是native,是否可同时是synchronized?
试题编号3 由 duyange 于2008-05-15 上传 查看本题答案
参考答案
都不能
Overload和Override的区别?
试题编号2 由 duyange 于2008-05-15 上传 查看本题答案
参考答案
方法的重写Overriding和重载Overloading是Java多态性的不同表现。重写Overriding是父类与子类之间多态性的一种表现,重载Overloading是一个类中多态性的一种表现。如果在子类中定义某方法与其父类有相同的名称和参数,我们说该方法被重写 (Overriding)。子类的对象使用这个方法时,将调用子类中的定义,对它而言,父类中的定义如同被"屏蔽"了。如果在一个类中定义了多个同名的方法,它们或有不同的参数个数或有不同的参数类型,则称为方法的重载(Overloading)。Overloaded的方法是可以改变返回值的类型
评论 共 1 条 (考虑到评论可能会影响到您的独立思考, 我们隐藏了所有评论 点击显示)
yuanqixun 2008-10-01 16:56