阶乘 hdu 1124 (Factorial)
2016-08-23 19:55
447 查看
Factorial
Problem Description
For example, they defined the function Z. For any positive integer N, Z(N) is the number of zeros at the end of the decimal form of number N!. They noticed that this function never decreases. If we have two numbers N1<N2, then Z(N1)
<= Z(N2). It is because we can never "lose" any trailing zero by multiplying by any positive number. We can only get new and new zeros. The function Z is very interesting, so we need a computer program that can determine its value efficiently.
[align=left]Input[/align]
There is a single positive integer T on the first line of input. It stands for the number of numbers to follow. Then there is T lines, each containing exactly one positive integer number N, 1 <= N <= 1000000000.
[align=left]Output[/align]
For every number N, output a single line containing the single non-negative integer Z(N).
[align=left]Sample Input[/align]
6
3
60
100
1024
23456
8735373
[align=left]Sample Output[/align]
0
14
24
253
5861
2183837
题意:给出n,求n的阶乘的末尾有多少0.
题解:由算数基本定理可:N!可划分为 质因数相乘的形式 N!=2^a*3^b*5^c*7^d........
因为只有2*5 才会出现 0 又因为2的数量肯定比5的多 所以计算阶乘中5的数量就可以得到该阶乘后有几个0。
50/5=10 10/5=2 所以50!后有10+2=12个0。
#include<cstdio>
#include<cmath>
using namespace std;
int cnt;
int solve(int n){
cnt=0;
while (n){
cnt+=n/5;
n/=5;
}
return cnt;
}
int main(){
int n, t;
scanf ("%d",&t);
while (t--){
scanf ("%d",&n);
solve(n);
printf ("%d\n",cnt);
}
return 0;
}
Problem Description
For example, they defined the function Z. For any positive integer N, Z(N) is the number of zeros at the end of the decimal form of number N!. They noticed that this function never decreases. If we have two numbers N1<N2, then Z(N1)
<= Z(N2). It is because we can never "lose" any trailing zero by multiplying by any positive number. We can only get new and new zeros. The function Z is very interesting, so we need a computer program that can determine its value efficiently.
[align=left]Input[/align]
There is a single positive integer T on the first line of input. It stands for the number of numbers to follow. Then there is T lines, each containing exactly one positive integer number N, 1 <= N <= 1000000000.
[align=left]Output[/align]
For every number N, output a single line containing the single non-negative integer Z(N).
[align=left]Sample Input[/align]
6
3
60
100
1024
23456
8735373
[align=left]Sample Output[/align]
0
14
24
253
5861
2183837
题意:给出n,求n的阶乘的末尾有多少0.
题解:由算数基本定理可:N!可划分为 质因数相乘的形式 N!=2^a*3^b*5^c*7^d........
因为只有2*5 才会出现 0 又因为2的数量肯定比5的多 所以计算阶乘中5的数量就可以得到该阶乘后有几个0。
50/5=10 10/5=2 所以50!后有10+2=12个0。
#include<cstdio>
#include<cmath>
using namespace std;
int cnt;
int solve(int n){
cnt=0;
while (n){
cnt+=n/5;
n/=5;
}
return cnt;
}
int main(){
int n, t;
scanf ("%d",&t);
while (t--){
scanf ("%d",&n);
solve(n);
printf ("%d\n",cnt);
}
return 0;
}
相关文章推荐
- HDU 1124 Factorial&&nyoj 84 阶乘的 0【数学】
- hdu 1124 Factorial 数论,就是求一个数的阶乘的结果末尾有多少0.
- HDU 1124 Factorial (阶乘后缀0)
- hdu 1124 Factorial(数论)
- HDU HDOJ 1124 Factorial
- HDU - 1124 Factorial
- HDU 1124 Factorial
- HDU 1124 Factorial(数论)
- HDU 1124 Factorial (数论)
- HDU 1124 Factorial(简单数论)
- hdu 1124 Factorial
- HDU:1124 Factorial
- hdu 1124 Factorial
- HDU 1124 Factorial
- HDU 1124 Factorial
- 【HDU】 1124 Factorial
- HDU 1124 Factorial(数论)
- hdu 1124 Factorial(高斯函数定理)
- hdu 1124 Factorial(末尾0)
- ACM 数论 hdu 1124 Factorial