#include <stdio.h>
int main()
{
int year = 0;
int count++;//计算共有多少年
for(year=1000; year<=2000; year++)
{
//判断year是否为闰年
//写法一:
if (year%4==0 && year%100!=0)
{
printf("%d", year);
}
else if(year%400==0)
{
printf("%d",year);
count++;
}
//写法二:
if(((year%4==0)&&(year%100!=0))||(year%400==0))
{
printf("%d ",year);
count++;
}
}
printf("\ncount = %d\n", count);
return 0;
}
公历年份是4的倍数,且不是100的倍数,为普通闰年。
公历年份是整百数,且必须是400的倍数,是世纪闰年。
归结起来就是通常说的:四年一闰;百年不闰,四百年再闰。
count:统计共有闰年的年数
#include <stdio.h>
#include <math.h>
int main()
{
int i = 0;
int count = 0;//统计个数
for(i=100; i<=200; i++)
{
//判断i是否为素数
//素数判断的规则:试除法
/*1.eg:11为素数,判断时在2-10之间寻找没有被11整除的数;
2.eg:16不是素数,16=2*8=4*4 其中16的因子中是少有一个数<=开平方4
即:i=a*b a和b中至少有一个<=i的开平方数 */
int j = 0;
for(j=2; j<i; j++)//2.for(j=2;j<=sqrt(i);j++)--sqrt为数学库函数-也可把sqrt(i)换成<i/2 //3.for(i=101;i<=200;i+=2)--偶数不可能为素数 找到奇数+2
{
if(i%j==0)
{
break;
}
}
if(j==i)//2.3.if(j>sqrt(i))
{
printf("%d ", i);
}
}
printf("\ncount = %d\n", count);
return 0;
}
求解素数的方法很多 具体可参考求解素数的n种境界:C语言:素数求解的N种境界 - 百度文库 (baidu.com)