首页 > USACO题解 > USACO Friday the Thirteenth

USACO Friday the Thirteenth

USACO Friday the Thirteenth主要是一个日期处理问题。首先是要会判断闰年,判断方法题目已经说的很清楚了。其次是处理好日期和星期的关系,这里是通过取余来判断的,具体请看下面的解题代码:

/*
	ID:stackex1
	LANG:C
	PROG:friday
*/

#include <stdio.h>
#include <string.h>

int month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int is_leap_year(int y)
{
	if((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0))
		return 1;
	return 0;
}

void work(int n, int f[])
{
	int i, j, s = 1;
	for(i = 0; i < n; ++i)
	{
		for(j = 1; j <= 12; ++j)
		{
			s += 12;
			++f[s % 7];
			if(j == 2 && is_leap_year(1900 + i))
				s += 29 - 12;
			else
				s += month[j] - 12;
		}
	}
}

int main(int argc, char **argv)
{
	int n, f[7], i;
	
	FILE *fin = fopen("friday.in", "r");
	FILE *fout = fopen("friday.out", "w");
	
	fscanf(fin, "%d", &n);
	memset(f, 0, sizeof(f));
	work(n, f);
	
	fprintf(fout, "%d %d", f[6], f[0]);
	fprintf(fout, " %d %d %d %d %d\n", f[1], f[2], f[3], f[4], f[5]);

	return 0;
}

觉得文章还不错?点击此处对作者进行打赏!


本文地址: 程序人生 >> USACO Friday the Thirteenth
作者:代码疯子(Wins0n) 本站内容如无声明均属原创,转载请保留作者信息与原文链接,谢谢!


更多



分类: USACO题解 标签: , , ,
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.