栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

ZCMU--5104: SuShu?(C语言)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

ZCMU--5104: SuShu?(C语言)

Description

HH likes math very much. Today he found some numbers that are special. These special
numbers are defined as "numbers that can be divisible by less than 3 positive integers." Now HH
wants to ask you how many special numbers there are in the interval [L,R].

Input

The first line contains two integers L,R — the interval. ( 0 <= L <= R <= 100000 )

Output

Output the number of special numbers in the interval [L,R].

Sample Input

3 6

Sample Output

2

HINT

Only 3, 5 in 3, 4, 5, 6 are special numbers

解析:题目让我们求出给定区间内能被小于3个数整除的数,我们知道一个数肯定能被1和它本身整除,因此几乎就是让我们判断区间有多少素数,为什么是几乎呢,因为这道题中1也满足条件,是特殊数,0不是特殊数,其他跟素数无异。

#include 
int su(int n){
	int i,shi=1; //shi表示是否为特殊数 
	if(n==0) shi=0;
	if(n==1) shi=1;		//1是特殊数 
	for(i=2;i*i<=n;i++){	//到i*i<=n即可,跟素数相同 
		if(n%i==0){
			shi=0;
			break;
		}
	}
	return shi;
}
int main()
{
	int a,b,i,cnt=0;
	scanf("%d%d",&a,&b);
	for(i=a;i<=b;i++){
		if(su(i)==1) cnt++; //计数 
	}
	printf("%dn",cnt);
	return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/917003.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号