感受: for循环一定要好好考虑,有数学公式的别一个一个试。参考了别人的答案才ac。自己的一直超时!!!
<pre name="code" class="cpp"> count=(nian2/4-nian2/100+nian2/400)-(nian1/4-nian1/100+nian1/400);
这个公式,怪怪的。如果先(年2-年1)就会出错
</pre><pre name="code" class="cpp">#include<iostream>
#include<stack>
#include<string>
#include <cstdio>
#include <fstream>
using namespace std;
bool is_leap_year (int year)
{
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
int main()
{
ifstream cin("a.txt");
//ofstream cout("b.txt");
int t;
int n=1;
cin>>t;
while(t--)
{
int count=0;
int head=0,rear=0;
string yue1,yue2,sri1,sri2;
int nian1,nian2,ri1,ri2;
cin>>yue1>>sri1>>nian1;
cin>>yue2>>sri2>>nian2;
ri1=atoi(sri1.substr(0,sri1.length()-1).c_str());
ri2=atoi(sri2.substr(0,sri2.length()-1).c_str());
count=(nian2/4-nian2/100+nian2/400)-(nian1/4-nian1/100+nian1/400);
if(is_leap_year(nian1)&&(yue1=="January"||yue1=="February"))
count++;
if(is_leap_year(nian2)&&((yue2=="January")||(yue2=="February"&&ri2<=28)))
count--;
cout<<"Case #"<<(n++)<<": "<<count<<"\n";
}
}