美文网首页
【基础学习】C 计算闰年

【基础学习】C 计算闰年

作者: Jiubao | 来源:发表于2016-12-30 15:52 被阅读15次

闰年计算逻辑:能够被 4 整除的年份是闰年,但其中能够被 100 整除的却不是闰年,除非它同时能被 400 整除。

实现逻辑:

#include <stdio.h>

int is_leap_year(int a) {
    int leap_year = 0;

    if (a % 4 == 0)
    {
        leap_year = 1;

        if (a % 100 ==0)
        {
            leap_year = 0;

            if (a % 400 == 0)
            {
                leap_year = 1;
            }
        }
    }

    return leap_year;
}

int main() {
    int a[6] = {2000, 1990, 2010, 1900, 2012, 2002};

    for (int i = 0; i <6 ; ++i)
    {
        if (is_leap_year(a[i]))
        {   
            printf("%d is leap year\n", a[i]);
        } else {
            printf("%d is not leap year\n", a[i]);
        }
    }

    return 0;
}

运行:

➜  C ./a.out 
2000 is leap year
1990 is not leap year
2010 is not leap year
1900 is not leap year
2012 is leap year
2002 is not leap year

相关文章

网友评论

      本文标题:【基础学习】C 计算闰年

      本文链接:https://www.haomeiwen.com/subject/uevmvttx.html