不要被阶乘吓到

来源:互联网 发布:淘宝金融 编辑:程序博客网 时间:2024/04/27 16:31

这里写图片描述

这里写图片描述
这里写图片描述

实现代码:

public class numOf0 {    public static void main(String[] args) {        int n = 100;        System.out.println(countOf0(n));    }    // 解法1    private static int countOf0(int n) {        // TODO Auto-generated method stub        int result = 0;        for (int i = 1; i <= n; i++) {            int j = i;            while (j % 5 == 0) {                result++;                j /= 5;            }        }        return result;    }    // 解法2    private static int countOf01(int n) {        // TODO Auto-generated method stub        int result = 0;        while (n != 0) {            n = n / 5;             result = result + n/5;        }         return result;        }}

测试结果:
这里写图片描述

这里写图片描述

public class LowestOne {    public static void main(String[] args) {        // TODO Auto-generated method stub        int n = 4;        System.out.println(LowestOne(n));    }    private static int LowestOne(int n) {        // TODO Auto-generated method stub        int result = 0;        while (n != 0) {            n = n >> 1;            result = result + n;        }        return result + 1;    }}

测试结果:
·

0 0
原创粉丝点击