首页 > 试题广场 > Day of Week
[编程题]Day of Week
  • 热度指数:4427 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
We now use the Gregorian style of dating in Russia. The leap years are years with number divisible by 4 but not divisible by 100, or divisible by 400. For example, years 2004, 2180 and 2400 are leap. Years 2004, 2181 and 2300 are not leap. Your task is to write a program which will compute the day of week corresponding to a given date in the nearest past or in the future using today’s agreement about dating.

输入描述:
There is one single line contains the day number d, month name M and year number y(1000≤y≤3000). The month name is the corresponding English name starting from the capital letter.


输出描述:
Output a single line with the English name of the day of week corresponding to the date, starting from the capital letter. All other letters must be in lower case.

Month and Week name in Input/Output:
January, February, March, April, May, June, July, August, September, October, November, December
Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
示例1

输入

9 October 2001
14 October 2001

输出

Tuesday
Sunday

Python四行代码,利用datetime库。

import datetime
while True:
    try:
        month = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October',
                 'November', 'December']
        week = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
        d, m, y = input().split()
        print(week[int(datetime.datetime(int(y), month.index(m) + 1, int(d)).strftime("%w"))])
    except:
        break

看了下面的答案后,发现还可以更简单,"A"直接输出的是Tuesday这种格式,太方便了。于是代码可以缩成三行:

import datetime
while True:
    try:
        month = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October','November', 'December']
        d, m, y = input().split()
        print((datetime.datetime(int(y), month.index(m) + 1, int(d)).strftime("%A")))
    except:
        break

人生苦短,我爱python。

编辑于 2019-04-18 20:51:19 回复(1)
import datetime
print datetime.datetime.strptime(raw_input(), "%d %B %Y").strftime("%A")

Python其实只要两行233

编辑于 2018-02-08 16:37:22 回复(1)
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <cmath>
#include <set>
#include <queue>
 
using namespace std;
map<string, int> mp;
map<int,string> _mp;
string months[] = {"","January","February","March","April","May","June","July","August","September","October","November","December"};
string weekdays[] = {"","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
void init()
{
    for(int i = 1;i<=12;i++)
    {
        mp[months[i]] = i;
    }
    for(int i = 1 ;i<=7;i++)
    _mp[i] = weekdays[i]; 
}
int main()
{
    int d,y;
    string m;
    init();
    while(cin >> d >> m >> y)
    {
        int mm = mp[m];
        if(mm == 1 || mm == 2)
        {
            mm += 12;
            y--;
        }
        int ans = (d+2*mm+3*(mm+1)/5+y+y/4-y/100+y/400)%7 + 1;
        cout << _mp[ans] << endl;
    }
}

发表于 2019-03-06 00:57:18 回复(1)
基姆拉尔森计算公式
W= (d+1+2*m+3*(m+1)/5+y+y/4-y/100+y/400) mod 7
编辑于 2019-02-27 10:23:56 回复(0)
//隐藏条件就是1年1月1日是星期一,把这个时间点设为锚点
//计算输入的日期与锚点之间隔了多少天
//天数对7取余,所得结果就是星期几
#include <cstdio>
#include <cstring>

int month[13][2]={
    {0,0},{31,31},{28,29},{31,31},{30,30},{31,31},{30,30},{31,31},{31,31},
    {30,30},{31,31},{30,30},{31,31}
};
char month_name[13][20]={
    "","January","February","March","April","May","June","July","August",
    "September","October","November","December"
};
char week_name[7][20]={
    "Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"
};
bool isLeap(int year){
    return (year%4==0&&year%100!=0)||(year%400==0);
}
int main(){
    int d,m,y;
    char s[20];
    while(scanf("%d%s%d",&d,s,&y)!=EOF){
        for(m=1;m<=12;m++){
            if(strcmp(s,month_name[m])==0) break;
        }
        int y1=1,m1=1,d1=1,day=1;
        while(y1<y||m1<m||d1<d){
            d1++;
            if(d1==month[m1][isLeap(y1)]+1){
                d1=1;
                m1++;
            }
            if(m1==13){
                m1=1;
                y1++;
            }
            day++;
        }
        printf("%s\n",week_name[day%7]);
    }
    return 0;
}
发表于 2019-02-12 11:17:47 回复(1)
用的基姆拉尔森公式,最开始用的蔡勒公式,但是只能过85%。。。。
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int test(int year)
{
    if(year%400==0||(year%100!=0&&year%4==0))
        return 1;
    else
        return 0;
}
int main(void)
{
    string months[12]={"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
    int day,year,month;
    int c,m,d,y,w;
    string str_m;
    while(cin>>day>>str_m>>year)
    {
        for(int i=0;i<12;i++)
        {
            if(str_m==months[i])
            {
                month=i+1;
                break;
            }
        }
        if(month==1)
        {
            m=13;
            y=year-1;
        }
        else if(month==2)
        {
            m=14;
            y=year-1;
        }
        else
        {
            m=month;
            y=year;
        }
        c=y/100;
        d=day;
        w = (d + 2*m + 3*(m + 1)/5 + y + y/4 - y/100 + y/400 + 1) % 7;
        switch(w){
            case 0:cout<<"Sunday"<<endl;break;
            case 1:cout<<"Monday"<<endl;break;
            case 2:cout<<"Tuesday"<<endl;break;
            case 3:cout<<"Wednesday"<<endl;break;
            case 4:cout<<"Thursday"<<endl;break;
            case 5:cout<<"Friday"<<endl;break;
            case 6:cout<<"Saturday"<<endl;break;
        }
    }
    return 0;
}
发表于 2019-11-23 22:51:48 回复(0)
真实麻烦QAQ,果然Python大法好
#include<iostream>
#include<string>
using namespace std;
int totalDay(int day, string str, int year) {
    int total = 0;
    int totalNum = 0;
    int table[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
    if (str == "January") total = 0;
    else if (str == "February") total = 1;
    else if (str == "March") total = 2;
    else if (str == "April") total = 3;
    else if (str == "May") total = 4;
    else if (str == "June") total = 5;
    else if (str == "July") total = 6;
    else if (str == "August") total = 7;
    else if (str == "September") total = 8;
    else if (str == "October") total = 9;
    else if (str == "November") total = 10;
    else if (str == "December") total = 11;
    if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
        table[1] = 29;
    if (total>0) {
        for (int i = 0; i<total; i++)
            totalNum += table[i];
    }
    return totalNum + day - 1;
}
int main() {
    int year, day;
    string month;
    int origin = 6;//2000年1月1日为周六
    while (cin >> day >> month >> year) {
        int total = 0;
        origin = 6;
        if (year - 2000 >= 0) {
            switch ((year - 2000) % 4) {
            case 0: total = ((year - 2000) / 4)*(366 + 365 * 3); break;
            case 1: total = ((year - 2000) / 4)*(366 + 365 * 3) + 366; break;
            case 2: total = ((year - 2000) / 4)*(366 + 365 * 3) + 366 + 365; break;
            case 3: total = ((year - 2000) / 4)*(366 + 365 * 3) + 366 + 365 * 2; break;
            }
            total += totalDay(day, month, year) - ((year - 2000) / 100 - (year - 2000) / 400);
            origin = (origin + total) % 7;
        }
        else {
            switch ((2000 - year) % 4) {
            case 1: total = ((2000 - year) / 4)*(366 + 365 * 3) + 365; break;
            case 2: total = ((2000 - year) / 4)*(366 + 365 * 3) + 365 * 2; break;
            case 3: total = ((2000 - year) / 4)*(366 + 365 * 3) + 365 * 3; break;
            case 0: total = ((2000 - year) / 4)*(366 + 365 * 3); break;
            }
            total -= totalDay(day, month, year) - ((year - 2000) / 100 - (year - 2000) / 400);
            origin = (origin - total % 7 > 0 ? origin - total % 7 : origin + 7 - total % 7) % 7;
        }
        switch (origin) {
        case 0: cout << "Sunday"<<endl; break;
        case 1: cout << "Monday"<<endl; break;
        case 2: cout << "Tuesday"<<endl; break;
        case 3: cout << "Wednesday"<<endl; break;
        case 4: cout << "Thursday"<<endl; break;
        case 5: cout << "Friday"<<endl; break;
        case 6: cout << "Saturday"<<endl; break;
        }
    }
}

发表于 2019-02-10 21:48:05 回复(0)
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
char  mon[][20]={" ","January","February","March","April","May","June","July","August","September","October","November","December"};
char  week[][20]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
bool leap(int y){
  if(y%400==0||(y%4==0&&y%100!=0))return true;
  else return false;
}
int days(int y,int m,int d){
  int sum=0;
  for(int i=1;i<y;++i){
    if(leap(i))sum+=366;
    else sum+=365;
    }
  if(leap(y))day[2]=29;
  else day[2]=28;
  for(int j=1;j<m;++j){
    sum+=day[j];
  }
  sum+=d;
  return sum;
}
int main(){
  int y,m,d;
  char month[20];

  while(cin>>d>>month>>y){
    for(int i=1;i<13;++i){

        if(strcmp(month,mon[i])==0){
           m=i;
           break;
        }
    }
    int cnt1=days(y,m,d);
    int cnt2=days(2018,3,7);
    int del=cnt1-cnt2;
    del=del%7;
    cout<<week[(del+3)%7];
  }
  return 0;
}

发表于 2018-03-07 01:52:22 回复(1)
#include<stdio.h>
#include<string.h>
#define ISYEAR(x) x%100!=0&&x%4==0||x%400==0?1:0
int dateOfMonth[13][2]={
    0,0,
    31,31,
    28,29,
    31,31,
    30,30,
    31,31,
    30,30,
    31,31,
    31,31,
    30,30,
    31,31,
    30,30,
    31,31
};
struct Date{
    int Year;
    int Month;
    int Day;
    void NextDay(){
        Day++;
        if(Day>dateOfMonth[Month][ISYEAR(Year)]){
            Day=1;
            Month++;
            if(Month>12){
                Month=1;
                Year++;
            }
        }
    }
};
int buf[3001][13][32];
char MonthName[13][20]={
    " ",
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December"
};
char WeekName[7][20]={
    "Sunday", 
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday"
};
int main(){
    Date tmp;
    tmp.Year=0;
    tmp.Month=1;
    tmp.Day=1;
    int cnt=0;
    while(tmp.Year!=3001){
        buf[tmp.Year][tmp.Month][tmp.Day]=cnt;
        tmp.NextDay();
        cnt++;
    }
    int d,m,y;
    char s[20];
    while(scanf("%d%s%d",&d,s,&y)!=EOF){
        for(m=1;m<13;m++){
            if(strcmp(MonthName[m],s)==0)
                break;
        }
    }
    int days=(buf[y][m][d]-buf[2012][7][16])+1;
    printf("%s",WeekName[(days%7+7)%7]);    
}


发表于 2018-03-05 17:17:20 回复(0)
//王道机试指南解法
#include<iostream>
#include<cstring>
#define ISLEAP(x)  (x%4==0&&x%100!=0)||x%400==0?1:0
int YofM[13][2] = {
    0,0,31,31,28,29,31,31,30,30,31,31,30,30,
    31,31,31,31,30,30,31,31,30,30,31,31
};
struct Data {
    int data;
    int month;
    int year;
    Data() {
        data = 1; month = 0; year = 0;
    }
    void nextdata() {
        data++;
        if (data>YofM[month][ISLEAP(year)]) {
            month++; data = 1;
            if (month>12)
            {
                year++; month = 1;
            }
        }//if
    }
};
char monthName[13][20] = {
    "","January","February","March","April","May","June","July",
    "August","September","October","November","December"
};
char weekName[7][20] = {
    "Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"
};
int Daycount[3001][13][32];
int main() {
    Data D;
    int num = 0;
    while (D.year <= 3000) {
        Daycount[D.year][D.month][D.data] = num;
        D.nextdata();
        num++;
    }//while
    int d, m, y;
    char s[20];
    while (std::cin >> d >> s >> y) {
        for (m = 1; m<13; m++) {
            if (strcmp(s, monthName[m]) == 0) { break; }
        }//for
        int i = Daycount[y][m][d] - Daycount[2018][2][25];
        std::cout << weekName[(i % 7 + 7) % 7];
    }//while
    return 0;
}

编辑于 2018-03-04 23:35:10 回复(0)
from datetime import datetime
Month = {'May': 5, 'December': 12, 'October': 10, 'August': 8,
         'July': 7, 'February': 2, 'January': 1, 'September': 9,
         'March': 3, 'April': 4, 'November': 11, 'June': 6}
try:
    while 1:
        k = raw_input().split()
        print datetime(int(k[2]),Month[k[1]],int(k[0])).strftime('%A').capitalize()
except:
    pass

发表于 2016-12-25 16:52:08 回复(0)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define IFYEAP(x) x%100!=0 && x%4==0 || x%400==0 ? 1:0
int yearDay[13][2] = {
    0, 0,
    31, 31,
    28, 29,
    31, 31,
    30, 30,
    31, 31,
    30, 30,
    31, 31,
    31, 31,
    30, 30,
    31, 31,
    30, 30,
    31, 31
};

char weekInfo[7][10] = {"Monday",
"Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "Sunday" };

char monthInfo[13][20] = { "0",
"January", "February",
"March", "April",
"May", "June",
"July", "August",
"September", "October",
"November", "December" };

int todayIndex = 2;
typedef struct dateInfo {
    int day;
    int month;
    int year;
    void nextDate() {
        day++;
        if (day > yearDay[month][IFYEAP(year)]) {
            month++;
            day = 1;
            if (month > 12) {
                month = 1;
                year++;
            }
        }
    }
}DateInfo;

int main() {
    int d2;
    char *m2_char = (char*)malloc(sizeof(char) * 10);
    int y2;
    scanf("%d%s%d", &d2, m2_char, &y2);
    int m2;
    for (int i = 0; i<13; i++) {
        if (!strcmp(m2_char, monthInfo[i])) {
            m2 = i;
        }
    }
    DateInfo date2, date1;
    date2.day = d2;
    date2.month = m2;
    date2.year = y2;
    date1.day = 31;
    date1.month = 7;
    date1.year = 2019;
    int index = -1;
    if (date1.year > date2.year) index = 2;
    else if (date1.year < date2.year) index = 1;
    else {
        if (date1.month > date2.month) index = 2;
        else if (date1.month < date2.month) index = 1;
        else {
            if (date1.day > date2.day) index = 2;
            else if (date1.day < date2.day) index = 1;
        }
    }
    switch (index) {
    case -1: break;
    case 1:
        while (date1.year != date2.year || date1.month != date2.month || date1.day != date2.day) {
            date1.nextDate();
            todayIndex = (todayIndex + 1) % 7;
        }
        break;
    case 2:
        while (date1.year != date2.year || date1.month != date2.month || date1.day != date2.day) {
            date2.nextDate();
            todayIndex = (todayIndex + 1) % 7;
        }
        break;
    }
    printf("%s", weekInfo[todayIndex]);
}
发表于 2019-07-31 11:29:23 回复(0)
#include<bits/stdc++.h>
using namespace std;
int main()
{
    string MONTH[13] = {"","January", "February", "March", "April","May", "June", "July", "August","September","October","November","December"};
    string WEEKDAY[8] ={"","Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday","Sunday"};
    map<string,int>map1;
    map<int,string>map2;
    for(int i=1;i<8;i++)
        map2[i] = WEEKDAY[i];
    for(int i=1;i<13;i++)
        map1[MONTH[i]] = i ;
    int day,year;
    string month;
    while(cin>>day>>month>>year)
    {
        int mon = map1[month];
        if(mon==1||mon==2)
        {
            mon+=12;
            year-=1;
        }
        int res = (day+2*mon+3*(mon+1)/5+year+year/4-year/100+year/400)%7 + 1;
        cout<<map2[res]<<endl;
    }
    return 0;
}
基姆拉尔森计算公式
W= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400) mod 7

在公式中d表示日期中的日数,m表示月份数,y表示年数。


发表于 2019-04-15 23:35:39 回复(0)
#include<stdio.h>
#include<string.h>
int month[13][2] =
{
    {0,0},//0
    {31,31},//1
    {28,29},//2
    {31,31},//3
    {30,30},//4
    {31,31},//5
    {30,30},//6
    {31,31},//7
    {31,31},//8
    {30,30},//9
    {31,31},//10
    {30,30},//11
    {31,31}//12
};
int isR(int n)
{
    if(n%4==0&&n%100!=0||n%400==0)
        return 1;
    return 0;
}
int main()
{
    char mon[13][100] = {"","January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
    char week[8][100] = {"","Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
    int y,d,m,w;
    char temp[100];
    while(scanf("%d %s %d",&d, temp, &y)!=EOF)
    {
        int y0 = 2001, m0 = 10, d0 = 9, w0 = 3;
        for(int i=1; i<13; i++)
        {
            if(strcmp(temp,mon[i])==0)
            {
                m = i;
                break;
            }
        }
        int flag = 0;
        while(y0 < y || m0 < m || d0 < d)
        {
            flag = 1;
            d0++;
            w0++;
            if(d0==month[m0][isR(y0)]+1)
            {
                d0 = 1;
                m0++;
            }
            if(m0==13)
            {
                y0++;
                m0 = 1;
            }
            if(w0==8)
                w0 = 1;
        }
        if(flag==1 || y==y0&&m==m0&&d==d0)
        {    flag = 1;
            printf("%s\n", week[w0]);
        }
    
        while(y < y0 || m < m0 || d < d0)
        {
             d++;
             w0--;
             if(d==month[m][isR(y)]+1)
             {
                d=1;
                m++;
            }
            if(m==13)
            {
                m=1;
                y++;
            }
            if(w0==0)
                w0 = 7;
        }
        if(flag==0)
            printf("%s\n", week[w0]);
        
    }

}
发表于 2019-03-27 09:38:58 回复(0)
很锻炼硬编码能力的“水题”啊!写的好长🙄

#include<cstdio>
#include<string>
#include<iostream>

using namespace std;

// 2019.01.01 Tuesday

const int dayOfMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const string date[7] = {"Tuesday", "Wednesday", "Thursday", "Friday", 
                        "Saturday", "Sunday", "Monday"};

bool isLeapYear(int n) {
    if (n % 4 == 0) {
        if (n % 100) {
            return true;
        } else {
            if (n % 400) {
                return false;
            } else {
                return true;
            }
        }
    }
    return false;
}

int main() {
    int day = 0, year = 0;
    char month[10];
    while (~scanf("%d %s %d", &day, month, &year)) {
        int days = 0;
        bool beforeFeb = false;
        int monthNum = 0;
        switch (month[0]) {
            case 'J':
                if (month[1] == 'a') {
                    monthNum = 1;
                } else if (month[2] == 'n') {
                    monthNum = 6;
                } else {
                    monthNum = 7;
                }
                break;
            case 'F':
                monthNum = 2;
                break;
            case 'M':
                if (month[2] == 'r') {
                    monthNum = 3;
                } else {
                    monthNum = 5;
                }
                break;
            case 'A':
                if (month[1] == 'p') {
                    monthNum = 4;
                } else {
                    monthNum = 8;
                }
                break; 
            case 'S':
                monthNum = 9;
                break;
            case 'O':
                monthNum = 10;
                break;
            case 'N':
                monthNum = 11;
                break;
            default:
                monthNum = 12;
        }
        beforeFeb = monthNum <= 2;
        if (year < 2019) {
            days += (2019 - year - 1) * 365;
            for (int i = year + 1; i < 2019; ++i) {
                if (isLeapYear(i)) {
                    ++days;
                }
            }
            for (int i = monthNum; i < 12; ++i) {
                days += dayOfMonth[i];
            }
            days += dayOfMonth[monthNum - 1] - day + 1;
            if (beforeFeb && isLeapYear(year)) {
                ++days;
            }
        } else {
            days += (year - 2019) * 365;
            for (int i = 2019; i < year; ++i) {
                if (isLeapYear(i)) {
                    ++days;
                }
            }
            for (int i = 0; i < monthNum - 1; ++i) {
                days += dayOfMonth[i];
            }
            days += day;
            if (!beforeFeb && isLeapYear(year)) {
                ++days;
            }
        }
        --days;
        cout << date[days % 7] << endl;
    }
    return 0;
}

编辑于 2019-03-10 08:56:48 回复(0)
呜呜呜菜b只会一个一个地数
1.通过hashTable和map建立月份、星期的string与int的映射
2.一个一个地数,到边界时修改日期,到达指定日期即可知道是星期几。
#include <cstdio>
#include <string>
#include <map>
#include <iostream>
using namespace std;
string monthName[13]={"December", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
string weekName[8]={"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
map<string,int> monthNum;
map<string,int> weekNum;
int notleapDays[13]={31,31,28,31,30,31,30,31,31,30,31,30,31};
int leapDays[13]={31,31,29,31,30,31,30,31,31,30,31,30,31};
struct Date
{
    int year,month,day,week;
};
void init()
{
    for(int i=1;i<=12;i++)
        monthNum[monthName[i]]=i;
    for(int i=1;i<=7;i++)
        weekNum[weekName[i]]=i;
}
bool isLeap(int year)
{
    if((year%4==0&&year%100!=0)||year%400==0)
        return true;
    return false;
}
//-1:calday是未来 0:calday是今天 1:calday是过去 
int compare(Date today,Date calday)
{
    if(today.year<calday.year)
        return -1;
    else if(today.year>calday.year)
        return 1;
    else
    {
        if(today.month<calday.month)
            return -1;
        else if(today.month>calday.month)
            return 1;
        else
        {
            if(today.day<calday.day)
                return -1;
            else if(today.day>calday.day)
                return 1;
            else
                return 0;
        }
    }
}
//calday是过去
int getWeek1(Date today,Date calday)
{
    while(today.day!=calday.day||today.month!=calday.month||today.year!=calday.year)
    {
        today.day--;
        if(today.day==0)
        {
            today.month--;
            if(today.month==0)
            {
                today.month=12;
                today.year--;
            }
            if(isLeap(today.year)==true)
                today.day=leapDays[today.month];
            else
                today.day=notleapDays[today.month];
        }
        today.week--;
        if(today.week==0)
            today.week=7;
    }
    return today.week;
}
//calday是未来
int getWeek2(Date today,Date calday)
{
    while(today.day!=calday.day||today.month!=calday.month||today.year!=calday.year)
    {
        today.day++;
        if((isLeap(today.year)==true&&today.day>leapDays[today.month])
            ||(isLeap(today.year)==false&&today.day>notleapDays[today.month]))
        {
            today.day=1;
            today.month++;
            if(today.month==13)
            {
                today.month=1;
                today.year++;
            }
        }
        today.week++;
        if(today.week==8)
            today.week=1;
    }
    return today.week;
}
int main()
{
    init();
    Date today,calday;
    today.day=7;today.month=3;today.year=2019;today.week=4;
    while(scanf("%d",&calday.day)!=EOF)
    {
        string str;
        cin>>str;
        calday.month=monthNum[str];
        scanf("%d",&calday.year);
        if(compare(today,calday)==1)
            //calday是过去
            calday.week=getWeek1(today,calday);
        else if(compare(today,calday)==-1)
            //calday是未来
            calday.week=getWeek2(today,calday);
        else
            calday.week=today.week;
        cout<<weekName[calday.week]<<endl;
    }
    return 0;
}

发表于 2019-03-07 11:25:52 回复(0)
#include <stdio.h>
#include <string.h>
#define leapyear(x) x%100!=0&&x%4==0||x%400==0? 1:0
#define today 1    //运行当天是周几
int dayOfMonth[13][2]={0,0,31,31,28,29,31,31,30,30,31,31,30,30,31,31,31,31,30,30,31,31,30,30,31,31};
int buf[3001][13][32];
char weekname[7][10]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
char monthname[13][10]={"","January","February","March","April","May","June","July","August","September","October","November","December"};

typedef struct Date{
    int year;
    int month;
    int day;
    void nextDay(){
        day++;
        if(day>dayOfMonth[month][leapyear(year)]){
            day=1;
            month++;
            if(month>12){
                month=1;
                year++;
            }
        }
    }
};
int main(){
    Date init;
    int d=0;        //日期差
    init.day=1;
    init.month=1;
    init.year=0;
    while(init.year!=3001){
        buf[init.year][init.month][init.day]=d;
        init.nextDay();
        d++;
    }
    int year,day;
    char s[10];
    while(scanf("%d %s %d",&day,s,&year)!=EOF){
        int month;
        for(month=1;month<=12;month++){
            if(strcmp(monthname[month],s)==0) break;
        }
        int d1=buf[2019][2][25];        //今天离初始日期差
        int d2=buf[year][month][day];        //目标日期离初始日期差
        int index;
        if(d1>d2){
            int k=(d1-d2)%7;            //今天比目标日期后,往前推算星期几
            index=(today-k+7)%7;
        }else {
            int k=(d2-d1)%7;
            index=(today+k+7)%7;
        }
        printf("%s\n",weekname[index]);
    }
    return 0;
}

发表于 2019-02-25 16:50:23 回复(0)

参考别人的代码,Scanner改为BufferedReaderif改为HashMap,时间快了三分之一(21ms)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
    static String[] week = new String[] { 
            "Sunday", "Monday", "Tuesday", "Wednesday", 
            "Thursday", "Friday", "Saturday" };
    static int[] dayOfMonth = new int[] { 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    static List<Integer> dayOfYear = new ArrayList<>();
    static Map<String, Integer> monthMap = new HashMap<>();
    static {
        monthMap.put("January", 1);
        monthMap.put("February", 2);
        monthMap.put("March", 3);
        monthMap.put("April", 4);
        monthMap.put("May", 5);
        monthMap.put("June", 6);
        monthMap.put("July", 7);
        monthMap.put("August", 8);
        monthMap.put("September", 9);
        monthMap.put("October", 10);
        monthMap.put("November", 11);
        monthMap.put("December", 12);
    }

    public static void main(String[] args) throws IOException {
        dayOfYear.add(365);
        int today = getTotalDayCount(2019, 2, 24);

        String line;
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        while ( (line=reader.readLine()) != null ) {
            String[] param = line.split(" ");
            int day = Integer.parseInt(param[0]);
            int month = monthMap.get(param[1]);
            int year = Integer.parseInt(param[2]);
            int curDay = getTotalDayCount(year, month, day);

            if (today >= curDay) {
                System.out.println(week[(7 - (today - curDay) % 7) % 7]);
            } else {
                System.out.println(week[(curDay - today) % 7]);
            }
        }
        reader.close();
    }

    static int getTotalDayCount(int year, int month, int day) {
        for (int i = dayOfYear.size(); i < year; i++) {
            dayOfYear.add(dayOfYear.get(i - 1) + getDayCount(i));
        }
        int curDayCount = dayOfYear.get(year - 1);
        for (int i = 0; i < month - 1; i++) {
            curDayCount += dayOfMonth[i];
            if (i == 1) {
                if (isLeap(year)) {
                    curDayCount += 29;
                } else {
                    curDayCount += 28;
                }
            }
        }
        curDayCount += day;
        return curDayCount;
    }

    static boolean isLeap(int year) {
        return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
    }

    static int getDayCount(int year) {
        if (isLeap(year)) return 366;
        return 365;
    }
}
发表于 2019-02-24 11:21:26 回复(0)
#include<stdio.h>
#include<string.h>
using namespace std;
#define ISYEAP(x) x%100!=0&&x%4==0 || x%400==0 ?1:0
int dayOfMonth[13][2]={
 0,0,
 31,31,
 28,29,
 31,31,
 30,30,
 31,31,
 30,30,
 31,31,
 31,31,
 30,30,
 31,31,
 30,30,
 31,31
};
struct Date{
 int day;
 int month;
 int year;
 void nextDay(){
  day++;
  if(day>dayOfMonth[month][ISYEAP(year)]){
   day=1;
   month++;
   if(month>12){
    month=1;
    year++;
   }
  } 
 }
}; 
int buf[5001][13][32];
char month[13][20]={"","January", "February", "March", "April", "May", "June", "July", "August", "September", 
      "October", "November", "December"};
char week[7][20]={"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
int main(){
 Date tmp;
 tmp.day=1;
 tmp.month=1;
 tmp.year=0;
 int idx=1;
 while(tmp.year!=5001){
  buf[tmp.year][tmp.month][tmp.day]=idx++;
  tmp.nextDay();
 }
 int d,y;
 char s[20];
 while(~scanf("%d%s%d",&d,s,&y)){
  int m;//记录month 
  for(m=1;m<=12;m++){
   if(strcmp(month[m],s)==0)
    break;
  }
  int days=buf[y][m][d]-buf[2019][2][21]+4;
  puts(week[(days%7+7)%7]);
 }
 return 0;
}

发表于 2019-02-21 11:16:08 回复(0)
  • 自行判断闰年,然后以2月的一个星期日为基准天数,进行计算

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import java.util.Scanner;
    public class DayOfWeek {
    
      private static int[] times = {
              31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
      };
      private static String[] months = {
              "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
      };
      private static String[] days = {
              "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
      };
    
      public static void main(String[] args) {
          List<String> inList = new ArrayList<String>();
          String in;
          Scanner sc = new Scanner(System.in);
          while (sc.hasNextLine()) {
              in = sc.nextLine();
              if (in.equals("")) {
                  break;
              }
              inList.add(in);
          }
    
          for (String res :
                  inList) {
              String[] strs = res.split(" ");
              int day = Integer.parseInt(strs[0]);
              int month = Arrays.asList(months).indexOf(strs[1]) + 1;
              int year = Integer.parseInt(strs[2]);
              OwnDate date = new OwnDate(year, month, day);
              OwnDate flagDate = new OwnDate(2019, 2, 24);
              int diff;
              boolean fl = flag(date, flagDate);
              if (fl) {
                  diff = (getValue(date, flagDate) - 1) % 7;
              } else {
                  diff = (getValue(flagDate, date) - 1) % 7;
              }
    //            int diff = (getValue(date, new OwnDate(2001, 10, 14)) - 1) % 7;
              if (fl) {
                  System.out.println(days[7 - diff]);
              } else {
                  System.out.println(days[diff]);
              }
          }
    
      }
    
      private static class OwnDate {
          int year;
          int month;
          int day;
    
          public OwnDate(int year, int month, int day) {
              this.year = year;
              this.month = month;
              this.day = day;
          }
    
          public OwnDate() {
          } 
      }
    
      private static boolean isFlag(int year) {
          return (year % 4 == 0 && year % 100 != 0 || year % 100 == 0 && year % 400 == 0);
      }
    
      private static int getValue(OwnDate date1, OwnDate date2) {
          int res = 1;
          int diff = 0;
          int i;
          for (i = date1.year; i < date2.year; i++) {
              res += isFlag(i) ? 366 : 365;
          }
    
          if (date1.month < date2.month) {
              for (int j = date1.month - 1; j < date2.month - 1; j++) {
                  diff += times[j];
                  if (isFlag(i) && j == 1) {
                      diff++;
                  }
              }
              res += diff;
          } else if (date1.month > date2.month) {
              for (int j = date2.month - 1; j < date1.month - 1; j++) {
                  diff += times[j];
                  if (isFlag(i) && j == 1) {
                      diff++;
                  }
              }
              res -= diff;
          }
    
          res += (date2.day - date1.day);
    
          return res;
      }
    
      private static boolean flag(OwnDate date1, OwnDate date2) {
          boolean flag = false;
          if (date1.year < date2.year) {
              flag = true;
          } else if (date1.year == date2.year) {
              if (date1.month < date2.month) {
                  flag = true;
              } else if (date1.month == date2.month) {
                  if (date1.day < date2.day) {
                      flag = true;
                  }
              }
          }
          return flag;
      }
    }
编辑于 2019-02-19 17:13:44 回复(0)