题目1043:Day of Week

#include<stdio.h>
#include<string>
#include<string.h>
using namespace std;
#define TheYear 2017
#define TheMonth 4
#define TheDay 13
#define TheDayOfWeek 4
struct mystring{
    char num[20];
};
const int leap=366;
const int notleap=365;
const int dayinmonth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
const mystring dayinweek[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
const mystring monthinyear[13]={"","January", "February","March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
bool isLeap(int x){
    if((x%4==0&&x%100!=0)||x%400==0)return true;
    else return false;
}
int pastdays(int year,int month,int day){//in year
    bool leap=isLeap(year);
    int daythisyear=0;
    for(int i=1;i<month;i++){
        if(i==2){
            if(leap)daythisyear+=29;
            else daythisyear+=28;
        }else daythisyear+=dayinmonth[i];
    }
    daythisyear+=day;
    return daythisyear;
}
int futuredays(int year,int month,int day){
    bool leap=isLeap(year);
    int daythisyear=0;
    for(int i=month+1;i<=12;i++){
        if(i==2){
            if(leap)daythisyear+=29;
            else daythisyear+=28;
        }else daythisyear+=dayinmonth[i];
    }
    if(month==2){
        if(leap)daythisyear+=(29-day);
            else daythisyear+=(28-day);
    }
    else daythisyear+=(dayinmonth[month]-day);
    return daythisyear;
}
int main(){
    int day,months,year,dayspast,daythisyear=0;
    char month[20];
    bool leap;
     
    while(scanf("%d%s%d",&day,month,&year)!=EOF){
        for(int i=1;i<=12;i++){
            if(strcmp(month,monthinyear[i].num)==0){months=i;break;}
        }
 
        if(year==TheYear){//in the same year
            dayspast=pastdays(TheYear,TheMonth,TheDay);
            int daysdayspast=pastdays(year,months,day);
             
            if(daysdayspast<dayspast){//before
                int last=dayspast-daysdayspast;
                int daypastinweek=TheDayOfWeek-last%7;
                if(daypastinweek<0)daypastinweek+=7;
                printf("%s\n",dayinweek[daypastinweek].num);
            }else if(daysdayspast>dayspast){//after
                int last=daysdayspast-dayspast;
                int daypastinweek=TheDayOfWeek+last%7;
                if(daypastinweek<0)daypastinweek+=7;
                printf("%s\n",dayinweek[daypastinweek].num);
            }
            else{
                printf("%s\n",dayinweek[TheDayOfWeek].num);
            }
 
        }else if(year<TheYear){
            dayspast=pastdays(TheYear,TheMonth,TheDay);
            for(int i=year+1;i<=TheYear-1;i++){
                if(leap=isLeap(i))dayspast+=366;
                else dayspast+=365;
                 
            }
            dayspast+=futuredays(year,months,day);
            int daypastinweek=TheDayOfWeek-dayspast%7;
            if(daypastinweek<0)daypastinweek+=7;
            printf("%s\n",dayinweek[daypastinweek].num);
             
        }else{
            dayspast=pastdays(year,months,day);
            for(int i=TheYear+1;i<=year-1;i++){
                if(leap=isLeap(i))dayspast+=366;
                else dayspast+=365;
                 
            }
            dayspast+=futuredays(TheYear,TheMonth,TheDay);
            int daypastinweek=TheDayOfWeek+dayspast%7;
            if(daypastinweek>=7)daypastinweek-=7;
            printf("%s\n",dayinweek[daypastinweek].num);
 
        }
         
         
 
    }
    return 0;
}
/**************************************************************
    Problem: 1043
    User: cust123
    Language: C++
    Result: Accepted
    Time:0 ms
    Memory:1020 kb
****************************************************************/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值