HDOJ 2005 第几天?

Problem Description
给定一个日期,输出这个日期是该年的第几天。
 

Input
输入数据有多组,每组占一行,数据格式为YYYY/MM/DD组成,具体参见sample input ,另外,可以向你确保所有的输入数据是合法的。
 

Output
对于每组输入数据,输出一行,表示该日期是该年的第几天。
 

Sample Input
  
  
1985/1/20 2006/3/12
 

Sample Output
  
  
20 71

水题,不过有几点需要注意:

1.需要判断是不是闰年。

2.这里需要一个小技巧来“吃掉”输入中的/,定义一个字符型变量存储即可。

3.注意逻辑结构,我第一次写的时候逻辑错了,错误代码给出,代码中注释的部分。

4.我还有一点错误的地方:||的错误用法

  month==1||3||5||7||8||10||12

代码如下:

#include <iostream>
using namespace std;


int isLeapYear(int year)
{
    if((year%4==0&&year%100!=0)||(year%400==0))
        return 1;
    else
        return 0;
}

int month_days(int year,int month)
{
    int monthdays;
    /*if(month==1||3||5||7||8||10||12)
        monthdays=31;
    else if(month!=2)   逻辑错了,!=2是包含第一个if里的情况的。
        monthdays=30;
    else if(month==2)
    {
        if(isLeapYear(year))
            monthdays=29;
        else
            monthdays=28;
    }
    return monthdays;*/
    if(month!=2)
    {
        if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
            monthdays=31;
        else if(month==4||month==6||month==9||month==11)
            monthdays=30;
    }
    else
    {
        if(isLeapYear(year))
            monthdays=29;
        else
            monthdays=28;
    }
    return monthdays;
}

int main()
{
    int year,month,day,dayth=0;
    char eat;
    while(cin>>year>>eat>>month>>eat>>day)
    {
        for(int i=1;i<month;i++)
        {
            dayth+=month_days(year,i);
        }
        dayth+=day;
        cout<<dayth<<endl;
        dayth=0;
    }
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值