我在做这个问题: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编写的。关于如何减少内存使用的任何想法?
我对此可能完全错误,但是如果您将变量声明移到循环之外,它会有所作为吗? – WildCrustacean 2011-02-08 02:12:51
他们被推入堆栈,然后在每次迭代的开始和结束时弹出。这可能会使其“执行较慢”,但不会增加或减少内存使用量。 – Marlon 2011-02-08 02:14:54