OJytu:Problem D: C语言习题5.12--查闰年

来源:互联网 发布:在线c语言编译 编辑:程序博客网 时间:2024/05/29 06:31

Problem D: C语言习题5.1

Description

大家知道如何判断某一年是否是闰年吗?这个问题可难坏了小编,小编在写一个查找m年到n年之间闰年的程序,却苦于判断闰年的函数不会写,据说 今天你有上机课,我就拿着这个问题来找你了--

闰年的条件是:能被4整除但不能被100整除,或能被400整除。

#include <stdio.h>
int leap_year(int n); /*声明判断闰年函数*/
int main()
{
int i;
int cnt; /*计数,用于每行满8个换行*/
int m,n;
scanf("%d %d",&m,&n); /*输入年份*/
cnt=0; /*初始化*/
for(i=m;i<=n;i++) /*遍历m到n的每一年*/
{
if(leap_year(i)) /*判断i年是不是闰年*/
{
printf("%d",i); /*输出闰年*/
cnt++; /*计数+1*/
if(cnt==8) /*够8个换行,cnt清0*/
{
printf("\n");
cnt=0;
}
else
{
printf(" ");
}
}
}
return 0;
}

主程序已给出,请自行编写leap_year函数并提交

Input

m年和第n

Output

m年至n年之间的所有闰年,输出结果每行8个,数据之间用空格分隔。

Sample Input

1949 2045

Sample Output

1952 1956 1960 1964 1968 1972 1976 19801984 1988 1992 1996 2000 2004 2008 20122016 2020 2024 2028 2032 2036 2040 2044
#include <stdio.h>int leap_year(int n);   /*声明判断闰年函数*/int main(){    int i;    int cnt;    /*计数,用于每行满8个换行*/    int m,n;    scanf("%d %d",&m,&n);   /*输入年份*/    cnt=0;  /*初始化*/    for(i=m;i<=n;i++)   /*遍历m到n的每一年*/    {        if(leap_year(i))    /*判断i年是不是闰年*/        {            printf("%d",i); /*输出闰年*/            cnt++;  /*计数+1*/            if(cnt==8) /*够8个换行,cnt清0*/            {                printf("\n");                cnt=0;            }            else            {                printf(" ");            }        }    }    return 0;}int leap_year(int n){if((n%4==0&&n%100!=0)||n%400==0)return 1;elsereturn 0;}


0 0
原创粉丝点击