浙大 | PTA 练习7-9 计算天数 (15分)

本题要求编写程序计算某年某月某日是该年中的第几天。

输入格式:

输入在一行中按照格式“yyyy/mm/dd”(即“年/月/日”)给出日期。注意:闰年的判别条件是该年年份能被4整除但不能被100整除、或者能被400整除。闰年的2月有29天。

输出格式:

在一行输出日期是该年中的第几天。

输入样例1:

2009/03/02

输出样例1:

61

输入样例2:

2000/03/02

输出样例2:

62
#include <stdio.h>

int findmonth ( int n); 

int main(void)
{
    int month_28[12] = {31,28,31,30,31,30,31,31,30, 31,30,31};
    int year, month, day, days = 0;

    scanf("%d/%d/%d", &year, &month,&day);
    if (findmonth(year)) month_28[1] = 29; //判断闰年

    
    for(int i = 0; i < month - 1; i++) //算每月的天数
        days += month_28[i];
    days += day; //总天数

    printf("%d\n", days);
    return 0;
}

int findmonth (int n )
{
    int result = 0;
    if ((n % 4 == 0 && n % 100 != 0) || (n % 400 == 0))
        result = 1;
    return result;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值