POJ 2080 && ZOJ 1256 日期计算实例

POJ 2080

题意分析

输出 2000年1月1日后 n 天后的日期与星期。

首先算整年,在整年不满足的情况下,计算整月,在月不满足的情况下,计算天数。最后求和,即是日期。根据2000年1月1日式星期天,设定wstr星期字符串常量,模7后求得星期。

代码总览

#include<cstdio>
using namespace std;
const char wstr[][20] = {"Saturday","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday"};
bool month_check[13] = {0,1,0,1,0,1,0,1,1,0,1,0,1};
int days_of_year(int year){
    if(year % 100 == 0) return year % 400 == 0? 366:365;
    return year % 4 == 0? 366:365;
}
int day_of_month(int month, int year){
    if(month == 2) return days_of_year(year) == 366? 29:28;
    if(month_check[month]) return 31;
    return 30;
}

int main(){
    int n;
    while(scanf("%d",&n) && n>=0){
        int year = 2000,month = 1,day = 1,week = n % 7;
        while(n){
            if(n >= days_of_year(year)){
                n-= days_of_year(year);
                ++year;
            }else if(n>=day_of_month(month,year)){
                n-= day_of_month(month,year);
                ++month;
            }else{
                day += n;
                n = 0;
            }
        }
        printf("%d-%s%d-%s%d %s\n",year,month<10?"0":"",month,day<10?"0":"",day,wstr[week]);
    }
    return 0;
}

ZOJ 1256

题意分析

根据1752年9月2日作为分界线,前面为旧日期,后面为新日期。然后检查合法性,计算日期,根据是否为旧日期做调整,最后输出结果。

代码总览

#include<bits/stdc++.h>
using namespace std;
const char wstr[][20] = {"Saturday","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday"};
const char mstr[][20] = {"","January","February","March","April","May","June","July","August","September","October","November","December"};
bool monthcheck[13] = {0,1,0,1,0,1,0,1,1,0,1,0,1};
bool isdayold = false;
bool isold(int month, int day, int year){
    if(year < 1752 || (year == 1752 && month < 9) || (year == 1752 && month == 9 && day <= 2))
        return true;
    return false;
}

bool isleap(int year){
    if(year <= 1752){
        return year % 4 == 0;
    }else{
        return (year % 100 == 0? (year % 400 == 0) : (year % 4 == 0));
    }
}

int day_of_year(int year){
    return isleap(year)? 366:365;
}

int day_of_month(int month,int year,bool leap){
    if(month == 2){
        return leap? 29:28;
    }else{
        return monthcheck[month]? 31:30;
    }
}

bool valid_date(int month, int day, int year){
    if(year < 1) return false;
    if(month < 1 || month > 12) return false;
    if(day < 1 || day > day_of_month(month, year, isleap(year))) return false;
    if(year == 1752 && month == 9 && day >=3 && day <=13) return false;
    return true;
}

int main(){
    int month,day,year,week;
    while(scanf("%d %d %d",&month,&day,&year) != EOF){
        if(month == 0 && day == 0 && year == 0) break;
        bool old = isold(month,day,year);
        if(!valid_date(month,day,year)){
            printf("%d/%d/%d is an invalid date.\n",month,day,year);
            continue;
        }
        int sum = -1;
        for(int y = 1;y<year;++y) sum += day_of_year(y);
        for(int m = 1;m<month;++m) sum += day_of_month(m,year,isleap(year));
        sum += day;
        week = (sum-11) % 7;
        if(old) week = sum % 7;
        printf("%s %d, %d is a %s\n",mstr[month],day,year,wstr[week]);
    }
    return 0;
}

总结

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值