首页 > 试题广场 >

Day of Week

[编程题]Day of Week
  • 热度指数:22986 时间限制: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 2005, 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
//隐藏条件就是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 回复(3)
基姆拉尔森计算公式
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)

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 回复(2)
#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 回复(10)
import datetime
print datetime.datetime.strptime(raw_input(), "%d %B %Y").strftime("%A")

Python其实只要两行233

编辑于 2018-02-08 16:37:22 回复(2)
// java
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Map<Object, Object> map = new HashMap<>();
        map.put("January", 1);
        map.put("February", 2);
        map.put("March", 3);
        map.put("April", 4);
        map.put("May", 5);
        map.put("June", 6);
        map.put("July", 7);
        map.put("August", 8);
        map.put("September", 9);
        map.put("October", 10);
        map.put("November", 11);
        map.put("December", 12);

        map.put(7, "Sunday");
        map.put(1, "Monday");
        map.put(2, "Tuesday");
        map.put(3, "Wednesday");
        map.put(4, "Thursday");
        map.put(5, "Friday");
        map.put(6, "Saturday");

        Scanner input = new Scanner(System.in);
        String[] split = input.nextLine().split(" ");
        int day = Integer.parseInt(split[0]);
        int month = (int) map.get(split[1]);
        int year = Integer.parseInt(split[2]);
        LocalDate date = LocalDate.of(year, month, day);
        DayOfWeek ofWeek = date.getDayOfWeek();

        System.out.println(map.get(ofWeek.getValue()));
    }
}
=========================
// c++
#include<bits/stdc++.h>
using namespace std;
bool isLeapYear(int year) {
    return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
}

int daysOfYear(int year) {
    if(isLeapYear(year)) return 366;
    return 365;
}


int daytab[2][13] = {
    {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
    {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};

vector<string> months = {"January","February","March","April","May","June","July","August","September","October","November","December"};
vector<string> weekdays = {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};

int main() {
    int year, month, day;
    //char monthStr[20];
    string monthStr;
    //scanf("%2d %s %4d", &day, monthStr, &year);
    while( cin >> day >> monthStr >> year) {
        //cout << day << monthStr << year;
        for(int i = 0; i < months.size(); i++) {
            if(monthStr == months[i]) {
               month = i;
               break;
            }
        }
        int y = year;
        long long allDay = 0;
        // 计算从公元1年1月1号到当前的天数
        for(int i = 1; i < year; i++) {
            allDay += daysOfYear(i);
        }
        for(int i = 0; i <= month; i++) {
            allDay += daytab[isLeapYear(year)][i];
        }
        allDay += day - 1;
        cout << weekdays[allDay % 7] << endl;
    }
}



编辑于 2020-02-18 10:46:55 回复(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)
#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 回复(2)
#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)
模仿大佬们的写法,自己写了一遍,感觉这样比较好理解
//方法二:这里使用比较简洁的做法,已知1年1月1日是星期一,则只要计算目标年份与1年1月1日的距离日期再对7取余即可
#include<iostream>
#include<stdio.h>
#include<string>

using namespace std;

//用来映射月份
string months[12] = {"January", "February", "March", "April", "May", "June",
                    "July", "August", "September", "October", "November", "December"};
int monthday[2][12] = {
                          {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
                          {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
                      };


//自定义一个函数用来判断是否为闰年
bool isleapyear(int year) {
    if ((year % 4 == 0 && year % 100 != 0 )|| year % 400 == 0)
        return true;
    else
        return false;
}

string weekday[7] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
                     "Saturday", "Sunday"};


int main() {
    int n;//用来计算自1年1月1日的距离天数
    int day,year; 
    string month;
    int mon;
    int week_day=0;
    while (cin >> day >> month >> year) {
        n=0;
        for (int i = 1; i < year; i++) {
            n += isleapyear(i) ? 366 : 365;
        }
        for (int i = 0; i < 12; i++) {
            if (months[i] == month)
                mon = i; //得到第几个月
        }
        for (int i=0;i<mon;i++){
            n+=monthday[isleapyear(year)][i];
        }
        n+=(day-1);
        week_day=n%7;
        cout<<weekday[week_day]<<endl;
        }
}



发表于 2024-01-18 12:40:07 回复(0)
java版本
import java.util.*;
public class Main{
	static int months[][]= {
			{0,31,28,31,30,31,30,31,31,30,31,30,31},
			{0,31,29,31,30,31,30,31,31,30,31,30,31}
	};
	static int isLeapYear(int year){ 
	    if(year%4==0&&year%100!=0||year%400==0){
	        return 1;
	    }return 0;
	}
	static int getMonth(String m){ 
		Map<String,Integer>map=new HashMap<>();
		map.put("January", 1);
		map.put("Febrary", 2);
		map.put("March", 3);
		map.put("April", 4);
		map.put("May", 5);
		map.put("June", 6);
		map.put("July", 7);
		map.put("August", 8);
		map.put("September", 9);
		map.put("October", 10);
		map.put("November", 11);
		map.put("December", 12);
		int month=map.get(m);
		return month;
	}
	static String getWeek(int n){ 
		Map<Integer,String>map=new HashMap<>();
		map.put(0, "Sunday");
		map.put(1, "Monday");
		map.put(2, "Tuesday");
		map.put(3, "Wednesday");
		map.put(4, "Thursday");
		map.put(5, "Friday");
		map.put(6, "Saturday");
		String week=map.get(n);
		return week;
	}
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            //输入
        	int day=sc.nextInt();
        	String m=sc.next();
        	int month=getMonth(m);
        	int year=sc.nextInt();
        	int n=help(year,month,day);
        	System.out.println(getWeek(n%7));
        }
    }
    private static int help(int y, int m, int d) {
    	int day=1;
    	int y1=1,m1=1,d1=1;
    	while(y1<y||m1<m||d1<d) {
			d1++;
            if(d1==months[isLeapYear(y1)][m1]+1){
                d1=1;
                m1++;
            }
            if(m1==13){
                m1=1;
                y1++;
            }
            day++;
    	}
    	return day;
	}
}
卡在了求日期间隔的地方一个多小时,不知道为什么,下面求日期的做法就是错的。。
明明后面的题求两日期间隔这个代码就是对的(我是从后往前做的)
//很好理解啊,为啥错了呢???
int help(int y, int m, int d) {
		int sum=0;
		for(int i=1;i<y;i++) {
			sum+=365;
			sum+=isLeapYear(y);
		}
		for(int i=1;i<m;i++) {
			sum+=months[isLeapYear(y)][i];
		}
		return sum+=d;
}



发表于 2020-04-11 09:43:26 回复(0)
Java

import java.time.LocalDate;
import java.time.Month;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            int day = scanner.nextInt();
            String month = scanner.next();
            int year = scanner.nextInt();
            LocalDate date = LocalDate.of(year, Month.valueOf(month.toUpperCase()), day);
            String s = date.getDayOfWeek().toString().toLowerCase();
            System.out.println(s.substring(0,1).toUpperCase()+s.substring(1));
        }
    }
}


