1 / 2
Jun 2017

Here is the code:

#include <iostream>
#include <cmath>
int main()
{


int num_test_cases;
std::cin>>num_test_cases;
int ranges[num_test_cases][2];

for(int i = 0; i < num_test_cases; i++ )
{
    int range_start, range_end;
    std::cin>>range_start>>range_end;
    ranges[i][0]= range_start;
    ranges[i][1]=range_end;


}
std::cout<<std::endl;


for(int i=0; i<num_test_cases; i++)
{
bool first_primes[(int)sqrt(ranges[i][1] ) + 1];
bool output_primes[ranges[i][1]-ranges[i][0]];
for(int l=0; l<sizeof(first_primes)/sizeof(first_primes[0]); l++)
{
first_primes[l]=true;
}



for(int l=0; l<sizeof(output_primes)/sizeof(output_primes[0]); l++)
{
output_primes[l]=true;
}

for(int j=2; j<=sqrt(ranges[i][1]); j++)
{

if(first_primes[j]==true)
{
    for(int h = j*j; h <= (int)sqrt(ranges[i][1]); h+=j )
        first_primes[h]=false;
}

}

int low;
for(int j=2; j<=sizeof(first_primes)/sizeof(first_primes[0]); j++)
{ 

  if(first_primes[j]==true)
  {

   low = int((ranges[i][0]/j)*j);

   for(int g=low + j; g<=ranges[i][1]; g+=j)
   {
    if(g == j)
     g = j*2;
    if(g < ranges[i][0])
     continue;
    output_primes[g-ranges[i][0]]=false;
   }


 }

}

for(int o =0; o<=sizeof(output_primes)/sizeof(output_primes[0]); o++)
{
  if(output_primes[o]==true && o+ranges[i][0] != 1 )
    std::cout<<o+ranges[i][0]<<std::endl;
}
std::cout<<std::endl;
}



}

Whenever I input a range the generated primes look right and the formatting,(i.e one number per line and one empty line between each test case). Can someone please explain why I am getting a wrong answer?
`

  • created

    Jun '17
  • last reply

    Aug '17
  • 1

    reply

  • 607

    views

  • 2

    users

2 months later

Tôi nghĩ là ở chỗ hàm này:
for(int j=2; j<=sizeof(first_primes)/sizeof(first_primes[0]); j++)
không thể = sizeof được, mà chỉ có nhỏ hơn.
Trân trọng!

Suggested Topics

Topic Category Replies Views Activity
C and C++ 0 37 28d

Want to read more? Browse other topics in C and C++ or view latest topics.