您的位置:首页 > 其它

题目1043:Day of Week

2017-04-14 09:09 417 查看


#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
****************************************************************/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: