牛客 NC129 阶乘末尾0的数量【简单 基础数学 Java/Go/PHP/C++】

作者 : admin 本文共1077个字,预计阅读时间需要3分钟 发布时间: 2024-06-10 共3人阅读

题目

牛客 NC129 阶乘末尾0的数量【简单 基础数学 Java/Go/PHP/C++】插图
题目链接:
http://www.nowcoder.com/practice/aa03dff18376454c9d2e359163bf44b8
http://www.lintcode.com/problem/2

思路

牛客 NC129 阶乘末尾0的数量【简单 基础数学 Java/Go/PHP/C++】插图(1)

Java代码

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * the number of 0
     * @param n long长整型 the number
     * @return long长整型
     */
    public long thenumberof0 (long n) {
        //就是可以转换成求有多少个5,然后求每个5的倍数的数中有多少个5.最后将所有5的个数加一起
        long ans =0;
        while(n > 0){
            n =n/5;
            ans+=n;
        }
        return ans;
    }
}

Go代码

package main

/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * the number of 0
 * @param n long长整型 the number
 * @return long长整型
 */
func thenumberof0(n int64) int64 {
	//就是可以转换成求有多少个5,然后求每个5的倍数的数中有多少个5.最后将所有5的个数加一起
	var ans int64= 0
	for n > 0 {
		n = n / 5
		ans += n
	}
	return ans
}

PHP代码

<?php


/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * the number of 0
 * @param n long长整型 the number
 * @return long长整型
 */
function thenumberof0( $n )
{
    //就是可以转换成求有多少个5,然后求每个5的倍数的数中有多少个5.最后将所有5的个数加一起

    $ans =0;
    while($n >0){
        $n = intval($n/5);
        $ans+=$n;
    }
    return $ans;
}

C++代码

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * the number of 0
     * @param n long长整型 the number
     * @return long长整型
     */
    long long thenumberof0(long long n) {
        //就是可以转换成求有多少个5,然后求每个5的倍数的数中有多少个5.最后将所有5的个数加一起
        long ans =0;

        while(n >0){
            n=n/5;
            ans+=n;
        }
        return ans;
    }
};
本站无任何商业行为
个人在线分享 » 牛客 NC129 阶乘末尾0的数量【简单 基础数学 Java/Go/PHP/C++】
E-->