Friday the Thirteenth(USACO)

来源:互联网 发布:php static 编辑:程序博客网 时间:2024/06/12 00:57

http://train.usaco.org/usacoprob2?a=yDGYEVaITpf&S=friday

题意:问你从1900年1月1日到1900 + n - 1年12月31日每个月的13日出现在星期几,然后把出现频率依次打印出来,打印顺序依次为星期六,星期日,星期一,星期二,星期三,星期四,星期五。

思考:本身是一道很水的题,不过一定要看输出格式,输出是要从星期六开始输出,因为这个问题调了很长时间。

源码忘存了,所以下面的代码是官网上的代码。

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <assert.h>intisleap(int y){    return y%4==0 && (y%100 != 0 || y%400 == 0);}int mtab[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };/* return length of month m in year y */intmlen(int y, int m){    if(m == 1)    /* february */        return mtab[m]+isleap(y);    else        return mtab[m];}voidmain(void){    FILE *fin, *fout;    int i, m, dow, n, y;    int ndow[7];    fin = fopen("friday.in", "r");    fout = fopen("friday.out", "w");    assert(fin != NULL && fout != NULL);    fscanf(fin, "%d", &n);    for(i=0; i<7; i++)        ndow[i] = 0;    dow = 0;    /* day of week: January 13, 1900 was a Saturday = 0 */    for(y=1900; y<1900+n; y++) {        for(m=0; m<12; m++) {            ndow[dow]++;            dow = (dow+mlen(y, m)) % 7;        }    }    for(i=0; i<7; i++) {        if(i)            fprintf(fout, " ");        fprintf(fout, "%d", ndow[i]);    }    fprintf(fout, "\n");    exit(0);}
0 0
原创粉丝点击