A recent interview has encountered a pen question.
1, the number of the tail 0 of the factorial result of 1000.
Solution: Calculates the number of 5 that can be decomposed
The idea is that 1000! = 1000*999*...*1, and 5*2= 10, so long as all the number from 1 to 1000 decomposition into (2,5) combination, you can know the number of mantissa 0. At the same time, it is clear that the number of 2 is more than the number of 5, so only need to focus on the number of decomposition of 5.
PackageCom.tk.demos; Public classTest { Public Static voidMain (string[] args) {intc =NewTest (). Test (1000); System.out.println (c); } Public intTestintTotal ) { intCount = 0; for(inti = 5; I <= total; i++) { intTMP =i; while(tmp% 5 = = 0) {Count++; TMP= TMP/5; } } returncount; }}
Additional solutions found on the Web:
Statistical calculation by classification
There are:
625 in Multiples: 1 (625*16)
Multiples of 125:8-1 = 7 (minus 625)
Multiples of 25:40-8 = 32 (remove the 7+1 above)
Multiples of 5:200-40 = 160 (Remove the 32+7+1 above)
Total: 1*4+7*3+32*2+160=249 of 0
2, assuming that similar to the "ABA", "AAB" string equality, writing methods to judge any given two sets of strings equal.
PackageCom.tk.demos; Public classTest { Public Static voidMain (string[] args) {BooleanTest =NewTest (). Strequals ("AB", "ABA"); SYSTEM.OUT.PRINTLN (test); } Public Booleanstrequals (String strone, String strtwo) {if(Strone = =NULL&& Strtwo = =NULL) { return true; } if(Strone = =NULL|| Strtwo = =NULL) { return false; } if(Strone.length ()! =strtwo.length ()) { return false; } returnsort (strone). Equals (sort (strtwo)); } Publicstring Sort (string strin) {Char[] S1 =Strin.tochararray (); for(inti = 0; I < S1.length; i++) { for(intj = 0; J < I J + +) { if(S1[i] < S1[j]) { Chartemp =S1[i]; S1[i]=S1[j]; S1[J]=temp; } } } returnstring.valueof (S1); }}
3. If you use the return statement in a try statement block, will the finally statement block also execute?
Answer: Will
The answer to this question is very simple, just inadvertently read an article on the Internet, very interesting.
What is the output of the following code? the answer is: 2, not 3 .
classTest { Public intaaa () {intx = 1; Try { return++x; } Catch(Exception e) {}finally { ++x; } returnx; } Public Static voidMain (string[] args) {Test T=NewTest (); inty =t.aaa (); System.out.println (y); }}
Specific analysis can look at this article you really understand the return of try{return}finally{}?
Java face question