发表于 2020-03-19 23:34:54 回复(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)
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)
思路:考察锚点(题目所给日期和星期的对应关系)和所求日期相差天数,每七日一个轮回使用模运算,再将运算得到的数字和星期几对应,所求日期减去锚点取余数,0就是锚点所在星期天,1是星期一,以此类推。月份对应关系和礼拜几对应关系可以改用字符串数组(我是真不知道可以这么使)
#include <iostream>
#include <cstdio>
using namespace std;
int yeartab[2][13] = {
    {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, //非闰年
    {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
bool isleap(int year) {
    return (year % 100 != 0 && year % 4 == 0) || year % 400 == 0;//是闰年返回1
}
//月份转数字
int monthtonum(string month) {
    if (month == "January") return 1;
    if (month == "February") return 2;
    if (month == "March") return 3;
    if (month == "April") return 4;
    if (month == "May") return 5;
    if (month == "June") return 6;
    if (month == "July") return 7;
    if (month == "August") return 8;
    if (month == "September") return 9;
    if (month == "October") return 10;
    if (month == "November") return 11;
    if (month == "December") return 12;
    return 0;//所有函数都有不满足情况时的返回
}
//数字转英文星期几
string numtoday(int i) {
    if (i == 0) return "Sunday";
    if (i == 1) return "Monday";
    if (i == 2) return "Tuesday";
    if (i == 3) return "Wednesday";
    if (i == 4) return "Thursday";
    if (i == 5) return "Friday";
    if (i == 6) return "Saturday";
    return "0";
}
int daytozero(int year, string month,
              int day) {//计算相对于0000 00 00的天数绝对值
    int num = 0;
    int flag;
    int monthnum;
    for (int i = 0; i < year; i++) {
        flag = isleap(i);
        if (flag == 0) num += 365;
        else num += 366;
    }
    monthnum = monthtonum(month);
    for (int j = 0; j < monthnum; j++) {
        flag = isleap(year);
        num += yeartab[flag][j];
    }
    num += day;
    return num;

}
int main() {
    int anchor;
    anchor = daytozero(2001, "October", 14);
    int day, year;
    string month;
    int newday;
    int k;
    string dayofweek;
    int i;
    while (cin >> day >> month >> year) {
        newday = daytozero(year, month, day);
        k = (newday - anchor) % 7;
        if(k<0) k=k+7;//取模有负数,应令其为正
        dayofweek = numtoday(k);
        cout << dayofweek << endl;
    }

    return 0;
}

注意c++模运算余数有可能为负,加上一个7就为正余数
发表于 2024-03-08 17:00:41 回复(0)
绷不住了,2024年1月1号就是星期一,赢
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;

string days[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
int mouth_days1[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int mouth_days2[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
map<string, int> mouths;
int isLeap(int a) {
	if ((a % 4 == 0 && a % 100 != 0) || a % 400 == 0) {
		return 1;
	}
	return 0;
}
int main() {
	// 2024.01.01 为 星期一
	int day;
	string mouth;
	int year;
	mouths["January"] = 1;
	mouths["February"] = 2;
	mouths["March"] = 3;
	mouths["April"] = 4;
	mouths["May"] = 5;
	mouths["June"] = 6;
	mouths["July"] = 7;
	mouths["August"] = 8;
	mouths["September"] = 9;
	mouths["October"] = 10;
	mouths["November"] = 11;
	mouths["December"] = 12;
	while (cin >> day >> mouth >> year) {
		int sum = 0;
		if (year >= 2024) {
			for (int i = 2024; i < year; i++) {
				if (isLeap(i) == 1) {
					sum += 366;
				} else {
					sum += 365;
				}
			}
			int isL = isLeap(year);
			for (int i = 1; i < mouths[mouth]; i++) {
				if (isL) {
					sum += mouth_days2[i];
				} else {
					sum += mouth_days1[i];
				}
			}
			sum += day;
			sum -= 1; // 1月1号 算一天
			cout << days[sum % 7] << endl;
		} else {
			for (int i = year + 1; i < 2024; i++) {
				if (isLeap(i) == 1) {
					sum += 366;
				} else {
					sum += 365;
				}
			}
			int isL = isLeap(year);
			int sum_temp = 0;
			for (int i = 1; i < mouths[mouth]; i++) {
				if (isL) {
					sum_temp += mouth_days2[i];
				} else {
					sum_temp += mouth_days1[i];
				}
			}
			sum_temp += day;
			if (isL) {
				sum += 366 - sum_temp;
			} else {
				sum += 365 - sum_temp;
			}
			cout << days[6 - (sum % 7)] << endl;
		}
	}
	return 0;
}


发表于 2024-03-04 10:12:06 回复(0)
//#include<cstdio>
#include<iostream>
#include<string>
#include "map"
using namespace std;
bool isLeap(int year)
{
    bool k=year%400==0||year%100!=0&&year%4==0;
    return k;
}
int main()
{
    map<string,int> mymap={
            {"January",1},{"February",2},
            {"March",3},{"April",4},
            {"May",5},{"June",6},
            {"July",7},{"August",8},
            {"September",9}, {"October",10},
            {"November",11},{"December",12}
            };
    map<int,string >keymap={
            {1,"Monday"},{2,"Tuesday"},
            {3,"Wednesday"},{4,"Thursday"},
            {5,"Friday"},{6,"Saturday"},
            {0,"Sunday"}
    };
    int maday[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    char str[10];
    string month;
    int day,year;
    int weekday;
    while(cin >> day >> month >> year)
    {


        int n=0;
        for(int i=1;i<year;i++)
        {
            if(isLeap(i))
            {
                n+=366;
            }
            else {

                n += 365;
            }
        }
        if(isLeap(year))
        {
            maday[2]=29;
        }
        else
        {
            maday[2]=28;
        }
        int m=mymap[month];
        for(int i=1;i<=m;i++)
        {
            n+=maday[i-1];
        }
        n+=day;
        weekday=n%7;
       cout<<keymap[weekday]<<endl;


    }
    return 0;
}摸着大佬的思想
发表于 2024-03-02 14:55:01 回复(0)
#include <array>
#include <iostream>
#include <string>
using namespace std;

const array<string, 13>months = {   //月份名
    "",
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December"
};

const array<string, 7>dayOfWeek = { //星期名
    "Sunday",
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday"
};

inline bool isLeapYear(int year) {  //判断是否是闰年
    return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}

class Date {
  private:
    int year, month, day;   //年月日
    int totalDays;          //该日期是从公元元年1月1日开始的第多少天
  public:
    Date(int day, const string& month, int year);   //构造函数
    string getDayOfWeek();  //判断星期几(隐含条件:公元元年1月1日是星期一)
};

Date::Date(int day, const string& month, int year): day(day), year(year) {
    for (int i = 1; i <= 12; i++) {
        if (months[i] == month) {
            this->month = i;
            break;
        }
    }
    int years = year - 1;
    totalDays = years * 365 + years / 4 - years / 100 + years / 400;
    for (int month = 1; month < this->month; month++) {
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                totalDays += 31;
                break;
            case 2:
                totalDays += 28 + isLeapYear(year);
                break;
            default:
                totalDays += 30;
        }
    }
    totalDays += day;
}

string Date::getDayOfWeek() {
    return dayOfWeek[totalDays % 7];
}

int main() {
    int day, year;
    string month;
    while (cin >> day >> month >> year) {
        cout << Date(day, month, year).getDayOfWeek() << endl;
    }
    return 0;
}

编辑于 2024-02-03 09:25:31 回复(0)
#include <iostream>
#include <string>
#include <map>
using namespace std;
bool isLeapYear(int year){
	 return year%400==0||year%4==0&&year%100!=0;
}
int main(){
	int days[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
	map<string,int> monthToint={
		{"January",1},
		{"February",2},
		{"March",3},
		{"April",4},
		{"May",5},
		{"June",6},
		{"July",7},
		{"August",8},
		{"September",9},
		{"October",10},
		{"November",11},
		{"December",12},
	};
	int year,mon,day;
	char str[10]; 
	string month;
	while(scanf("%d%s%d",&day,str,&year)!=EOF){
		month=str;
		mon=monthToint[month];
		int sum=0;
		int i=year,j=mon,k=day;
		int left;
		if(i<2024){
			while(1){
				if(isLeapYear(i)){
					days[2]=29;		
				}else{
					days[2]=28;	
				}
				k++;
				sum++;
				if(k>days[j]){
					++j;
					k=1;
					if(j>12){
						j=1;
						++i;
					}
				}
				if(i==2024&&j==1&&k==13){
					break;
				}
			}
			left=sum%7;
			switch(left){
				case 0: cout<<"Saturday"<<endl;break;	
				case 1: cout<<"Friday"<<endl; break;
				case 2: cout<<"Thursday"<<endl;break;
				case 3: cout<<"Wednesday"<<endl;break;
				case 4: cout<<"Tuesday"<<endl;break;
				case 5: cout<<"Monday"<<endl;break;
				case 6: cout<<"Sunday"<<endl;break;
			}
		}else if(year==2024&&mon==1&&day<13){
			while(1){
				if(isLeapYear(i)){
					days[2]=29;		
				}else{
					days[2]=28;	
				}
				k++;
				sum++;
				if(k>days[j]){
					++j;
					k=1;
					if(j>12){
						j=1;
						++i;
					}
				}
				if(i==2024&&j==1&&k==13){
					break;
				}
			}
			left=sum%7;
			switch(left){
				case 0: cout<<"Saturday"<<endl;break;	
				case 1: cout<<"Friday"<<endl; break;
				case 2: cout<<"Thursday"<<endl;break;
				case 3: cout<<"Wednesday"<<endl;break;
				case 4: cout<<"Tuesday"<<endl;break;
				case 5: cout<<"Monday"<<endl;break;
				case 6: cout<<"Sunday"<<endl;break;
			}
		}else{ //2024-1-13 2024-1-15
			while(1){
				if(isLeapYear(i)){
					days[2]=29;		
				}else{
					days[2]=28;	
				}
				--k;
				sum++;
				if(k==0){
					--j;
					if(j<1){
						j=12;
						--i;
					} 
					k=days[j];
				}
				if(i==2024&&j==1&&k==13){
					break;
				}
			}
			left=sum%7;
			switch(left){
				case 0: cout<<"Saturday"<<endl;break;	
				case 1: cout<<"Sunday"<<endl; break;
				case 2: cout<<"Monday"<<endl;break;
				case 3: cout<<"Tuesday"<<endl;break;
				case 4: cout<<"Wednesday"<<endl;break;
				case 5: cout<<"Thursday"<<endl;break;
				case 6: cout<<"Friday"<<endl;break;
			}
		}
	} 
	return 0;
}

发表于 2024-01-13 21:37:16 回复(0)

问题信息

难度:
88条回答 11230浏览

热门推荐

通过挑战的用户

查看代码