【ACM】How many prime numbers

本文介绍了一段使用C++编程语言实现的程序,该程序用于计算输入序列中素数的数量。通过输入序列逐个读取整数,并判断每个整数是否为素数,最后输出素数的总数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=2&sectionid=1&problemid=2

1

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	int f,s,n,i,m;
	
	while( scanf( "%d" , &n) != EOF ) {
		s = 0 ;
		while( n-- ) {
			scanf( "%d" , &m );
			f = 0;
			for( i = 2; i <= sqrt( m * 1.0) ; i++ ) { /*统计素数个数 */ 
				if( 0 == m % i   ) {     /*素数是除了1和本身能整除的数 如果到sqrt(m*1.0) 还不能整除就是素数*/ 
					f = 1 ; 
					break ;
				}
			}
			if ( 0 == f ) s++ ;
		}
		printf( "%d\n" , s );
	}
	
	return 0;
}

转载于:https://www.cnblogs.com/hInstance/p/3468279.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值