Problem Description

請判斷某數是否為質數  

The Input

一個整數x, x>= 2 且 x<=2147483647

The Output

質數或非質數

Sample Input

13
14

Sample Output

質數
非質數

-----* Problem from【ZeroJudge, an Online Judge System For Beginners

My Answer

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  #include<iostream>
#include<cmath>

using namespace std;
int main(void)
{
  int x;
  while(cin>>x){
    bool f=true;
    int num=(int)sqrt(x);
    for(int i=2; i<=num; ++i){
      if(x%i==0){
        f=false;
        break;
      }
    }
    (f==false) ? cout<<"非質數"<<endl : cout<<"質數"<<endl;
  }
  return 0;
}

 


arrow
arrow
    全站熱搜

    兔老大 發表在 痞客邦 留言(0) 人氣()