Calendar 日历 http://vjudge.net/contest/view.action?cid=49515#problem/I

时间:2022-08-27 13:48:01
第一次做这种题目,感悟就是慢慢改自己的代码有太多种情况了

注意同年同月同日,还有同年不同月不同日,同年同月不同日,不同时注意要交换它们

#include<iostream>
#include<cstdio>
using namespace std;
int month[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int leap(int year)
{
    if(year%400==0||(year%4==0&&year%100!=0))
    return 1;
    else
        return 0;
}
int main()
{
    int year,a,b,year1,b1,a1;
    while(scanf("%d:%d:%d",&year,&a,&b)!=EOF)
    {
        scanf("%d:%d:%d",&year1,&a1,&b1);
        int sum=0;
        if(year>year1)
        {
            int t;
            t=year;year=year1;year1=t;
            t=a;a=a1;a1=t;
            t=b;b=b1;b1=t;
        }


               for(int i=year+1;i<year1;i++)
               {
                   if(leap(i)==1)sum+=366;
                else sum+=365;
               }
                        if(year==year1)
                        {
                            if(a==a1)
                            {
                                if(b==b1)
                                    cout<<0<<endl;
                                else
                                    cout<<b1-b<<endl;
                            }
                            else
                            {
                                if(a>a1)
                                {int t;
                                 t=a;a=a1;a1=t;
                                 t=b;b=b1;b1=t;}
                                  if(leap(year)==1)month[1]=29;
                                else month[1]=28;
                                for(int i=a;i<a1-1;i++)
                                {
                                sum+=month[i];
                                }
                                sum=sum+b1+month[a-1]-b;
                                cout<<sum<<endl;
                            }
                        }
                        else
                        {
                            if(leap(year)==1)month[1]=29;
                            else month[1]=28;
                            for(int i=a;i<12;i++)
                            sum+=month[i];
                            int   k=month[a-1]-b;
                            if(leap(year1)==1)month[1]=29;
                            else month[1]=28;
                            for(int i=0;i<a1-1;i++)
                            sum+=month[i];
                            sum=sum+b1+k;
                            printf("%d\n",sum);
                        }
    }
    return 0;
}