Decreasing memory usage, C, CodeChef
I'm doing this problem: http://www.codechef.com/problems/FCTRL I have the solution, but, the memory usage is coming out to be 1.6 MB, which, apparently, is too much. I don't understand how I can decrease this seeing that I have almost no persistent data. Here's my code:
#include <stdio.h>
#include <math.h>
int maxPower(long x) {
int i;
for(i = 0; i<= 100; i++) {
long myPower = pow(5,i);
if(myPower > x) {
return (i-1);
}
}
}
int main (void) {
int lines;
scanf("%d", &lines);
int i;
for(i = 0; i<lines; i++) {
long temp;
scanf("%ld", &temp);
int five_counter = 0;
int myPower = maxPower(temp);
int power;
for(power = 1; power<=my开发者_运维问答Power; power++) {
five_counter += floor(temp/((int)(pow(5,power))));
}
printf("%d\n", five_counter);
five_counter = 0;
}
}
As you can see, its written in C. Any ideas on how to decrease memory usage?
This is what i submitted and it was accepted
#include<stdio.h>
int main()
{
long n,q=0,t,c=1,result,z,m=5;
scanf("%ld",&t);
while(c<=t)
{
scanf("%ld",&n);
z=0;
while(n!=0)
{
q=n / m;
z=z+q;
n=q;
}
printf("%ld\n",z);
c++;
}
return 0;
}
The main logic is that no of zeroes at the end of a factorial is the highest power of 5 that divides the number.
How much of that 1.6MB is shared memory? How much is local to your process? Simply by starting a process linked to the C standard library, you'll use a certain amount of memory, even if your entire program is:
int main(int argc, char *argv[]) {
return 0;
}
Where are you seeing the 1.6MB figure?
精彩评论