zoukankan      html  css  js  c++  java
  • N!结果中末尾0的个数 2.2

    主要是看N!结果中2的个数和5的个数,多的那个个数即是末尾0的个数

       

       

    计算Z有两种方法

       

    一种是对每个n都去看有多少个5的因子

       

    一种是隔5个增加一个5的因子,隔25个再在之前的基础上增加一个5的因子

       

    两种方法差不多

       

    第二种,循环少

       

    package numOfZeroFactorialN_2_2;

       

    public class NumOfZeroFactorialN_2_2 {

       

    static int numOfZeros(int n) {

    int result = 0;

    for (int i = 1; i <= n; i++) {

    int j = i;

    while (j % 5 == 0) {

    result++;

    j /= 5;

    }

    }

    return result;

       

    }

       

    static int sol2(int n) {

    int result = 0;

    while (n != 0) {

    result += n / 5;

    n = n / 5;

    }

       

    return result;

       

    }

       

    public static void main(String[] args) {

    System.out.println(sol2(10));

    }

       

    }

  • 相关阅读:
    两种方法生成随机字符串
    cmd命令总结
    NOI前乱写
    多校模拟9
    字符串 口胡
    HEOI2020游记
    省选模拟104
    省选模拟103
    省选模拟102
    省选模拟101
  • 原文地址:https://www.cnblogs.com/keedor/p/4385482.html
Copyright © 2011-2022 走看看