zoukankan      html  css  js  c++  java
  • USACO 1.1friday

    题意:

    计算1900年后N年内(1900~1900+n-1)所有月的13号是星期几的个数

    思路:

    简单模拟

    易错点:

    1. 当年不算。如算1906年时,这年有365天是不能算的
    2. 当年闰年影响条件:当年是闰年,对当年天数有影响,还得保证月份>2

    经验:

    模拟题,最好用最简单的方式去做。。或者,全部先想清楚再做。

    代码:

    简单方式

    /*
    PROG: friday
    LANG: C++
    */
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    
    int yearday[410], monthday[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
    int count[7] = {0};
    
    bool isrun(int year) {
        if (year % 4 == 0 && year % 100 != 0) return true;
        else if (year % 400 == 0) return true;
        else return false;
    }
    
    void init(int n) {
        int i;
        for (i = 0; i < n; i++) {
            if (isrun(i+1900)) yearday[i] = 366;
            else yearday[i] = 365;
        }
    }
    int getdaytype(int year, int month, int day) {
        int i;
        int totalday = 0;
        for (i = 1900; i < year; i++) {
            totalday += yearday[i-1900];
        }
        for (i = 1; i < month; i++) {
            totalday += monthday[i];
        }
        if (month > 2 && isrun(year)) totalday++;
        totalday += day;
        return totalday % 7;
    }
    
    int main() {
            freopen("friday.in", "r", stdin);
        freopen("friday.out", "w", stdout);
        int n;
        scanf("%d", &n);
    
        init(n);
        int i;
        for (i = 1900; i < 1900+n; i++) {
            int j;
            for (j = 1; j <= 12; j++) {
                count[getdaytype(i,j,13)]++;
            }
        }
    
        printf("%d", count[6]);
        for (i = 7; i < 6+7; i++) {
            printf(" %d", count[i%7]);
        }
        puts("");
        return 0;
    }

    公式方式

    /*
    PROG: friday
    LANG: C++
    */
    //类型:模拟
    //Submit:1WA…………SHIT 1AC
    //错误点:主要是当年不算这个问题。。
    //Gain:练习模拟
    //Experince:模拟题,一定要用最简单的方式去写,或者先完完全全想好思路(特别是复杂思路)。
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    
    int monthday[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
    
    int getdaytype(int year, int month, int day) {
        int totalday = 0;
        //算这年时,这年所有的天数不算!
        totalday += (year-1900)*365 + (year-1900-1)/4 - (year-1900-1)/100;
        if (year > 2000) totalday++;
        int i;
        for (i = 0; i < month; i++) {
            totalday += monthday[i];
        }
        //算这年时,如果是闰年,还要再特别处理
        if (month > 2 && (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)) totalday ++;
        totalday += day;
        return totalday % 7;
    }
    
    int count[7] = {0};//sunday = 0
    int main() {
    //    int y, m, d;
    //    while(scanf("%d%d%d", &y, &m, &d) != EOF) {
    //        printf("%d\n", getdaytype(y,m,d));
    //    }
        freopen("friday.in", "r", stdin);
        freopen("friday.out", "w", stdout);
        int n;
        scanf("%d", &n);
        int i;
    
    
        for (i = 1900; i < 1900+n; i++) {
            int j;
            for (j = 1; j <= 12; j++) {
                count[getdaytype(i, j, 13)]++;
            }
        }
    
        int isfirst = 1;
        for (i = 6; i < 13; i++) {
            if (isfirst) {
                isfirst = 0;
                printf("%d", count[i%7]);
            } else printf(" %d", count[i%7]);
        }
        puts("");
        return 0;
    }
  • 相关阅读:
    CISP/CISA 每日一题 七
    CISP/CISA 每日一题 六
    CISP/CISA 每日一题 五
    C++编码优化之减少冗余拷贝或赋值
    CISP/CISA 每日一题 四
    CISP/CISA 每日一题 三
    CISP/CISA 每日一题 二
    CISP/CISA 每日一题
    C#与C++ DLL的交互
    数据同步工具otter(二)
  • 原文地址:https://www.cnblogs.com/shinecheng/p/3085112.html
Copyright © 2011-2022 走看看