#5878: 小的質數自己測試OK,但是ZeroJudge給我...WA


shanehsu (Shane)

學校 : 國立武陵高級中學
編號 : 20013
來源 : [114.33.126.145]
最後登入時間 :
2016-11-09 06:12:46
a007. 判斷質數 | From: [118.161.80.135] | 發表日期 : 2011-10-05 20:07

#include <iostream>

using namespace std;

int main () {

int Primes[46340] = { 0 } ;

int runner = 0 ;

int chaser = 0 ;

int bound = 0 ;

int count = 0 ;

cin >> bound ;

if (bound == 2){

cout << "質數" ;

return 0;

}

if (runner < bound + 1 ) {

Primes[0] = 2;

count = 0;

for ( runner = 3 ; runner < bound +1 ; runner ++ ) {

for ( chaser = 0 ; Primes[chaser] != 0 ; chaser ++) {

if ( runner % Primes[chaser] == 0 ){

break;

}

if ( chaser == count ) {

count += 1;

Primes[count] = runner;

break;

}

}

}

}

if ( bound != Primes[count] ) {

cout << "非質數"

}

else if ( bound == Primes[count] ) {

cout << "質數" ;

}

return 0;

}

 

請各位幫我找一下盲點。Thanks A Lot! 

 
#5882: Re:小的質數自己測試OK,但是ZeroJudge給我...WA


n1415926535 (nick)

學校 : 不指定學校
編號 : 20436
來源 : [203.121.231.227]
最後登入時間 :
2020-12-29 01:38:36
a007. 判斷質數 | From: [114.24.30.186] | 發表日期 : 2011-10-05 22:38

#include

using namespace std;

int main () {

int Primes[46340] = { 0 } ;

int runner = 0 ;

int chaser = 0 ;

int bound = 0 ;

int count = 0 ;

cin >> bound ;

if (bound == 2){

cout << "質數" ;

return 0;

}

if (runner < bound + 1 ) {

Primes[0] = 2;

count = 0;

for ( runner = 3 ; runner < bound +1 ; runner ++ ) {

for ( chaser = 0 ; Primes[chaser] != 0 ; chaser ++) {

if ( runner % Primes[chaser] == 0 ){

break;

}

if ( chaser == count ) {

count += 1;

Primes[count] = runner;

break;

}

}

}

}

if ( bound != Primes[count] ) {

cout << "非質數"

}

else if ( bound == Primes[count] ) {

cout << "質數" ;

}

return 0;

}

 

請各位幫我找一下盲點。Thanks A Lot! 

我不知道哪有盲點但我是這樣過的

#include <stdio.h>
#include <math.h>

int prime(int);

int main(void)
{
    int n;
   
    while(scanf("%d",&n)!=EOF)
    {
    if(prime(n))
       printf("質數");
    else
       printf("非質數");
    }
      
    return 0;
}

int prime(int n)
{
    int i;
    double m;

    m=sqrt(n);

    if(n==1)
       return 0;
    for(i=2;i<=m;i++)
       if((n%i)==0)
          return 0;
    return 1;
}
 


 
ZeroJudge Forum