于归


私信TA

用户名:dotcpp0681287

访问量:197

签 名:

等  级
排  名 9041
经  验 1128
参赛次数 0
文章发表 7
年  龄 0
在职情况 学生
学  校
专  业

  自我简介:

解题思路:

使用 isLeapYear函数来判断输入的年份是否为闰年,该函数返回1表示是闰年,返回0表示不是闰年。

然后,使用 calculateDayOfYear函数来计算该日在本年中是第几天。

在函数中,使用了一个数组 daysInMonth来存储每个月份的天数。在循环中,将输入的月份之前的所有天数累加到 dayOfYear中。然后,将输入的日期也加到 dayOfYear中。


注意事项:

如果是闰年且当前月份大于2月,则需要额外加1天。


参考代码:

#include<stdio.h>

typedef struct Date{

    int year;

    int month;

    int day;

}Date;


int isLeapYear(int year) {

    if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {

        return 1; // 是闰年

    }

    return 0; // 不是闰年

}


int calculateDayOfYear(Date date) {

    int daysInMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    int dayOfYear = 0;

    for (int i = 1; i < date.month; i++) {

        dayOfYear += daysInMonth[i];

    }


    dayOfYear += date.day;

    

    if (isLeapYear(date.year) && date.month > 2) {

        dayOfYear += 1; // 是闰年且当前月份大于2月,则需要额外加1天

    }

    

    return dayOfYear;

}


int main()

{

    Date date;

    scanf("%d %d %d",&date.year,&date.month,&date.day);

    int dayOfYear = calculateDayOfYear(date);

    printf("%d",dayOfYear);

return 0;

}


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区