NYOJ 219 An problem about date
AC代码
#include<stdio.h>
int main()
{int y,m,d;
while(scanf("%d%d%d",&y,&m,&d)!=EOF)
{
if(m==1||m==2)
{
y-=1;
m+=12;
}
printf("%d\n",(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400+1)%7);
}
return 0;
}
超时代码
#include<stdio.h>
int py(int a)
{ int y;
if(a%4==0&&a%100!=0||a%400==0)
y=1;
else y=0;
return y;
}
int pd(int b,int c,int d)
{ int s;
int m[13]={31,28,31,30,31,30,31,31,30,31,30,31};
s=0;
py(b);
if(py(b)==1)
m[1]+=1;
int k;
for(k=0;k<c-1;k++)
s+=m[k];
s+=d;
return s;
}
int main()
{
int y2,m2,d2;
while( scanf("%d%d%d",&y2,&m2,&d2)!=EOF)
{
int y1=1600,m1=1,d1=1,i,s=0,l1,l2,m,t;
if(y2-y1==0)
{ l1= pd(y1,m1,d1);
l2= pd(y2,m2,d2);
//printf("%d %d\n",l1,l2);
l1=l2-l1;
if(l1<0)
l1=-l1;
printf("%d\n",l1);
}
else
{ if(y2-y1<0)
{t=y2;y2=y1;y1=t;}
for(i=y1,m=0;i<y2;i++)
{ if(py(i)==1)
m+=366;
else
m+=365;
}
l1= pd(y1,m1,d1);
l2= pd(y2,m2,d2);
//printf("%d %d\n",l1,l2);
l1=l2-l1;
if(l1<0)
l1=-l1;
//printf("%d\n",l1+m);
m=(l1+m+6)%7;
printf("%d\n",m);
}
}
return 0;
}