首页 > 试题广场 > Day of Week
[编程题]Day of Week
  • 热度指数:10077 时间限制: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 回复(1)

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)
基姆拉尔森计算公式
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)
#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 回复(9)
//王道机试指南解法
#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)
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<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)
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 回复(0)
用的基姆拉尔森公式,最开始用的蔡勒公式,但是只能过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<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)
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)
以1000.1.1为起点,不知道这天星期几没关系,首先计算出输入日期与这天相差的天数,模7后输出,用样例来试探,差几天补几天即可。此外,计算相差天数注意闰年。
// Day_of_week.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <vector>
using namespace std;

vector<int> leap_year;

void find_leap_year();
int getNumofly(int, int);
bool isly(int);

int main()
{
    vector<string> month = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
    vector<string> week = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
    int monthgap[12] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
    int date,mo,we,year;
    string str_mo;
    find_leap_year();
    while (cin >> date >> str_mo >> year) {
        for (int i = 0; i < month.size(); i++) {
            if (str_mo == month[i]) {
                mo = i+1;
                break;
            }
        }
        int numofly = getNumofly(1000, year);
        // cout << "numofly: " << numofly << endl;
        if (isly(year) && mo > 2) numofly += 1;
        we= numofly + (year - 1000) * 365 + monthgap[mo - 1] + date - 5;
        we %= 7;
        cout << week[we] << endl;
    }
}

void find_leap_year() {
    for (int i = 0; i <= 3000; i++) {
        if (i % 400 == 0) leap_year.push_back(i);
        else if (i % 100 != 0 && i % 4 == 0) leap_year.push_back(i);
    }
}

int getNumofly(int y1, int y2) {
    int count = 0;
    for (int i = 0; i < leap_year.size(); i++) {
        if (y1 < leap_year[i] && y2 > leap_year[i]) count++;
    }
    return count;
}

bool isly(int y) {
    if ((y % 400 == 0) || (y % 100 != 0 && y % 4 == 0)) return true;
    return false;
}




编辑于 2021-03-01 18:32:40 回复(0)
//利用基姆拉尔森计算公式
//1月、2月当成13月、14月,从3月开始进行计算
//从星期日到星期六
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

int main()
{
    char WeekName[7][20] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
    char MonthName[15][20] = {"", "", "", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December", "January", "February"};
    int d, m, y;
    char M[20];

    while(scanf("%d %s %d", &d, M, &y) != EOF)
    {
        for(int i = 3; i < 15; i++)
        {
            if(strcmp(MonthName[i], M) == 0)
            {
                m = i;
                break;
            }
        }
        if(m >= 13)
            y--;
        int W = (d + 1 + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7;

        printf("%s\n", WeekName[W]);
    }

    return 0;
}

发表于 2021-02-24 09:45:56 回复(0)
#include <iostream>
#include <algorithm>
#include <map>
#include <sstream>
using namespace std;

int yy1,m1,d1,w1,yy2,m2,d2,w2;

string month[12] = {"January", "February", 
                    "March", "April", "May", "June", "July", "August", "September", 
                    "October", "November", "December"};
string week[7] = {"Sunday", "Monday", "Tuesday", "Wednesday",
                  "Thursday", "Friday", "Saturday"};
map<string, int> mp;


// q[1] 为闰年
int q[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}};

//闰年返回true
bool isyear(int y)
{
    if(y % 4 == 0 && y % 100 != 0 ) return true;
    if(y % 400 == 0) return true;
    return false;
}

int main()
{
    for(int i = 0; i < 12; i ++) mp[month[i]] = i + 1;

    string s;
    while(getline(cin,s))
    {
        if(s.size() == 0) continue;
        stringstream ss;
        ss << s;

        string t;
        ss >> d2 >> t >> yy2;
        m2 = mp[t];


        yy1 = 1;
        m1 = 1;
        d1 = 1;
        w1 = 1;


        while(!(yy1 == yy2 && m1 == m2 && d1 == d2))
        {
            int i = isyear(yy1);// i = 1 为闰年
            w1 = (w1 + 1) % 7;
            d1 ++;
            if(d1 > q[i][m1 - 1])
            {
                d1 -= q[i][m1 - 1];
                m1 ++;
            }
            if(m1 > 12)
            {
                yy1 ++;
                m1 = 1;
            }
        }
        cout<<week[w1]<<endl;

    }

}
发表于 2021-02-23 22:12:21 回复(0)
#include <bits/stdc++.h>
#include<string.h>
using namespace std;

int m[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};
//string y[12]={"01","02","03","04","05","06","07","08","09","10","11","12"};
//string z[31]={"01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20",
               // "21", "22","23","24","25","26","27","28","29","30","31"};

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


char month_name[13][20]={
    "","January","February","March","April","May","June","July","August",
    "September","October","November","December"
};
bool IsRun(int year)
{
    if((year%400==0) || ((year%4==0) &&(year%100!=0)))
    {
        return true;
    }else
    {
        return false;
    }

}
int dayofyear(int year)
{
    if(IsRun(year))
    {
        return 366;

    }else
    {
        return 365;
    }
}

int sumofdate(int y,int z,int d)
{
    int sum;
    for(int i=0;i<y;i++)
    {
        sum+=dayofyear(i);

    }
    for(int i=1;i<z;i++)
    {
        sum+=m[int(IsRun(y))][i-1];
    }
    sum+=d;
    return sum;


}

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=2000,m1=1,d1=1;
      int y2=y,m2=m,d2=d;

     int dectime=abs(sumofdate(y1,m1,d1)-sumofdate(y2,m2,d2));

     printf("%s\n",wekt[(dectime-1)%7].c_str());


  }

  return 0;


