用C语言根据天数输出对应的年、月、日

题目描述:输入年份和这一年的第几天,输出具体的年、月、日的信息。(注意闰年的判断!)

输入要求:输入两个整数分别代表年份和这一年的第几天。(假设数据都在有效范围内)

输出要求:输出对应的年、月、日。输出的数字之间以一条横线间隔,输出完毕换行。

代码如下:

#include<stdio.h>

//接口定义,year为年,date为天数,*nmonth是计算得出的月,*nday是计算得出的日
void month_day(int year, int date, int* nmonth, int* nday) {
    int yu, hao;
    int wang[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},                        //定义数组存放闰年和平年的天数
    };
    hao = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;        //闰年判别条件
    for (yu = 1; date > wang[hao][yu]; yu++)
        date -= wang[hao][yu];
    *nmonth = yu;
    *nday = date;
}
int main() {
    int day, month, year, date;                                //定义日、月、年和天数的变量
    printf("请输入年份和第几天:\n");
    scanf_s("%d%d", &year, &date);
    if (date >= 1 && year >= 1) {
        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0 && date < 367) {
            month_day(year, date, &month, &day);
            printf("具体年、月、日如下:\n%d-%d-%d", year, month, day);
        }
        if (year % 4 != 0 && date < 366) {
            month_day(year, date, &month, &day);
            printf("具体年、月、日如下:\n%d-%d-%d", year, month, day);
        }
    }
    else {
        printf("搞事情是吧?这边给你强行退出!\n");
    }
    return 0;
}

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值