首页 > ACM题库 > HDU-杭电 > hdu 2138 How many prime numbers-最小生成树-[解题报告]C++
2013
12-29

hdu 2138 How many prime numbers-最小生成树-[解题报告]C++

How many prime numbers

问题描述 :

  Give you a lot of positive integers, just to find out how many prime numbers there are.

输入:

  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.

输出:

  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.

样例输入:

3
2 3 4

样例输出:

2

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2138

解题思路:

复习一下素数,这道题数据太BT。没法打表,只能暴力了。。。。~

sqrt尽量写在外面。要不然累计时间

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
using namespace std;

bool isprim(int n)
{
	int res = sqrt(n + 0.0); //注意超时~
	for(int i = 2; i <= res; ++i)
	{
		if(n % i == 0)
			return false;
	}
	return true;
}

int main()
{
	int num, n, count;
	while(scanf("%d", &num) != EOF)
	{
		count = 0;	
		for(int i =0; i < num; ++i)
		{
			scanf("%d", &n);
			if(isprim(n))
				count++;
		}
		printf("%d\n", count);
	}
	return 0;
}

解题转自:http://blog.csdn.net/niushuai666/article/details/7010897