2017年10月6日 星期五

[C_MM30-易] 質數判別(C++)

[C_MM30-易] 質數判別

  1. #include <iostream>    
  2. #include <cmath>    
  3. using namespace std;    
  4.     
  5. int main()    
  6. {    
  7.      int num, count;    
  8.      while(cin >> num)  
  9.      {  
  10.          for(int j = 1;j <= num;j++)    
  11.          {    
  12.               if(num % j == 0)    //計算可以被除盡的次數
  13.               {    
  14.                    count++;    
  15.               }    
  16.          }    
  17.          if(count == 2)    //等於二代表只有1和數字本身
  18.          {    
  19.               cout << "YES" << endl;    
  20.          }    
  21.          else  
  22.          {  
  23.             cout << "NO" << endl;    
  24.          }  
  25.          count = 0;    
  26.      }  
  27. }  

沒有留言:

張貼留言