signed

QiShunwang

“诚信为本、客户至上”

《c语言程序设计》习题.日期计算

2020/12/27 13:56:56   来源:
#include <stdio.h>
int GetYearDay(int year, int month, int day);
int IsLeap(int year);
int main()
{
    int year, month, day;
    scanf("%d%d%d", &year, &month, &day);
    printf("%d", GetYearDay(year, month, day));
}
int GetYearDay(int year, int month, int day)
{
    int k, leap;
    int tab[2][13] = {{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
                      {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
    leap = IsLeap(year);
    for (k = 0; k < month; k++)
    {
        day += tab[leap][k];
        /* code */
    }
    return day;
}
int IsLeap(int year)
{
    return ((year % 4 == 0 && year % 100 != 0 || year % 400 == 0));
}