1199: 英雄無敵3(2)
時間限制: 1 Sec 內存限制: 128 MB提交: 351 解決: 73
[提交][狀態][討論版]
題目描述
英雄無敵3中的獸人覺得人類的星期制度很好,要模仿這個制度,但他們大多都很愚蠢,他們搞不明白人類的星期制度。雖然他們出了很多題目(給出日期,輸出星期幾),但不知道答案,作爲獸人的人類朋友,你可以幫助他們嗎?(爲了表示方便,0代表星期天,1代表星期一,2代表星期二,3代表星期三,4代表星期四,5代表星期五,6代表星期六)
輸入
第一行輸入一個數t,代表題目數。
每個題目有3個正整數分別爲y(0<y<=2013), m(0=<m<=20), d(0<=d<=40)分別代表年,月,日。
輸出
對於每組案例,輸出一個整數,代表星期幾(當然由於獸人很愚蠢,他們給出的日期有可能是不存在的,對於這種情況,請輸出-1)
樣例輸入
12013 4 25
樣例輸出
4
提示
來源
經典的判斷周幾的題目,比較坑的時他給的時間會不存在,而且這個時間會出現0 月或0日的情況。
運用基姆拉爾森計算公式比較方便
下面是基姆拉爾森計算公式的代碼。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
int dayofweek(int y,int m,int d)
{
static int t[]={0,3,2,5,0,3,5,1,4,6,2,4};
y-=m<3;
return (y+y/4-y/100+y/400+t[m-1]+d)%7;
}
下面是題目代碼#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
int dayofweek(int y,int m,int d)
{
static int t[]={0,3,2,5,0,3,5,1,4,6,2,4};
y-=m<3;
return (y+y/4-y/100+y/400+t[m-1]+d)%7;
}
int main()
{
int moth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int t;
scanf("%d",&t);
while(t--)
{
moth[2]=28;
int y,m,d,sum=0;
scanf("%d %d %d",&y,&m,&d);
if((y%4==0&&y%100!=0)||y%400==0){
moth[2]=29;
}
if(m<1||m>12||moth[m]<d||d<1){printf("-1\n");continue;}
sum=d+(y-1)*365+(y-1)/4-(y-1)/100+(y-1)/400;
sum=dayofweek(y,m,d);
printf("%d\n",sum);
}
return 0;
}