messytang 2015-09-13 06:14 采纳率: 0%
浏览 2491

ccf 日期问题 求答案求答案

问题描述
  给定一个年份y和一个整数d,问这一年的第d天是几月几日?
  注意闰年的2月有29天。满足下面条件之一的是闰年:
  1) 年份是4的整数倍,而且不是100的整数倍;
  2) 年份是400的整数倍。
输入格式
  输入的第一行包含一个整数y,表示年份,年份在1900到2015之间(包含1900和2015)。
  输入的第二行包含一个整数d,d在1至365之间。
输出格式
  输出两行,每行一个整数,分别表示答案的月份和日期。
样例输入
2015
80
样例输出
3
21
样例输入
2000
40
样例输出
2
9

  • 写回答

2条回答 默认 最新

  • 霓为衣兮风为裳 2015-09-13 09:36
    关注
     #pragma once
    class CDate
    {
    
    public:
        CDate();
        CDate(unsigned year, unsigned month, unsigned day) :m_nYear(year), m_nMonth(month), m_nDay(day){}
        virtual ~CDate();
    
    public:
        friend int DaysMinus(const CDate& smallDate, const CDate& bigDate);
    private:
        unsigned int m_nYear;
        unsigned int m_nMonth;
        unsigned int m_nDay;
    
    };
    
    #include "stdafx.h"
    #include "CDate.h"
    
    
    CDate::CDate()
    {
        m_nYear = 0;
        m_nMonth = 0;
        m_nDay = 0;
    }
    
    
    CDate::~CDate()
    {
    }
    
    int DaysMinus(const CDate& smallDate, const CDate& bigDate)
    {
        if (smallDate.m_nYear > bigDate.m_nYear)
            cout << "smallDate is bigger than bigDate !"<<endl;
        else if(smallDate.m_nYear==bigDate.m_nYear){
            if (smallDate.m_nMonth>bigDate.m_nMonth)
                cout << "smallDate is bigger than bigDate !"<<endl;
            else if (smallDate.m_nMonth==bigDate.m_nYear&&smallDate.m_nDay>bigDate.m_nDay)
                cout << "smallDate is bigger than bigDate !" << endl;
        }
    
        int days = 0;//相差的天数
        for (int i = smallDate.m_nYear + 1; i < bigDate.m_nYear; i++){
            days += 365;
            if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0)//闰年的条件
                days++;
        }
    
        int iSmdays = 0,iBidays = 0;//第一年和最后一年的时间
        bool bSm_Run, bBi_Run;
    
        bSm_Run = ((smallDate.m_nYear % 4 == 0 && smallDate.m_nYear % 100 != 0) || smallDate.m_nYear % 400 == 0);//小年是闰年
    
        switch (smallDate.m_nMonth)
        {
        case 1:iSmdays = smallDate.m_nDay; break;
        case 2:iSmdays = 31 + smallDate.m_nDay; break;
        case 3:iSmdays = 31 + 28 + smallDate.m_nDay + bSm_Run; break;
        case 4:iSmdays = 31 * 2 + 28 + smallDate.m_nDay + bSm_Run; break;
        case 5:iSmdays = 31 * 2 + 28 + 30 + smallDate.m_nDay + bSm_Run; break;
        case 6:iSmdays = 31 * 3 + 28 + 30 + smallDate.m_nDay + bSm_Run; break;
        case 7:iSmdays = 31 * 3 + 28 + 30 * 2 + smallDate.m_nDay + bSm_Run; break;
        case 8:iSmdays = 31 * 4 + 28 + 30 * 2 + smallDate.m_nDay + bSm_Run; break;
        case 9:iSmdays = 31 * 5 + 28 + 30 * 2 + smallDate.m_nDay + bSm_Run; break;
        case 10:iSmdays = 31 * 5 + 28 + 30 * 3 + smallDate.m_nDay + bSm_Run; break;
        case 11:iSmdays = 31 * 6 + 28 + 30 * 3 + smallDate.m_nDay + bSm_Run; break;
        case 12:iSmdays = 31 * 6 + 28 + 30 * 4 + smallDate.m_nDay + bSm_Run; break;
        default:
            break;
        }
    
        //bSm_Run = ((smallDate.m_nYear % 4 == 0 && smallDate.m_nYear % 100 != 0) || smallDate.m_nYear % 400 == 0);//小年是闰年
        bBi_Run = ((bigDate.m_nYear % 4 == 0 && bigDate.m_nYear % 100 != 0) || bigDate.m_nYear % 400 == 0);
            switch (bigDate.m_nMonth){
            case 1:iBidays = bigDate.m_nDay; break;
            case 2:iBidays = 31 + bigDate.m_nDay; break;
            case 3:iBidays = 31 + 28 + bigDate.m_nDay + bBi_Run; break;
            case 4:iBidays = 31 * 2 + 28 + bigDate.m_nDay + bBi_Run; break;
            case 5:iBidays = 31 * 2 + 28 + 30 + bigDate.m_nDay + bBi_Run; break;
            case 6:iBidays = 31 * 3 + 28 + 30 + bigDate.m_nDay + bBi_Run; break;
            case 7:iBidays = 31 * 3 + 28 + 30 * 2 + bigDate.m_nDay + bBi_Run; break;
            case 8:iBidays = 31 * 4 + 28 + 30 * 2 + bigDate.m_nDay + bBi_Run; break;
            case 9:iBidays = 31 * 5 + 28 + 30 * 2 + bigDate.m_nDay + bBi_Run; break;
            case 10:iBidays = 31 * 5 + 28 + 30 * 3 + bigDate.m_nDay + bBi_Run; break;
            case 11:iBidays = 31 * 6 + 28 + 30 * 3 + bigDate.m_nDay + bBi_Run; break;
            case 12:iBidays = 31 * 6 + 28 + 30 * 4 + bigDate.m_nDay + bBi_Run; break;
            default:
                break;
            }
            if (smallDate.m_nYear == bigDate.m_nYear){
                days = iBidays - iSmdays;
            }
            else{
                days = days + 365 + bSm_Run - iSmdays;
                days = days + iBidays;
            }
            return days;
    }
    
    // DATE.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include "CDate.h"
    
    int _tmain(int argc, TCHAR* argv[])
    {
        CDate SmallDate(2007,3,12);
        CDate bigDate(2013,6,16);
        cout << DaysMinus(SmallDate, bigDate) << endl;
        system ("pause");
        return 0;
    }
    
    

    这是原来的一个课堂作业,可能不能满足你的要求,大致就是这么个算法。 如果有用请点击下采纳。

    
    
    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献