1101 一年中的第几天

Description

给出某个日期,判断这是当年的第几天,每年的1月1日是第1天,非闰年年份的12月31日是第365天。

Input

输入的第一个数为n,后接n个日期。日期用3个正整数按顺序表示年月日,中间用“-”连接。

Output

若输入的日期为错误,则输出“error date!”;否则输出这个日期是本年的第几天。每个日期对应一行输出。

Sample Input

9
2010-01-01
2010-06-22
2010-12-31
2011-02-29
2011-07-18
2011-12-02
2012-02-29
2012-09-31
2012-12-31

Sample Output

1
173
365
error date!
199
336
60
error date!
366

HINT

抽象出判断闰年和判断日期合法性的两个函数,能够大大降低程序设计过程中的思维复杂程度。

Append Code

#include <stdio.h>
#include <string.h>
int main()
{
    int n,year,month,day,i,sum,j;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        int a[15]={0,31,28,31,30,31,30,31,31,30,31,30,31};
        sum=0;
        scanf("%d-%d-%d",&year,&month,&day);
        if((year%4==0&&year%100!=0)||(year%400==0))
            a[2]=29;
        if((day>0&&day<=a[month])&&(month>0&&month<=12))
        {
            for(j=1;j<month;j++)
            {
                sum+=a[j];
            }
            sum+=day;
            printf("%d\n",sum);
        }
        else
            printf("error date!\n");
    }
    return 0;
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值