2013 ACM-ICPC Malaysia National Programming Contest (QUESTION A-TRAILING SIFAR)

A
TRAILING SIFAR

Problem Description

Sifar is a malay word for zero or 0. In mathematics, trailing sifar is a sequence of 0s in the
decimal representation of a number after which no other digits follow.
The number of trailing sifar in the decimal representation of N! (5 <= N <= 1,000,000) is simply
the multiplicity of the prime factor 5 in N!. Given a decimal integer N, you are to find the
number of trailing sifar for N! For example, 10! = 3,628,800. Thus, the number of trailing sifar
for 10! is 2.

Input

Each line of input contains an integer N where 5 <= N <= 1,000,000. The input is terminated
with a line containing 0.

Output

For each test case, the output contains a line in the format Case #x: M, where x is the case
number (starting from 1) and M is the number of trailing sifar for N!
Example of sample I/O

Sample Input
5
10
118

Sample Output
Case #1: 1
Case #2: 2

Case #3: 27

Cara nak selesaikan soalan ini adalah dengan bahagikan nilai yang diberi kepada 5. Bakinya kita tambah sehinggalah baki yang terakhir no dibahagi itu kurang daripada 5.

Solution :
import java.util.*;
public class acmA {
public static void main(String [] args) {
Scanner s = new Scanner(System.in);
int num = s.nextInt();
int x=0;
while( num != 0) {
int t = num%5;
int d = (num-t)/5;
t =d;
int tot = d;
while(t >= 5) {
t = t%5;
d = (d-t)/5;
t=d;
tot = tot + d;
}
x++;
System.out.println("Case #"+x +": "+tot);
num = s.nextInt();
}
}
}

Share this

Related Posts

Previous
Next Post »