SCUT Training 20170913 Problem I

来源:互联网 发布:西安行知教育地址 编辑:程序博客网 时间:2024/06/13 13:26

原题: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;        }    }}

原创粉丝点击