zzulioj - 2600: 有多少天?

题目链接:http://acm.zzuli.edu.cn/problem.php?id=2600

题目描述
小D同学对日期类的问题很是有兴趣,已知1900-01-01是星期一,那么小D想知道给定两个年限x,y,在[x,y]内的每一年,每月的z号,是星期w的天数有多少天。例如x,y,z,w分别是1900、2000、12、7,表示在1900年到2000年之间的每一年(包括1900年和2000年),每月的12号,有多少天是星期七(周日)。
输入
多组测试数据,以EOF结束。
每组测试数据一行,每行四个整数,依次序分别是x,y,z,w。其中1900<=x<=y<=2100,1<=z<=31,1<=w<=7。
输出
对于每组测试数据,输出一行,每行一个整数,其意义如题目描述。注意如果某月的z号并不存在,请忽略,例如z=31时,2月31号不存在,忽略即可。
样例输入  Copy
2019 2019 8 7
2010 2019 31 7
样例输出  Copy
2
10

模拟,既然1900 01 01是周一,那就从这一天开始加,直到加到满足题目条件的区间里再去做判断
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<climits>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
#define mst(a) memset(a, 0, sizeof(a))
#define _test printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n")
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const double eps = 1e-7;
const int INF = 0x3f3f3f3f;
const ll ll_INF = 0x3f3f3f3f3f3f3f;
const int maxn = 1e4+10;
const int ls[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool isr(int y) { //判断闰年
    return (y%100 && !(y%4)) || !(y%400); 
}
bool ok(int sum, int w) { //判断是不是周w
    return sum%7 + 1 == w;
}
int main(void) {
    int x, y, z, w;
    while(~scanf("%d%d%d%d", &x, &y, &z, &w)) {
        ll sum = z-1; 
        int start = 1900;
        while(start < x) { //计算x年1月z号到1900年1月1号的天数
            sum += (365 + isr(start));
            ++start;
        }
        int cnt = 0;
        while(start <= y) { //求解答案
            for (int i = 1; i<=12; ++i) {
                sum += ls[i-1]; //加上上个月的天数
                if (i == 3) //如果要加上2月的天数, 判断要不要补1
                    sum += isr(start);
                if ((i==2 && z > ls[2]+isr(start)) || (i != 2 && z > ls[i])) //如果该月的z号不存在,跳过, 不统计次数
                    continue;
                cnt += ok(sum, w); //统计次数
            }
            sum += ls[12]; //加上第十二月的天数
            ++start; //换到下一年
        }
        printf("%d\n", cnt);
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/shuitiangong/p/12076012.html