I was doing a question on spoj cubefr .
I have written a code but i don’t know hy i am getting wrong answer always can anyone tell me please
#include<stdio.h>
#include<stdlib.h>
int main()
{
int arr[1000001];
int i,root,temp,j=0;
for(i=2;i<=100;i++)
{
temp = i*i*i;
root = temp;
while(root <=1000000)
{
arr[root] = 1;
root = root + temp;
}
}
int a[1000001];
a[0]=0;
a[1]=1;
int b=2,n;
int cnt=0;
for(j=2;j<=1000000;j++)
{
if (arr[j] != 1)
{
a[b] = j-cnt;
b++;
}
else
{
b++;
cnt++;
}
}
int k,t;
scanf("%d",&t);
for(k=1;k<=t;k++)
{
scanf("%d",&n);
if (arr[n] == 1)
{
printf("Case %d: Not Cube Free\n", k);
}
else
{
printf("Case %d: %d\n", k, a[n]);
}
}
return 0;
}
Local variables are not guaranteed to be zeroed on program execution. If you want to be sure that a and arr are zeroed, either make them global or use memset. I would do both by the way. It is generally a good idea to have big arrays in global scope(for competitions of course) and using memset you create a good routine that will help you for some multi-test case problems. Trust me – painful experience.