Cho một số nguyên n. Hãy đếm xem trong kết quả của số n! (n giai thừa) có bao nhiêu chữ số 0 liên tiếp tính từ hàng đơn vị (hay bao nhiêu số 0 liên tiếp bên phải).
INPUT: Là số nguyên n (1 ≤ n ≤ 1.000)
OUTPUT: Là số lượng chữ số 0 liên tiếp tính từ hàng đơn vị của n!.
Ví dụ:
INPUT | OUTPUT |
4 | 0 |
8 | 1 |
20 | 4 |
Code trong C++
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,i,d; freopen("bzero.inp","r",stdin); freopen("bzero.out","w",stdout); cin >>n; d=0; for (i=1;i<=n;i++) if (i%5==0) { int m=i; while (m%5==0) { d++; m=m/5; } } cout <<d; return 0; }
Code trong Pascal
var n,sl:longint; f1,f2:text; procedure motep; begin assign(f1,'BZERO.INP'); reset(f1); assign(f2,'BZERO.OUT'); rewrite(f2); end; procedure dongtep; begin close(f1); close(f2); end; begin motep; readln(f1,n); sl:=0; while n<>0 do begin sl:=sl+n div 5; n:=n div 5; end; write(f2,sl); dongtep; end.