题解 | #KiKi求质数个数#
KiKi求质数个数
http://www.nowcoder.com/practice/d3a404ee0f8d41f98bf4707035d91086
#include <stdio.h>
int main()
{
int n, i, cnt=0;
for (n=100; n<1000; n++)
{
int flag =1; //学会用flag,当只要有一个个例出现就可以确定的判断时
for(i=2; i<n; i++) //对从2到n-1的数取余
{
if(n % i == 0) //只要有一个取余=0,表示可以整除,不是质数
flag = 0; //易帜
}
if(flag == 1) //内循环结束时全都没有易帜,表示全都不能整除,是质数
cnt++;
}
printf("%d\n", cnt);
return 0;
}


查看12道真题和解析