【算法】程序猿不写代码是不对的76

来源:互联网 发布:js调用mvc方法 编辑:程序博客网 时间:2024/05/29 18:02
package com.kingdz.algorithm.time201707;/** * <pre> * 阶乘最后一个非零位 *  * http://judgecode.com/problems/1008 *  * Given a non-negative integer n, calculate the last non-zero digit of n's factorial, namely n!. 0! = 1! = 1, 2!= 2, 3!= 6…etc. *  * Input:A non-negative integer n no larger than 1000000. *  * </pre> *  * @author kingdz *  */public class Algo03 {    public static void main(String[] args) {        int n = 1000000;        int result = calculateLast(n);        System.out.println("the last is:" + result);    }    private static int calculateLast(int n) {        if (n == 0 || n == 1) {            return 1;        }        int result = 1;        for (int i = 1; i <= n; i++) {            result = result * i;            while (("" + result).endsWith("0")) {                result = result / 10;            }            result = result % 1000;        }        return result % 10;    }}

原创粉丝点击