赞
踩
输入在一行中给出两个正整数M和N(1≤M≤N≤500)。
在一行中顺序输出M和N区间内素数的个数以及它们的和,数字间以空格分隔。
#include <stdio.h> #include <math.h> int main(){ int m,n; scanf("%d %d",&m,&n); int i; int count=0,sum=0;//赋初值 for(m;m<=n;m++){ int s; s=sqrt(m);//素数取值的判断标准只要取到开方数 for(i=2;i<=s;i++){ if(m%i==0){ break;//判断是不是素数 } } if(i>s){ if(m==1){//排除1的情况 count=count; } else{ sum+=m;//求和 count++;//素数标志加一 } } } printf("%d %d",count,sum); return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。