172
선택한 UI 언어에 맞게 문제 텍스트를 러시아어에서 번역합니다. 코드는 변경하지 않습니다.
. Factorial Trailing Zeroes
given 정수 n, return количество конечных нулей в n!.
Обратите внимание, что n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1.
예제:
Input: n = 3
Output: 0
Explanation: 3! = 6, no trailing zero.
C# 해법
매칭됨/원본class Solution {
public int TrailingZeroes(int n) {
BigInteger nFactorial = BigInteger.One;
for (int i = 2; i <= n; i++) {
nFactorial *= i;
}
int zeroCount = 0;
while (nFactorial % 10 == 0) {
nFactorial /= 10;
zeroCount++;
}
return zeroCount;
}
}
C++ 해법
자동 초안, 제출 전 검토#include <bits/stdc++.h>
using namespace std;
// Auto-generated C++ draft from the C# solution. Review containers, LINQ and helper types before submit.
class Solution {
public int TrailingZeroes(int n) {
BigInteger nFactorial = BigInteger.One;
for (int i = 2; i <= n; i++) {
nFactorial *= i;
}
int zeroCount = 0;
while (nFactorial % 10 == 0) {
nFactorial /= 10;
zeroCount++;
}
return zeroCount;
}
}
Java 해법
자동 초안, 제출 전 검토import java.util.*;
import java.math.*;
// Auto-generated Java draft from the C# solution. Review API differences before LeetCode submit.
class Solution {
public int TrailingZeroes(int n) {
BigInteger nFactorial = BigInteger.One;
for (int i = 2; i <= n; i++) {
nFactorial *= i;
}
int zeroCount = 0;
while (nFactorial % 10 == 0) {
nFactorial /= 10;
zeroCount++;
}
return zeroCount;
}
}
Algorithm
1️⃣
Вычислите факториал n:
Инициализируйте переменную nFactorial значением 1.
Для каждого i от 2 до n включительно умножайте nFactorial на i.
2️⃣
Подсчитайте количество конечных нулей в nFactorial:
Инициализируйте переменную zeroCount значением 0.
Пока nFactorial делится на 10 без остатка, делите его на 10 и увеличивайте zeroCount на 1.
3️⃣
return значение zeroCount как количество конечных нулей в n!.
😎
Vacancies for this task
활성 채용 with overlapping task tags are 표시됨.
아직 활성 채용이 없습니다.