我想放假

我想放假

时间: 1ms        内存:128M

描述:

小明的弟弟上小学了,每次刚入学就想知道什么时候放假,但是每学期开学的日子和每学期的有效天数都不一样,请你编程帮他计算放假日期。

本题只需要提交填空部分

#include <iostream>
using namespace std;
class Date
{
public:
    void input(int y,int m,int d);
    friend Date operator+(Date &c,int &day);
    void display();
private:
    int year;
    int month;
    int day;
};

void Date::input(int y,int m,int d)
{
    year=y;
    month=m;
    day=d;
}
Date operator+(Date &c,int &day)
{
    /*********************/

              填空部分

    /*********************/
}

void Date::display()
{
    cout<<year<<"/"<<month<<"/"<<day<<endl;
}
int main()
{
    Date date1,date2;
    int y,m,d;
    int day;
    cin>>y>>m>>d;
    date1.input(y,m,d);
    cin>>day;
    date2=date1+day;
    date2.display();
    return 0;
}

输入:

第一行输入开学的年月日,以空格隔开;第二行输入本学期的有效天数

输出:

输出计算后得到的放假日期,格式为年/月/日。

示例输入:

2008 2 29
140

示例输出:

2008/7/18

提示:

参考答案(内存最优[1484]):


#include <iostream> 
using namespace std; 
class Date 
{ 
public: 
    void input(int y,int m,int d); 
    friend Date operator+(Date &c,int &day); 
    void display(); 
private: 
    int year; 
    int month; 
    int day; 
}; 
  
void Date::input(int y,int m,int d) 
{ 
    year=y; 
    month=m; 
    day=d; 
} 
Date operator+(Date &c,int &day) 
{ 
    Date date;
    date=c;
    date.day+=day;
    while(1)
    {
        switch(date.month)
        {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            if(date.day>31)
            {
                ++date.month;
                date.day-=31;
            }
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            if(date.day>30)
            {
                ++date.month;
                date.day-=30;
            }
            break;
        case 2:
            if((date.year%4==0&&date.year%100!=0)||date.year%400==0)
            {
                if(date.day>29)
                {
                    ++date.month;
                    date.day-=29;
                }
            }
            else
            {
                if(date.day>28)
                {
                    ++date.month;
                    date.day-=28;
                }
            }
            break;
        }
        if(date.month>12)
        {
            ++date.year;
            date.month-=12;
        }
        //cout<<"m:"<<date.month<<"d:"<<date.day<<endl;
        if(((date.year%4==0&&date.year%100!=0)||date.year%400==0)&&date.month==2&&date.day<=29)
            break;
        else if(date.month==2&&date.day<=28)
            break;
        else if((date.month==1||date.month==3||date.month==5||date.month==7||date.month==8||date.month==10||date.month==12)&&date.day<=31)
            break;
        else if(date.day<=30)
            break;
    }
    return date;
} 
  
void Date::display() 
{ 
    cout<<year<<"/"<<month<<"/"<<day<<endl; 
} 
int main() 
{ 
    Date date1,date2; 
    int y,m,d; 
    int day; 
    cin>>y>>m>>d; 
    date1.input(y,m,d); 
    cin>>day; 
    date2=date1+day; 
    date2.display(); 
    return 0; 
} 

参考答案(时间最优[0]):


#include <iostream> 
using namespace std; 
class Date 
{ 
public: 
    void input(int y,int m,int d); 
    friend Date operator+(Date &c,int &day); 
    void display(); 
private: 
    int year; 
    int month; 
    int day; 
}; 
  
void Date::input(int y,int m,int d) 
{ 
    year=y; 
    month=m; 
    day=d; 
} 
Date operator+(Date &c,int &day) 
{ 
    Date date;
    date=c;
    date.day+=day;
    while(1)
    {
        switch(date.month)
        {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            if(date.day>31)
            {
                ++date.month;
                date.day-=31;
            }
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            if(date.day>30)
            {
                ++date.month;
                date.day-=30;
            }
            break;
        case 2:
            if((date.year%4==0&&date.year%100!=0)||date.year%400==0)
            {
                if(date.day>29)
                {
                    ++date.month;
                    date.day-=29;
                }
            }
            else
            {
                if(date.day>28)
                {
                    ++date.month;
                    date.day-=28;
                }
            }
            break;
        }
        if(date.month>12)
        {
            ++date.year;
            date.month-=12;
        }
        //cout<<"m:"<<date.month<<"d:"<<date.day<<endl;
        if(((date.year%4==0&&date.year%100!=0)||date.year%400==0)&&date.month==2&&date.day<=29)
            break;
        else if(date.month==2&&date.day<=28)
            break;
        else if((date.month==1||date.month==3||date.month==5||date.month==7||date.month==8||date.month==10||date.month==12)&&date.day<=31)
            break;
        else if(date.day<=30)
            break;
    }
    return date;
} 
  
void Date::display() 
{ 
    cout<<year<<"/"<<month<<"/"<<day<<endl; 
} 
int main() 
{ 
    Date date1,date2; 
    int y,m,d; 
    int day; 
    cin>>y>>m>>d; 
    date1.input(y,m,d); 
    cin>>day; 
    date2=date1+day; 
    date2.display(); 
    return 0; 
} 

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注