Reputation: 186
I gave this solution at the codechef for the problem code : FCTRL.
I saw the compile time of others people using the same the language c ( i am using c++ gcc 4.8.1) is somewhat less, mine is 0.46s while their is 0.23
Can somebody help me in reducing the time if possible?
#include<iostream>
using namespace std;
int main()
{
long int t,i,temp;
cin>>t;
long int n[t],a[t];
for(i=0;i<t;i++)
{
temp=1;
a[i]=0;
cin>>n[i];
while(temp)
{
temp=n[i]/5;
a[i]+=temp;
n[i]=n[i]/5;
}
}
for(i=0;i<t;i++)
cout<<a[i]<<"\n";
return(0);
}
Upvotes: 1
Views: 126
Reputation: 299730
You worst offense to C++ is using Variadic-Length arrays, those are non-standards.
And in fact, it turns out you absolutely do not need them. This problem can be solved on a line-per-line basis so using arrays to hold the input and output is useless.
Here is your program, simplified. I also noted that temp
was useless within the loop (though it was likely optimized out anyway, it polluted the code).
#include <iostream>
int main()
{
size_t number = 0;
std::cin >> number;
for(size_t i = 0 ; i < number; ++i)
{
size_t a = 0, n = 0;
std::cin >> n;
while (n)
{
n /= 5;
a += n;
}
std::cout << a << '\n';
}
}
Is it possible to do better ? Oh yes! The main issues here is that the C++ streams are none too fast so you may get a good boost by switching to the C reading methods... however they are not as nice (and safe).
Upvotes: 0
Reputation: 9380
From your description, as you are using c++ and they are using c, it might be due to how the compiler handles each instruction.
Also you may try replacing
temp=n[i]/5;
a[i]+=temp;
n[i]=n[i]/5;
By
temp=n[i]/5;
a[i]+=temp;
n[i]=temp; //why compute the value again
And see if some time reduces or not
Upvotes: 1