UVa 12439 February 29
解題:算閏年
Code:
Code:
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
int main()
{
int cas=0;
string line;
cin>>cas;
getline(cin,line);
for(int cc=0;cc<cas;cc++)
{
char date1[100]="",date2[100]="";
char m1[25]="",m2[25]="",dd1[10]="",dd2[10]="";
long long int y1=0,y2=0,d1=0,d2=0;
gets(date1);
gets(date2);
sscanf(date1,"%s %s %lld",m1,dd1,&y1);
sscanf(date2,"%s %s %lld",m2,dd2,&y2);
if(strlen(dd1)==3) d1=(dd1[0]-'0')*10+(dd1[1]-'0');
else if(strlen(dd1)==2) d1=dd1[0]-'0';
if(strlen(dd2)==3) d2=(dd2[0]-'0')*10+(dd2[1]-'0');
else if(strlen(dd2)==2) d2=dd2[0]-'0';
long long int first=0,second=0;
if(strcmp(m1,"January")==0||strcmp(m1,"February")==0)
{
if((y1%4==0 && y1%100!=0)||y1%400==0)
first=y1/4-y1/100+y1/400-1;
else
{
first=y1/4-y1/100+y1/400;
}
}
else first=y1/4-y1/100+y1/400;
if(strcmp(m2,"January")==0||(strcmp(m2,"February")==0)&&d2!=29)
{
if((y2%4==0 && y2%100!=0)||y2%400==0)
second=y2/4-y2/100+y2/400-1;
else second=y2/4-y2/100+y2/400;
}
else second=y2/4-y2/100+y2/400;
int ans=second-first;
printf("Case %d: %lld\n",cc+1,ans);
}
return 0;
}
留言
張貼留言