题解 | 素数判断

素数判断

https://www.nowcoder.com/practice/5ab1b9690af047699e96c87dee65def4

#include <iostream>
using namespace std;

int main() {
  int t;cin>>t;
  for (int i=1; i<=t; i++) {
    int n;
    cin>>n;

    int a=-2;//每个数都会被1和自己除(两次),刚好数字1只会被除1次,a变成-1不等于0,输出为No
    for(int j=1;j<=n;j++)
    {
      if(n%j==0)
      {
        a++;
      }
    }//让n从1到n除它

    cout<<(a==0?"Yes":"No")<<endl;

  }
    
}

全部评论

相关推荐

评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务