USACO:Friday the Thirteenth

考点:水题?
思路:计算从1900 1月1日开始计算天数,7天一周循环来记录13号星期几
提交情况:1A
收获:.....
经验:读题读题读题...最后输出理所当然的想着是从星期一开始输出,结果想了2个小时....还有对循环范围的控制
******************************************************************
Friday the Thirteenth

Is Friday the 13th really an unusual event?

That is, does the 13th of the month land on a Friday less often than on any other day of the week? To answer this question, write a program that will compute the frequency that the 13th of each month lands on Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday over a given period of N years. The time period to test will be from January 1, 1900 to December 31, 1900+N-1 for a given number of years, N. N is non-negative and will not exceed 400.

There are few facts you need to know before you can solve this problem:

  • January 1, 1900 was on a Monday.
  • Thirty days has September, April, June, and November, all the rest have 31 except for February which has 28 except in leap years when it has 29.
  • Every year evenly divisible by 4 is a leap year (1992 = 4*498 so 1992 will be a leap year, but the year 1990 is not a leap year)
  • The rule above does not hold for century years. Century years divisible by 400 are leap years, all other are not. Thus, the century years 1700, 1800, 1900 and 2100 are not leap years, but 2000 is a leap year.

Do not use any built-in date functions in your computer language.

Don't just precompute the answers, either, please.

PROGRAM NAME: friday

INPUT FORMAT

One line with the integer N.

SAMPLE INPUT (file friday.in)

20  

OUTPUT FORMAT

Seven space separated integers on one line. These integers represent the number of times the 13th falls on Saturday, Sunday, Monday, Tuesday, ..., Friday.

SAMPLE OUTPUT (file friday.out)

36 33 34 33 35 35 34  ****************************************************************************************************
AC code:
/*
ID: jun41821
PROG: friday
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
int main()
{
    ofstream fout ("friday.out");
    ifstream fin ("friday.in");
    int a[7]={0},year=1899,i,N,m,day=13;                       //分别代表周一到周日的次数  初始清零
    fin>>N;
    for(i=1;i<=N;i++)
    {                       //从1900年一月一号开始     到    1900+N-1年的12月结束
        year++;
        if((year%4==0&&year%100!=0)||(year%400==0&&year%100==0))            //闰年
        {
            //从一月开始   搜索13号    该星期++
            //1900一月一日是周一;     //该月的13号是这年的第N天     则13号为星期N/7;
            for(m=1;m<=12;m++)//一月
            {
                if(m==1&&year==1900) day=13;            //第一年
                if(m==1&&year!=1900)  day+=31;                //上年12月份  大月
                if((m-1)==2)        //二月的情况;
                    day+=29;
                if(m<=8&&m>1&&m!=3)
                {
                    if(m%2==0)      //1  3  5 7  大月的情况;     单月为大月
                    day+=31;
                    if(m-1==4||m-1==6)  //4月  六月的情况;
                    {
                     day+=30;
                    }
                }
                if(m>8)
                {
                    if((m-1)%2==0) day+=31;              //八份  10  月份       双月为大月
                    if(m%2==0)  day+=30;                //9月份11月  小月;
                }
               // fout<<day<<' ';
            switch(day%7)
            {
                case 1: a[0]++;break;
                case 2: a[1]++;break;
                case 3: a[2]++;break;
                case 4: a[3]++;break;
                case 5: a[4]++;break;
                case 6: a[5]++;break;
                case 0: a[6]++;break;
            }
            }
        }
        else
        {
            //同上
            //从一月开始   搜索13号    该星期++
            //1900一月一日是周一;     //该月的13号是这年的第N天     则13号为星期N/7;
            for(m=1;m<=12;m++)//一月
            {
                if(m==1&&year==1900) day=13;            //第一年
                if(m==1&&year!=1900)  day+=31;                //上年12月份  大月
                if((m-1)==2)        //二月的情况;
                    day+=28;
                if(m<=8&&m>1&&m!=3)
                {
                    if(m%2==0)      //1  3  5 7  大月的情况;
                    day+=31;
                    if(m-1==4||m-1==6)  //4月  六月的情况;
                    {
                     day+=30;
                    }
                }
                if(m>8)
                {
                    if((m-1)%2==0) day+=31;              //八份  10  月份
                    if(m%2==0)  day+=30;                //9月份11月  小月;
                }
               // fout<<day<<' ';
                switch(day%7)
                {
                case 1: a[0]++;break;
                case 2: a[1]++;break;
                case 3: a[2]++;break;
                case 4: a[3]++;break;
                case 5: a[4]++;break;
                case 6: a[5]++;break;
                case 0: a[6]++;break;
                }
            }//day处理完毕      一个月处理完毕
            //判断星期几
        }
    }
    fout<<a[5]<<' '<<a[6]<<' '<<a[0]<<' '<<a[1]<<' '<<a[2]<<' '<<a[3]<<' '<<a[4]<<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/amourjun/p/5134207.html