#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
int month(const string &s)
{
istringstream stream1(s);
string mon;
string days;
int day;
stream1>>mon>>days;
istringstream stream2;
stream2.str(days.substr(0,2));
stream2>>day;
if((mon=="January")||(mon=="February"&&day<29))
return 0; //smaller than 2.29
else
if((mon=="February"&&day==29))
return 1; //equal than 2.29
else
return 2; //larger than 2.29
}
long lastFour(const string &s)
{
auto it=s.end()-4;
int pos=it-s.begin();
string ret=s.substr(pos,4);
istringstream stream1;
stream1.str(ret);
long i;
stream1>>i;
return i;
}
bool isleapYear(int year)
{
if(((year%4==0)&&(year%100!=0))||(year%400==0))
return true;
else
return false;
}
int numLeapYear(const string &s1,const string &s2)
{
long fromYear=lastFour(s1);
long endYear=lastFour(s2);
int sum=0;
for(long i=fromYear;i<=endYear;i++)
{
if(isleapYear(i))
sum++;
}
if(isleapYear(fromYear))
{
if(month(s1)==2) //if day of fromyear larger than 2.29
sum--;
}
if(isleapYear(endYear))
{
if(month(s2)==0) //if day of endyear smaller than 2.29
sum--;
}
return sum;
}
int main()
{
vector<string> years;
int N;
cin>>N;
string year;
cin.clear();
getline(cin,year);
while(getline(cin,year))
years.push_back(year);
//for(auto v:years)
// cout<<v<<endl;
vector<int> results;
int num;
for(int i=0;i<N*2;i++)
{
num=numLeapYear(years[i],years[i+1]);
results.push_back(num);
i++;
}
for(int i=0;i<N;i++)
{
cout<<"Case #"<<i+1<<": "<<results[i]<<endl;
}
return 0;
}
为什么没通过呢
最新推荐文章于 2024-05-22 16:04:10 发布