原题:http://acm.hdu.edu.cn/showproblem.php?pid=2161
思路:
判断质数而已,注意2在这题是不算质数的。
源代码:
#include <iostream> #include <cmath> using namespace std; int counter=0; int n,lim; bool out=false; int main() { while (cin>>n && n>0) { out=false; counter++; if (n==1 || n==2) { cout<<counter<<": no"<<endl; } else { lim=sqrt(n); for (int i = 2;i<=lim;i++) if (n%i==0) { cout << counter << ": no" << endl; out=true; break; } if (out==false) cout<<counter<<": yes"<<endl; } } }