I - How many prime numbers HDU - 2138
Posted 神韵袖藏
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了I - How many prime numbers HDU - 2138相关的知识,希望对你有一定的参考价值。
Give you a lot of positive integers, just to find out how many prime numbers there are.
Input There are a lot of cases. In each case, there is an integer N representing the number of integers to find. Each integer won’t exceed 32-bit signed integer, and each of them won’t be less than 2.Output For each case, print the number of prime numbers you have found out.Sample Input
3 2 3 4
Sample Output
2
最开始用打表筛选素数,居然超时间。然后猜想到这个测试的数据的数目可能不大,然后就改为检测每一个数是不是素数的方法,就ac了
#include<iostream>
#include<cmath>
using namespace std;
bool fun(int num);
int main()
{
int datanum, num,ans;
while (cin >> datanum)
{
ans = 0;
while (datanum--)
{
cin >> num;
if (num == 1) continue;
if (num == 2 || fun(num)) ans++;
}
cout << ans << endl;
}
}
bool fun(int num)
{
for (int i = 2; i <= sqrt(num); i++)
{
if (num%i == 0) return false;
}
return true;
}
以上是关于I - How many prime numbers HDU - 2138的主要内容,如果未能解决你的问题,请参考以下文章
HDU-2138 How many prime numbers
HDU 2138 How many prime numbers
HDU 2138 How many prime numbers (判素数,米勒拉宾算法)
HDu 2138 How many prime numbers 高效Miller素数測试
[LeetCode] 1365. How Many Numbers Are Smaller Than the Current Number