C语言编程:从键盘输入你的出生年和今年的年份,编程判断并输出从你的出生年到今年之间中有多少个闰年。

从键盘输入你的出生年和今年的年份,编程判断并输出从你的出生年到今年之间中有多少个闰年。
程序的运行结果示例1: 
Input your birth year:2000↙
Input this year:2020↙
2000
2004
2008
2012
2016
2020
count=6

这个挺简单的就是普通的判断闰年再加上循环就可以了,具体代码如下:

#include <stdio.h>
#include<stdlib.h>

main()
{   
int i,by,ty,r=0;
printf("Input your birth year:");
scanf("%d",&by);
printf("Input this year:");
scanf("%d",&ty);
for(i=by;i<=ty;i++){
if(i%400==0||i%100!=0&&i%4==0){
printf("%d\n",i);
r++;
}
}
printf("count=%d\n",r);
system("pause");
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-11-10
第一种,循环判断所有年份
第二种,减法除以4再判断是否有特殊年份在区间内,比如判断百年就是今年/100去整-出生/100去整,判断四百年同理。还要判断头尾年的情况,比如1998-1994是两个,1997-1993是一个。追问

我写出来了 但是那种方法在慕课上不能通过 = =

本回答被提问者和网友采纳
第2个回答  2017-12-13
#include <stdio.h>
int main()
{
    int y1, y2, i, count;
    printf("Input your birth year:");
    scanf("%d", &y1);
    printf("Input this year:");
    scanf("%d",&y2);
    count= 0;
    for(i = y1; i <= y2; i++)
    {
        if(((i % 4 == 0) && (i % 100 != 0)) || i % 400 == 0)
            printf("%d\n",i);
    }
    for(i = y1; i <= y2; i++)
    {
        if(((i % 4 == 0) && (i % 100 != 0)) || i % 400 == 0)
            count++;
    }
    printf("count=%d\n", count);
    return 0;

}

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网