Problem D: 我想放假
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 102 Solved: 37
[ Submit][ Status][ Web Board]
Description
本题只需要提交填空部分
#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;
}
Input
第一行输入开学的年月日,以空格隔开;第二行输入本学期的有效天数
Output
输出计算后得到的放假日期,格式为年/月/日。
Sample Input
2008 2 29
140
Sample Output
2008/7/18
代码:
#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)
{
int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
if((c.year%4==0&&c.year%100!=0)||(c.year%400==0))
a[2]=29;
c.day+=day;
while (c.day>a[c.month])
{
c.day-=a[c.month];
c.month++;
if(c.month>12)
{
c.year++;
c.month-=12;
}
a[2]=((c.year%4==0&&c.year%100!=0)||(c.year%400==0))?29:28;
}
return c;
}
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;
}
运行结果: