华为OJ——计算日期到天数转换

计算日期到天数转换

题目描述

根据输入的日期,计算是这一年的第几天。。

详细描述:

输入某年某月某日,判断这一天是这一年的第几天?。

接口设计及说明:

 /*****************************************************************************
 Description   : 数据转换
 Input Param   : year 输入年份
                Month 输入月份
                Day 输入天
                    
 Output Param  :
 Return Value  : 成功返回0,失败返回-1(如:数据错误)
 *****************************************************************************/
 public static int iConverDateToDay(int year, int month, int day)
 {
     /* 在这里实现功能,将结果填入输入数组中*/ 
     return 0;
 }
 
 /*****************************************************************************
 Description   : 
 Input Param   :
                    
 Output Param  :
 Return Value  : 成功:返回outDay输出计算后的第几天;
                                           失败:返回-1
 *****************************************************************************/
 public static int getOutDay()
 {
  return 0;
 }

输入描述:

输入三行,分别是年,月,日

输出描述:

成功:返回outDay输出计算后的第几天;
失败:返回-1

输入例子:

2012

12

31

输出例子:

366

解答代码:

#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <cstring>
#include <algorithm>
using namespace std;

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

int main()
{
    int year,mouth,day;
    int list[12]= {31,28,31,30,31,30,31,31,30,31,30,31};
    int i,result=0;

    while(cin>>year>>mouth>>day)
    {
        result=0;
        for(i=0; i<mouth-1; i++)
            result+=list[i];
        if(mouth>2 && judgeYear(year))
            result+=1;
        result+=day;

        cout<<result<<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值