Skip to content

Commit fd3449e

Browse files
committed
SPOJ New factorial Program
1 parent d631e54 commit fd3449e

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed
File renamed without changes.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
The most important part of a GSM network is so called Base Transceiver Station (BTS). These transceivers form the areas called cells (this term gave the name to the cellular phone) and every phone connects to the BTS with the strongest signal (in a little simplified view). Of course, BTSes need some attention and technicians need to check their function periodically.
3+
4+
ACM technicians faced a very interesting problem recently. Given a set of BTSes to visit, they needed to find the shortest path to visit all of the given points and return back to the central company building. Programmers have spent several months studying this problem but with no results. They were unable to find the solution fast enough. After a long time, one of the programmers found this problem in a conference article. Unfortunately, he found that the problem is so called "Travelling Salesman Problem" and it is very hard to solve. If we have N BTSes to be visited, we can visit them in any order, giving us N! possibilities to examine. The function expressing that number is called factorial and can be computed as a product 1.2.3.4....N. The number is very high even for a relatively small N.
5+
6+
The programmers understood they had no chance to solve the problem. But because they have already received the research grant from the government, they needed to continue with their studies and produce at least some results. So they started to study behaviour of the factorial function.
7+
8+
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.
9+
10+
Input
11+
12+
There is a single positive integer T on the first line of input (equal to about 100000). It stands for the number of numbers to follow. Then there are T lines, each containing exactly one positive integer number N, 1 <= N <= 1000000000.
13+
14+
Output
15+
16+
For every number N, output a single line containing the single non-negative integer Z(N).
17+
Example
18+
19+
Sample Input:
20+
21+
6
22+
3
23+
60
24+
100
25+
1024
26+
23456
27+
8735373
28+
29+
Sample Output:
30+
31+
0
32+
14
33+
24
34+
253
35+
5861
36+
2183837
37+
38+
ALGORITHM :
39+
40+
1. Divide input by 5 and add to result (20/5) Add 4
41+
2. Continue until data goes below 5
42+
43+
44+
*/
45+
46+
# include <iostream>
47+
48+
using namespace std;
49+
50+
51+
void factorial(int input)
52+
{
53+
int count = 0;
54+
while( input >=5)
55+
{
56+
count += (input/5);
57+
input /=5;
58+
}
59+
cout<<count<<endl;
60+
return;
61+
62+
}
63+
64+
int main()
65+
{
66+
int size;
67+
int input;
68+
cin>>size;
69+
for(int i=0; i<size;i++)
70+
{
71+
cin>>input;
72+
factorial(input);
73+
}
74+
}
75+
76+

0 commit comments

Comments
 (0)