Project Euler 19 Counting Sundays( 蔡勒公式计算星期数 )


题意:在二十世纪(1901年1月1日到2000年12月31日)中,有多少个月的1号是星期天?

蔡勒公式:****计算 ( year , month , day ) 是星期几

以下图片仅供学习!


/*************************************************************************
    > File Name: euler019.c
    > Author:    WArobot 
    > Blog:      http://www.cnblogs.com/WArobot/ 
    > Created Time: 2017年06月30日 星期五 15时58分37秒
 ************************************************************************/

#include <stdio.h>
#include <math.h>
#include <inttypes.h>

int32_t CountingSundays(int32_t y , int32_t m , int32_t d) {	// 蔡勒公式
	if (m == 1 || m == 2)	m += 12 , y--;
	int32_t w;
	w = (d + 2 * m + 3 * (m + 1) / 5 + y + (int32_t)floor(y/4) - (int32_t)floor(y/100) + (int32_t)floor(y/400)) % 7;
	return w;
}
int32_t main() {
	int32_t ans = 0;
	for (int32_t y = 1901 ; y <= 2000 ; y++) {
		for (int32_t m = 1 ; m <= 12 ; m++) {
			if (CountingSundays(y , m , 1) != 6)	continue;
			ans++;
		}
	}
	printf("%d
",ans);
	return 0;
}
原文地址:https://www.cnblogs.com/WArobot/p/7099463.html