zoukankan      html  css  js  c++  java
  • 日期类问题-判断星期几

    题目及思路:

    代码:

    #include <stdio.h>
    #include <string.h>
    
    int isLeapYear(int x){//1代表闰年 
        if( (x%4==0 && x%100!=0) || x%400==0) return 1;
        return 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][isLeapYear(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 abs(int x){
        if(x < 0) return -x;
        return x;
    }
    int main(){
        Date time;
        time.day=1;
        time.month=1;
        time.year=1000;
        int cnt=0;//记录天数
        while(time.year!=3001) {
            buf[time.year][time.month][time.day]=cnt;
            time.nextDay();
            cnt++;
        }
        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,monthName[m])==0)
                    break;
            }
            int days=abs( buf[y][m][d] - buf[2018][1][8])+1;
            printf("%s
    ",weekName[days%7]);
        }
        return 0;
    }
  • 相关阅读:
    shell脚本程序练习
    02、重定向和管道符
    01、bash的基本特性
    python--03day
    python--02day
    python--01day
    Django之Form
    Django之ajax
    csrf的中间件
    Django之中间件
  • 原文地址:https://www.cnblogs.com/chao-zjj/p/8278867.html
Copyright © 2011-2022 走看看