HDU-#2005 第几天?

      题目大意:给出年月日,求是该年的第几天?

     解题思路:写法有很多,主要考察switch的用法。详见code。

     题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=2005

     code:

#include <iostream>
#include <cstdio>
using namespace std;

int year,month,day,ans;

int main(){
    while(scanf("%d/%d/%d",&year,&month,&day)!=EOF){
        ans=0;
        switch(month-1){ //这里需要减一,加的是上个月的时间,本月的只加天数
            case 11: ans+=30; //case必须倒着写,因为没有遇到break,程序会加上其下属的值
            case 10:  ans+=31;
            case 9: ans+=30;
            case 8: ans+=31;
            case 7: ans+=31;
            case 6: ans+=30;
            case 5: ans+=31;
            case 4: ans+=30;
            case 3: ans+=31;
            case 2: if((year%4==0&&year%100!=0)||(year%400==0)) ans+=29;
                    else ans+=28;
            case 1: ans+=31;
        }
        ans+=day;
        printf("%d\n",ans);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值