#5800: 為何Line 10就WA?


kin1010hk (kin1010hk)


本人使用C++,程式碼如下:

#include <iostream>
using namespace std;
int main () {
    int x;
    while (cin >> x) {
          if (x%2==0 && x!=1 && x!=2 && x!=3 && x!=5 && x!=7 && x!=11 || x%3==0 && x!=1 && x!=2 && x!=3 && x!=5 && x!=7 && x!=11 || x%5==0 && x!=1 && x!=2 && x!=3 && x!=5 && x!=7 && x!=11 || x%7==0 && x!=1 && x!=2 && x!=3 && x!=5 && x!=7 && x!=11 || x%11==0 && x!=1 && x!=2 && x!=3 && x!=5 && x!=7 && x!=11) {
                     cout << "非質數" << endl;
                     }
          else {
               cout << "質數" << endl;
               }
}
} 為何在Line 10 就WA?
#5801: Re:為何Line 10就WA?


tigeryangname (LF2magic)


本人使用C++,程式碼如下:

#include
using namespace std;
int main () {
    int x;
    while (cin >> x) {
          if (x%2==0 && x!=1 && x!=2 && x!=3 && x!=5 && x!=7 && x!=11 || x%3==0 && x!=1 && x!=2 && x!=3 && x!=5 && x!=7 && x!=11 || x%5==0 && x!=1 && x!=2 && x!=3 && x!=5 && x!=7 && x!=11 || x%7==0 && x!=1 && x!=2 && x!=3 && x!=5 && x!=7 && x!=11 || x%11==0 && x!=1 && x!=2 && x!=3 && x!=5 && x!=7 && x!=11) {
                     cout << "非質數" << endl;
                     }
          else {
               cout << "質數" << endl;
               }
}
} 為何在Line 10 就WA?

路過~召喚高人(?)
#5822: Re:為何Line 10就WA?


longbiau ((~o ̄▽ ̄)o Summer)


本人使用C++,程式碼如下:

#include
using namespace std;
int main () {
    int x;
    while (cin >> x) {
          if (x%2==0 && x!=1 && x!=2 && x!=3 && x!=5 && x!=7 && x!=11 || x%3==0 && x!=1 && x!=2 && x!=3 && x!=5 && x!=7 && x!=11 || x%5==0 && x!=1 && x!=2 && x!=3 && x!=5 && x!=7 && x!=11 || x%7==0 && x!=1 && x!=2 && x!=3 && x!=5 && x!=7 && x!=11 || x%11==0 && x!=1 && x!=2 && x!=3 && x!=5 && x!=7 && x!=11) {
                     cout << "非質數" << endl;
                     }
          else {
               cout << "質數" << endl;
               }
}
} 為何在Line 10 就WA?
驚訝if中的條件太過混亂了,而且沒適當加入括號。
if ((x%2==0 && x!=1 && x!=2 && x!=3 && x!=5 && x!=7 && x!=11) || (x%3==0 && x!=1 && x!=2 && x!=3 && x!=5 && x!=7 && x!=11) || (x%5==0 && x!=1 && x!=2 && x!=3 && x!=5 && x!=7 && x!=11) || (x%7==0 && x!=1 && x!=2 && x!=3 && x!=5 && x!=7 && x!=11) || (x%11==0 && x!=1 && x!=2 && x!=3 && x!=5 && x!=7 && x!=11)
不過就算這樣改還是不行,如果當x等於1或169(例子很多),就掛掉了。還是請從質數的定義下手吧。