2011-02-08 88 views
1

我在做这个问题:http://www.codechef.com/problems/FCTRL 我有解决方案,但是,内存使用情况出现为1.6 MB,显然,这太多了。我不明白如何减少这种情况,因为我几乎没有持久数据。这是我的代码:减少内存使用量,C,CodeChef

#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<=myPower; power++) { 
         five_counter += floor(temp/((int)(pow(5,power)))); 
       } 

       printf("%d\n", five_counter); 
       five_counter = 0; 
     } 
} 

正如你所看到的,它用C编写的。关于如何减少内存使用的任何想法?

+0

我对此可能完全错误,但是如果您将变量声明移到循环之外,它会有所作为吗? – WildCrustacean 2011-02-08 02:12:51

+0

他们被推入堆栈,然后在每次迭代的开始和结束时弹出。这可能会使其“执行较慢”,但不会增加或减少内存使用量。 – Marlon 2011-02-08 02:14:54

回答

0

这是我提出和被接受

#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; 
} 

的主要逻辑是,没有在阶乘结束零的为把号码5的最高功率。

-1

1.6MB共享内存有多少?您的流程有多少本地化?只需通过启动连接到C标准库的过程中,您将使用一定量的内存,即使你的整个程序是:

int main(int argc, char *argv[]) { 
    return 0; 
} 

你在哪里看到1.6MB身影?