Test_Java_再帰的プラクティス(すべてのゼロと後続のゼロの数の1000階乗)



Test_java_ Recursive Practice 1000 Factorial Number All Zeros



需要:1000の階乗をゼロの数と後続のゼロすべてで見つけ、再帰的にしないでください

import java.math.BigInteger public class Test6 { /* * Demand: find the number of all zeros and trailing zeros factorial 1000, do not do recursive */ public static void main(String[] args) { / * Factorial since 1000, far beyond the range of int * int result = 1 for (int i = 1 i <1000 i++) { result = result * i } System.out.println(result) //0 */ // the demo1 () // factorial seeking all 1000 0 // demo2 () // get the factorial tail of 1000 the number of 0 } // Get the factorial of the number of tail 1000 0 public static void demo2() { BigInteger bi1 = new BigInteger('1') for (int i = 1 i <=1000 i++) { BigInteger bi2 = new BigInteger(i+'') bi1 = bi1.multiply(bi2) // will bi1 with bi2 multiplied assign the result to bi1 } String str = bi1.toString() // Get string representation StringBuilder sb = new StringBuilder(str) str = sb.reverse().toString() // chain Programming int count = 0 // define the counter for(int i=0 i<str.length()i++){ if('0'!= str.charAt(i)){ break }else{ count++ } } System.out.println(count) } // find all of 1000 factorial 0 public static void demo1() { BigInteger bi1 = new BigInteger('1') for (int i = 1 i <=1000 i++) { BigInteger bi2 = new BigInteger(i+'') bi1 = bi1.multiply(bi2) // will bi1 with bi2 multiplied assign the result to bi1 } String str = bi1.toString() // Get string representation int count=0 for (int i = 0 i <str.length() i++) { if('0'==str.charAt(i)){ // If there has been a string of characters 0 count++ // counter +1 } } System.out.println(count) //472 } }