ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1086. Cryptography

Runtime error (access violation) and I don't know why?
Posted by kaa..........ai 21 Jun 2013 21:01
Here's my code:
#include<iostream>
using namespace std;
int pri[20000],pr[4040000];
void p()
{
    memset(pr,0,sizeof(pr));
    int i,j;
    for(i=2;i<=1500;i++)
        for(j=i*i;j<4000000;j+=i)pr[j]=1;
    int kk=1;
    for(i=2;i<=4000000;i++)
        if(pr[i]==0)pri[kk++]=i;
}
int main()
{
    int n,t;
    p();
    while(scanf("%d",&n)!=-1)
    {
    while(n--)
    {
        scanf("%d",&t);
        printf("%d\n",pri[t]);
    }
    }
    return 0;
}
What's wrong with it?Who can help me?

Edited by author 21.06.2013 21:02
Re: Runtime error (access violation) and I don't know why?
Posted by kaa..........ai 21 Jun 2013 21:39
Cheer up!
Solved!
Here's my code:(一个合适的范围是看来是很必要的啊!)
#include<iostream>
using namespace std;
int pri[20000];
int pr[204000];
void p()
{
    int i,j;
    for(i=0;i<=200000;i++)pr[i]=1;
    for(i=2;i<=447;i++)
        for(j=i*i;j<200000;j+=i)pr[j]=0;
    int kk=1;
    for(i=2;i<=200000&&kk<=15010;i++)
        if(pr[i])pri[kk++]=i;
}
int main()
{
    int n,t;
    p();
    while(scanf("%d",&n)!=-1)
    {
       while(n--)
       {
        scanf("%d",&t);
        printf("%d\n",pri[t]);
       }
    }
    return 0;
}