- OO1-6 日期时间类DateTime
- 2019-09-16 20:52:10 @
显示一个wrong answer,我好奇是哪个数据没法得出正确结果
#include<iostream>
using namespace std;
class DateTime
{
private:
int year;
int month;
int day;
int hour;
int minute;
int second;
public:
int get_time()
{
return hour*3600+minute*60+second;
}
void set_datetime(int y,int m,int d,int h,int min,int sec)
{
year=y;
month=m;
day=d;
hour=h;
minute=min;
second=sec;
}
int is_leap_year()
{
if(year%400==0 || (year%4==0&&year%100!=0))
return 1;
else
return 0;
}
int is_leap_year(int i)
{
if(i%400==0 || (i%4==0&&i%100!=0))
return 1;
else
return 0;
}
int mid_year(int large_year)//large_year>=year
{
int sum=365*(large_year-year-1);//若同年,为-365,减去了till_end和till_start中多出的天数
for(int i=year+1;i<large_year;i++)
{
if(is_leap_year(i))
sum++;
}
return sum;
}
/*
//int i;
int sum=0;
int Month[13]={0,31,28+is_leap_year(i),31,30,31,30,31,31,30,31,30,31};
if(large_year-year<=1)//相等,算天数月份差,差一年,只需要用到till_end和till_start函数
return 0;
for(int i=year+1;i<large_year;i++)
{
for(int j=1;j<=12;j++)
{
sum=sum+Month[j];
}
}
return sum;
*/
//算整的年的天数
int till_end()
{
int sum=0;
int Month[13]={0,31,28+is_leap_year(),31,30,31,30,31,31,30,31,30,31};
if(month==12)
return 31-day;
//return Month[month]-day;
for(int i=month+1;i<=12;i++)
{
sum=sum+Month[i];
}
return sum+Month[month]-day;
}
int till_start()
{
//int i;
int sum=0;
int Month[13]={0,31,28+is_leap_year(),31,30,31,30,31,31,30,31,30,31};
if(month==1)
return day;
for(int i=1;i<month;i++)
{
sum=sum+Month[i];
}
return sum+day;
}
int get_hour()
{
return hour;
}
int get_minute()
{
return minute;
}
int get_second()
{
return second;
}
int get_year()
{
return year;
}
};//实现思路是先算出中间间隔的天数*3600,再加减秒数
int main()
{
DateTime datetime1,datetime2;
int y1,m1,d1,h1,min1,sec1;
int y2,m2,d2,h2,min2,sec2;
cin>>y1>>m1>>d1>>h1>>min1>>sec1;
cin>>y2>>m2>>d2>>h2>>min2>>sec2;
datetime1.set_datetime(y1,m1,d1,h1,min1,sec1);
datetime2.set_datetime(y2,m2,d2,h2,min2,sec2);
cout<<(datetime1.till_end()+datetime1.mid_year(datetime2.get_year())+datetime2.till_start())*3600*24+(3600*24-datetime1.get_time())-(3600*24-datetime2.get_time())<<endl;
//cout<<datetime1.till_end()+datetime1.mid_year(datetime2.get_year())+datetime2.till_start()<<endl;
system("pause");
return 0;
}
0 条评论
目前还没有评论...
信息
- ID
- 1008
- 难度
- 3
- 分类
- (无)
- 标签
- 递交数
- 266
- 已通过
- 130
- 通过率
- 49%
- 被复制
- 9
- 上传者