码迷,mamicode.com
首页 > 其他好文 > 详细

How many prime numbers(素数)

时间:2015-08-16 16:31:59      阅读:101      评论:0      收藏:0      [点我收藏+]

标签:

Problem Description
  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
 

 

Author
wangye
 

 

Source

 

 

 1 #include<stdio.h>
 2 #include<math.h>
 3   int prime(int x)
 4     {  int i;
 5         if(x<=2)   return 1;
 6         for(i=2;i<=sqrt(x*1.0);i++)   //i*i<=x  是会超时的,因为i*i每次都要计算
 7           {
 8               if(x%i==0)  
 9                 {
10                     return 0;
11                     break;
12                 }
13           }
14         return 1;
15     }
16   int main()
17     {
18         int n;
19         int i;
20         int sum;
21         int a;
22         while(scanf("%d",&n)!=EOF)
23           {
24                  sum=0;
25                for(i=0;i<n;i++)
26                 {
27                     scanf("%d",&a);
28                       if(prime(a))
29                         sum++;
30                 }      
31               printf("%d\n",sum);     
32           }
33         return 0;
34         
35     }

 

How many prime numbers(素数)

标签:

原文地址:http://www.cnblogs.com/wangmengmeng/p/4734377.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!