POJ 2080 Calendar(很水的模拟)

刚开始一直WA,才发现原来代码中两处减去年份、月份的天数的判断条件用的是>=,虽然最后考虑n=0要退回一天的情况,但还是WA。后来改成>的条件判断,省去了考虑n=0的麻烦,AC。

此题无非就是考虑平年、闰年,月底月末,年底年末的情况。

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <cstring>
#include <stdlib.h>

using namespace std;
char strd[33][5],strm[14][5];
char week[7][12]={"Saturday","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday"};
int month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int year[2]={365,366};
struct Cal {
    int y,m,d;
} cal;

int isRun(int n){
    if((n%4==0 && n%100!=0)||(n%400==0))
        return 1;
    return 0;
}
int main() {
    int n,y,m,d;//y:年,m:月,d:周几
    while(scanf("%d",&n)!=EOF) {
        if(n==-1)
            break;
        d=n%7;//2000.01.01为星期六
        n++;
        y=m=0;
        //如果写成>=,还要考虑当n刚好为365、366时要退回一天的情况
        while(n>year[isRun(2000+y)]) {
            n-=year[isRun(2000+y)];
            y++;
        }

        y+=2000;
        if(isRun(y))
            month[2]++;
        int i;
        for(i=1; i<=12; i++) {
            //改成n>month[i],即可不用考虑当n=0要退回一天的情况
            if(n>month[i]) {
                n-=month[i];
            }
            else
                break;
        }

        m=i;
        if(month[2]==29)
            month[2]--;
        cout<<y<<"-"<<(m<10?"0":"")<<m<<"-"<<(n<10?"0":"")<<n<<" "<<week[d]<<endl;

    }
    return 0;
}
原文地址:https://www.cnblogs.com/chenxiwenruo/p/3327175.html