HDU 2138 How many prime numbers 题解由题意得:

1.输入一个数n,并随后输入n个数

2.输出n个数中素数的个数

3.代码如下:

#include<iostream>
#include<cmath>
using namespace std;
bool sieve(int num)
{
	if(num==1) return false;
	else{
        int t=sqrt(num);//缩小范围 若直接i<num 会超时
		for(int i=2;i<=t;i++)
		 if(num%i==0)
		  return false;
    return true;
	}
}

int main()
{
	int n,num,count;
	while(cin>>n){
		count=0;
		for(int i=1;i<=n;i++){
			cin>>num;
			if(sieve(num))
			 count++;
		}
		cout<<count<<endl;
	}
return 0;
}

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-07-26
  • 2021-05-21
  • 2021-11-30
  • 2022-12-23
  • 2021-12-17
  • 2021-06-13
猜你喜欢
  • 2021-10-30
  • 2022-02-23
  • 2021-08-25
  • 2022-02-04
  • 2021-07-05
相关资源
相似解决方案