![]() |
#2
Vsnow2015-04-19 15:45
#include <iostream>
#include <string> using namespace std; int main() { int year,month,day; puts("请输入年份:"); cin>>year; puts("请输入月份:"); cin>>month; puts("请输入日期:"); cin>>day; int t,days,m; if((year%4==0)||((year%4==0)&&(year%100==0))) t=1; else t=0; if(t==1) { switch(month) { case 1: days=day; m=day%7; case 2: days=31+day; m=days%7; case 3: days=28+31+day; m=day%7; case 4: days=31+28+31+day; m=day%7; case 5: days=30+31+28+31+day; m=day%7; case 6: days=31+30+31+28+31+day; m=day%7; case 7: days=30+31+30+31+28+31+day; m=day%7; case 8: days=31+30+31+30+31+28+31+day; m=day%7; case 9: days=31+31+30+31+30+31+28+31+day; m=day%7; case 10: days=30+31+31+30+31+30+31+28+31+day; m=day%7; case 11: days=31+30+31+31+30+31+30+31+28+31+day; m=day%7; case 12: days=30+31+30+31+31+30+31+30+31+28+31+day; m=day%7; } } else if(t==0) { switch(month) { case 1: days=day; m=day%7; case 2: days=31+day; m=days%7; case 3: days=29+31+day; m=day%7; case 4: days=31+29+31+day; m=day%7; case 5: days=30+31+29+31+day; m=day%7; case 6: days=31+30+31+29+31+day; m=day%7; case 7: days=30+31+30+31+29+31+day; m=day%7; case 8: days=31+30+31+30+31+29+31+day; m=day%7; case 9: days=31+31+30+31+30+31+29+31+day; m=day%7; case 10: days=30+31+31+30+31+30+31+29+31+day; m=day%7; case 11: days=31+30+31+31+30+31+30+31+29+31+day; m=day%7; case 12: days=30+31+30+31+31+30+31+30+31+29+31+day; m=day%7; } } switch(m) { case 0: puts("星期天"); break; case 1: puts("星期一"); break; case 2: puts("星期二"); break; case 3: puts("星期三"); break; case 4: puts("星期四"); break; case 5: puts("星期五"); break; case 6: puts("星期天"); break; } return 0; } |
题目描述
A year and the day of week of the first day in this year will be given to you, you are asked to calculate the day of week of any given date for this year.
输入
The input may contain several test cases.
The first line of each test case is a year (a 4-digit positive integer) and the day of week of the first day in this year (Sun, Mon, Tue, Wed, Thu, Fri, or Sat).
The second line is the given date of this year (in format of integer/integer, denoting Month/Day). You need to calculate the day of week of this date.
Input is terminated by EOF.
输出
For each test case, output the day of week of the given date in this year (i.e. Sun, Mon, Tue, Wed, Thu, Fri, or Sat)
样例输入
2009 Thu
12/25
1996 Mon
12/25
1984 Sun
2/2
样例输出
Fri
Wed
Thu
#include <iostream>
#include <string>
using namespace std;
int main()
{
int y;
string w;
while(cin >> y >> w)
{
int m,d,n,W;
if(w=="Sun") W=0;
else if(w=="Mom") W=1;
else if(w=="Tue") W=2;
else if(w=="Wed") W=3;
else if(w=="Thu") W=4;
else if(w=="Fri") W=5;
else if(w=="Sat") W=6;
scanf("%d/%d",&m,&d);
if(m==1) n=d%7;
else if (m==2) n=(d+31)%7;
else
{
if ((y%4==0&&y%100!=0)||y%400==0)
{
if(m==3) n=(d+31+29)%7;
if(m==4) n=(d+31+29+31)%7;
if(m==5) n=(d+31+29+31+30)%7;
if(m==6) n=(d+31+29+31+30+31)%7;
if(m==7) n=(d+31+29+31+30+31+30)%7;
if(m==8) n=(d+31+29+31+30+31+30+31)%7;
if(m==9) n=(d+31+29+31+30+31+30+31+31)%7;
if(m==10) n=(d+31+29+31+30+31+30+31+31+30)%7;
if(m==11) n=(d+31+29+31+30+31+30+31+31+30+31)%7;
if(m==12) n=(d+31+29+31+30+31+30+31+31+30+31+30)%7;
}
else
{
if(m==3) n=(d+31+28)%7;
if(m==4) n=(d+31+28+31)%7;
if(m==5) n=(d+31+28+31+30)%7;
if(m==6) n=(d+31+28+31+30+31)%7;
if(m==7) n=(d+31+28+31+30+31+30)%7;
if(m==8) n=(d+31+28+31+30+31+30+31)%7;
if(m==9) n=(d+31+28+31+30+31+30+31+31)%7;
if(m==10) n=(d+31+28+31+30+31+30+31+31+30)%7;
if(m==11) n=(d+31+28+31+30+31+30+31+31+30+31)%7;
if(m==12) n=(d+31+28+31+30+31+30+31+31+30+31+30)%7;
}
}
W=W+n;
if(W>7) W=W%7;
else if(W==0) cout << "Sun\n";
else if(W==1) cout << "Mom\n";
else if(W==2) cout << "Tue\n";
else if(W==3) cout << "Wed\n";
else if(W==4) cout << "Thu\n";
else if(W==5) cout << "Fri\n";
else if(W==6) cout << "Sat\n";
}
return 0;
}