统计2和5的因子个数,取较小值即可:
import java.util.*; public class Main { public static void main(String[] args) throws Exception{ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int two = 0, five = 0; for(int i = 2; i <= n; ++i){ two += f(i, 2); five += f(i, 5); } System.out.println(Math.min(two, five)); //2和5的个数取较小值 } static int f(int a, int b){ //统计a含几个b因子 int r = 0; while(a%b == 0){ r++; a = a/b; } return r; } }