i am posting here to receive some help from some senior coders out there about the memory occupied by a c++ or c programme.
the situation was as follows.
I'm doing this problem: http://www.codechef.com/problems/RECIPE I have the solution, but, the memory usage is coming out to be 2.0 MB, which, apparently, is too much considering some ppl have done it using less 0.00...MB memory . I don't understand how My programme is any different than the others..
Can someone please explain how was the programme given below is so different from mine that it uses such less memory..
Some tips on how to decrease memory usage would be appreciated..
(If u want to run the programme please refer to the question before)
SOMEONES CODE (0.00M memory occupied)
#include<stdio.h>
int get_gcd(int a, int b)
{
int c = a%b;
if(c == 0)
return b;
else
return get_gcd(b, c);
}
main()
{
int T;
int ing[50];
scanf("%d", &T);
while(T-- > 0)
{
int N, i, min = 1001, second_min, fact, denom;
scanf("%d", &N);
for(i = 0; i < N; i++)
{
scanf("%d", &ing[i]);
if(min > ing[i])
{
second_min = min;
min = ing[i];
}
else if(second_min > ing[i])
second_min = ing[i];
}
fact = get_gcd(second_min, min);
while(fact != 1)
{
for(i = 0; i < N; i++)
{
if(ing[i]%fact)
break;
}
if(i == N)
break;
fact--;
while(min%fact || second_min%fact)
fact--;
}
if(fact != 1)
{
for(i = 0; i < N-1; i++)
printf("%d ", ing[i]/fact);
printf("%d", ing[i]/fact);
}
else
{
for(i = 0; i < N-1; i++)
printf("%d ", ing[i]);
printf("%d", ing[i]);
}
printf("\n");
}
return 0;
}
MY C CODE (2.0M memory occupied)
#include<stdio.h>
int main()
{
int T,N,i[50],j,p,r;
scanf("%d",&T);
while(T--)
{
scanf("%d",&N);
for(j=0;j<N;j++)
{
scanf("%d",&i[j]);
}
k:printf("");
int min=i[0];
for(j=0;j<N;j++)
{
if(min>i[j])
{
min=i[j];
}
}
for(r=2;r<=min;r++)
{
p=0;
for(j=0;j<N;j++)
{
if(i[j]%r==0)
{
p++;
}
}
if(p==N)
{
goto t;
}
}
for(j=0;j<N;j++)
{
printf("%d ",i[j]);
}
goto p;
t:for(j=0;j<N;j++)
{
i[j]=i[j]/r;
}
goto k;
p:printf("");
}
}