洛谷P1202 [USACO1.1]Friday the Thirteenth

Topic links: P1202 [USACO1.1] Black Friday Friday the Thirteenth

Program description:

The date three loops, declare two variables date and week start counting simultaneously. date cumulative date, number of days equal to 0 mod month on behalf of the last day of this month. week accumulation weeks, equal to 0 modulo 7 Representative Sunday.

code show as below:

#include <iostream>
#include <cstdio>
using namespace std;
int n;
//判断闰年 
bool judge(int i) {
	if(i % 400 == 0 || i % 4 == 0 && i % 100 != 0)
		return true;
	else
		return false;
}
int a[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int b[7], i, j, k, date, week;
int main() {
	scanf("%d", &n);
	for(i = 1900; i < 1900 + n; i++) {
		if(judge(i))
			a[1] = 29;
		else
			a[1] = 28;
		for(j = 0; j < 12; j++) {
			for(k = 1; k <= a[j]; k++) {
				date++;
				date = date % a[j]; 
				week++;
				week = week % 7;
				if(date == 13)
					b[week]++;
			}			
		}
	}
	printf("%d %d %d %d %d %d %d", b[6], b[0], b[1], b[2], b[3], b[4], b[5]);
	return 0;
} 
Published 44 original articles · won praise 0 · Views 822

Guess you like

Origin blog.csdn.net/Komatsu_1137/article/details/104090107