對於一個數n的階乘 n! ,計算其後面有幾個連續的零。
- static int zeroCount ( int n) {
- int counter = 0;
- for( int i = 5,m; i <= n; i += 5) {
- m = i;
- while ( m % 5 == 0) {
- counter++;
- m /= 5;
- }
- }
- return counter;
- }
- static int zeroCount ( int n) {
- int counter = 0;
- while ( n >= 5) {
- n /= 5;
- counter += n;
- }
- return counter;
- }