Could someone please tell me what is wrong with this code.
Basic idea behind my logic is i calculate the factorial by multiplying all numbers from 1 to N(N being the number itself) and at each juncture i check the condition whether the factorial at that particular instant is divisible by 10 leaving a remainder of 0 and if it is i am calculalating the sum at the end.
here goes the code.
int main()
{
int a,b,c,fact=1,sum=0,e=0;
cin>>a;//for taking in the total number of input
int f=0;
for(b=0;b<a;b++)
{
cin>>c;//selecting each input value
@dark_pharoah Your logic is correct but it takes more time, quite cumbersome and is prone to mistakes. The logic is 0βs are formed by combination of 5βs and 2βs(since 5x2=10) Agree? For example 30=2x3x5(0 is formed by 5 and 2). So, we have to find min(number of 2βs,number of 5βs) in n! so that they can combine to form 0βs. That can be found by:-
number of 2βs->[n/2]+[n/2^2]+β¦ until it is 0
number of 5βs->[n/5]+[n/5^2]+β¦ until it is 0, Here [] is floor
The reason for this is explained clearly in the above editorial. Have a look at it.
We can observe that number of 5βs will be less as itβs a big number(so just calculate number of 5βs)
int mmofive( long int t)
{ int n=0, pof=5;
while ( t/pof>0)
{pof*=5; n++;}
return n-1;
}//max divisible multiple of 5 (power)
int main()
{
long int n,t; int i,j;
int trailingzero=0;
cin>>n;
const long int f=n;
int A[f];
for ( i=0; i<n; i++)
{cin>>t;
for(j=5;j<=5^mmofive(t);j*=5)
{trailingzero+=t/j;}
A[i]=trailingzero;
trialingzero=0;
}
for (long int i=0; i<n; i++)
{cout<<A[i]<<endl;}
return 0;
}
WHY NOT WORKING? :((
i devised the same logic as expected.
but it says it exceeds time limit.
i believe, even if the number is as large as 100000000 , on dividing by 5 continuously, the final output should take quite less time.
Help in C#!!
My Code is Running successfully, giving desires o/p
But on submit getting error as Wrong Answer.
using System;
public class Test
{
public static void Main()
{// your code goes here
int n = Convert.ToInt32(Console.ReadLine());
int[] N = new int[n];
for(int i=0;i<N.Length;i++)
{
N[i] = Convert.ToInt32(Console.ReadLine());
}
for(int i=0;i<N.Length;i++)
{
int fact = 5;
int trailingzeros = 0;
while(N[i]>fact)
{
trailingzeros = trailingzeros + (N[i]/fact);
fact = fact*5;
}
Console.WriteLine(trailingzeros);
}
}
}