发表于 2021-02-06 01:16:00 回复(0)
有一个用来换算日期和星期的公式:基姆拉尔森计算公式
W= (d+1+2*m+3*(m+1)/5+y+y/4-y/100+y/400) mod 7
其中d,m,y分别为日,月,年;
需要注意如果月份为一月或二月,需要加12变为13、14月,同时年份减一
#include <iostream>
#include <string>
using namespace std;

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}
};
string monthTab[13]={"none","January","February","March","April","May","June","July","August","September","October","November","December"};
string wTab[8]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
int dayOfYear(int year, int month, int day)
{
    int countd=0;
    for(int i=0; i<month;i++)
    {
        countd += dayTab[0][i];
    }
    countd += day;
    return countd;

}

int main()
{
    int year,day;
    string month;
    while(cin>>day>>month>>year)
    {
        int monthNumber;
        for(int i=0;i<=12;i++)
        {

            if(monthTab[i]==month)
            {
                monthNumber = i;
            }
        }
        if(monthNumber==1||monthNumber==2)
        {
            monthNumber+=12;
            year--;
        }
        int w = (day+1+2*monthNumber+3*(monthNumber+1)/5+year+year/4-year/100+year/400)%7;
        cout<<wTab[w]<<endl;
    }
    return 0;
}

发表于 2021-01-29 16:33:40 回复(0)
#include<stdio.h>
#include<string.h>
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}
};
int IS_row(int year){
    return(year%4==0&&year%100!=0)||year%400==0;
}
int dis_tance(int year ,int month,int day){
    int sum=0;int i;int y=IS_row(year);
       for(i=0;i<year;++i){
        if(IS_row(i)){
            sum+=366;
        }else{
            sum+=365;
        }
    }
      for(i=0;i<month;++i){
        sum+=daytab[y][i];
    }
   
    sum+=day;
   
    return sum-3;
}
int How_month(char s[]){
    if(strcmp(s,"January")==0)return 1;
    if(strcmp(s,"February")==0)return 2;
    if(strcmp(s,"March")==0)return 3;
    if(strcmp(s,"April")==0)return 4;
    if(strcmp(s,"May")==0)return 5;
    if(strcmp(s,"June")==0)return 6;
    if(strcmp(s,"July")==0)return 7;
    if(strcmp(s,"August")==0)return 8;
    if(strcmp(s,"September")==0)return 9;
    if(strcmp(s,"October")==0)return 10;
    if(strcmp(s,"November")==0)return 11;
    if(strcmp(s,"December")==0)return 12;
    return 0;
}
void How_week(int b){
      
         if(b==1)printf("Tuesday");
        if(b==2)printf("Wednesday");
        if(b==3)printf("Thursday");
        if(b==4)printf("Friday");
        if(b==5)printf("Saturday");
        if(b==6)printf("Sunday");
        if(b==0)printf("Monday");
        }
      
        

int main(){
    int day,month,year;
    char s[20];int a;int b,y;
    while(scanf("%d %s %d",&day,s,&year)!=EOF){
        y=year;
        month=How_month(s);
          
        a=dis_tance(year,month,day);
        
        b=a%7;
        How_week(b);
        printf("\n");    
                
               
   }   
    
    return 0;
}
发表于 2021-01-06 09:21:34 回复(0)
#include<cstdio>
#include<map>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
bool isyun(int y){
    if(y%4==0&&y%100!=0||y%400==0)
        return true;
    return false;
}
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}};
map<string,int> m;
string s[7]={"Sunday","Monday","Tuesday", "Wednesday","Thursday", "Friday", "Saturday"};
int main(){
	m["January"]=1,m["February"]=2,m["March"]=3,m["April"]=4,m["May"]=5;
    m["June"]=6,m["July"]=7,m["August"]=8,m["September"]=9,m["October"]=10;
    m["November"]=11,m["December"]=12;
    int y2,d2,week2;
    string mm2;
    while(cin>>d2>>mm2>>y2){
    int y1=2020,m1=4,d1=21,week1=2;
    //cout<<y2<<m[mm2]<<d2<<endl;
    int m2=m[mm2];
    int sum1=y1*10000+m1*100+d1;
    int sum2=y2*10000+m2*100+d2;
    int flag=0;
    if(sum2<sum1)
    {
        flag=1;
        swap(y1,y2);
        swap(m1,m2);
        swap(d1,d2);
    }
    int sum=0;
    while(y1!=y2||d1!=d2||m1!=m2){
          d1++;
          sum++;
          if(d1>month[m1][isyun(y1)]){
              d1=1;
              m1++;
              if(m1>12)
              {
                  m1=1;
                  y1++;
              }
          }
    }
    if(flag==1){
        week2=((week1+(sum/7+1)*7)-sum)%7;
    }else{
        week2=(week1+sum)%7;
    }
    cout<<s[week2]<<endl;
    }
    return 0;
}

编辑于 2020-04-22 13:53:44 回复(0)

问题信息

难度:
60条回答 6681浏览

热门推荐

通过挑战的用户

查看代码