[HDU] [1124] Factorial

Problem Description

Loading...

Problem Analysis

A small formula I got from Yu Shen
Find how many zeros are at the end of N!

Core code:

cpp
1
2
3
4
5
int cnt=0;
		while(n) {
			cnt+=n/5;
			n/=5;
		}

I also picked a problem on HDU
…By the way, the problem statement is quite complicated
But the main idea is very simple
No pitfalls

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include<stdio.h>
int main() {
	int T;
	scanf("%d",&T);
	while(T--) {
		int n;
		scanf("%d",&n);
		int cnt=0;
		while(n) {
			cnt+=n/5;
			n/=5;
		}
		printf("%d\n",cnt);
	}
	return 0;
}

Problem link:HDU 1124 Factorial

This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...