错误答案总是对cubefr(spoj)

时间:2012-06-01 05:51:05

标签: c algorithm

我在诈骗cubefr上做了一个问题。

我写了一段代码,但我不知道我得到了错误答案总是可以有人告诉我

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

1 个答案:

答案 0 :(得分:2)

不保证局部变量在程序执行时归零。如果要确保a和arr归零,请将它们设为全局或使用memset。我会顺便做两件事。一般来说,在全球范围内拥有大型阵列(当然是竞赛)并使用memset创建一个良好的例程来帮助您解决一些多测试案例问题通常是一个好主意。相信我 - 痛苦的经历。