Description
这是一个很经典也很简单的小题目,就是判断一个给定的正整数是否素数。
Input
有多组测试样例,每组输入在第一行给出一个正整数N(<=10),随后N行,每行给出一个小于2的31次的需要判断的正整数。
Output
对每个需要判断的正整数,如果它是素数,则在一行中输出“Yes”,否则输出“No”。
Sample Input
2 11 111
Sample Output
Yes No
一道很基础的题目
但是由于我最开始忽略了判断数为1时 进不了while循环 导致将1判断为素数的错误 一直不能ac
注释部分代码为错误代码
#include <iostream> using namespace std; int main(){ int n; int m; int i; loop:while(cin>>n){ while(n--){ cin>>m; if(m<2)cout<<"No"<<endl; else{ for(i=2;i<=m/2;i++) if(m%i==0) {cout<<"No"<<endl; goto loop; } cout<<"Yes"<<endl; } } } return 0; } /* #include <iostream> #include <cmath> using namespace std; int main() { int n; int i; bool j = true; int x; cin >> x; while(x--) { cin>>n; i = 2; while (i < n) { if (n % i == 0) { j = false; } i++; } if (j == true) cout<<"Yes"<<endl; else cout<<"No"<<endl; } return 0; } */