USACO Black Friday

Topic Description: The
13th is a Friday. No. 13 on Friday less than on other days it? To answer this question, write a procedure that requires the calculation of the number of times the thirteenth month from Monday to Sunday falls. In a given cycle N, the required calculation January 1, 1900 to 1900 + N-1年thirteenth Japan on December 31 falls on a Sunday, Monday to frequency, N is a positive integer not more than 400.
Here there is something you need to know:
1, January 1, 1900 is Monday
. 2,4,6,11 and September other months have 30 days except February has 31 days leap year, February has 29 days, non-leap year February has 28 days
3 year divisible by 4 is a leap year (1992 = 4 * 498 so 1992 is a leap year, but 1990 is not a leap year).
4, the above rules are not suitable for century years. It can be divisible by 400 Century as a leap year, otherwise average. So, 1700,1800,1900 and 2100 is a leap year, but 2000 is a leap year
, please do not call the existing function
, do not pre-considered a good data (not allowed to play table is called)!
Input formats:
A positive integer n
output format :
output start at Saturday
Example:
input # 1
20 is
output # 1
36 35 35 33 is 34 is 33 is 34 is

the subject read my first thought is to determine leap year, then leap year judgment function defines:

int judge(int year)
{ 
    if(year%4==0&&year%100||year%400==0) 
    {
       return 1;
    }
    return 0; 
}

The definition of what an array of memory month average year and leap year, as well as the number of days of deposit:

int run[13]={0,31,29,31,30,31,30,31,31,30,31,30,31}; 
int ping[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; 
int week[7]={0,0,0,0,0,0,0};

Input + is determined:

cin >> n;
for(i=1900;i<1900+n;i++) 
    { 
        if(judge(i)) //调用函数,如果是闰年
        { 
            for(j=1;j<=12;j++) 
            { 
                s+=13;//s增加13 
                week[s%7]++; //week数组对应的位置增加一次
                s+=(run[j]-13); //闰年天数增加
            } 
        } 
        else 
        { 
            for(j=1;j<=12;j++) //与上面基本相同,只不过增加平年天数
            { 
                s+=13; 
                week[s%7]++; 
                s+=(ping[j]-13); 
            } 
        } 
    } 

Output:

    cout<< week[6] << ' '; //一共有几个黑色星期五
    for(i=0;i<6;i++) 
	{
	    cout<<week[i]<<' ';//输出其他日期出现了几次
	} 

Complete code:

#include<bits/stdc++.h>     
using namespace std;
int judge(int year)
{ 
    if(year%4==0&&year%100||year%400==0)
	{
	   return 1; 
	} 
    return 0; 
}
int run[13]={0,31,29,31,30,31,30,31,31,30,31,30,31}; 
int ping[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; 
int week[7]={0,0,0,0,0,0,0}; 
int main() 
{ 
    int n,s=0; 
    int i,j; 
    cin>>n; 
    for(i=1900;i<1900+n;i++) 
    { 
        if(judge(i)) 
        { 
            for(j=1;j<=12;j++) 
            { 
                s+=13; 
                week[s%7]++; 
                s+=(run[j]-13); 
            } 
        } 
        else 
        { 
            for(j=1;j<=12;j++) 
            { 
                s+=13; 
                week[s%7]++; 
                s+=(ping[j]-13); 
            } 
        } 
    } 
    cout<< week[6] << ' '; 
    for(i=0;i<6;i++) 
	{
	    cout<<week[i]<<' ';
	} 
    return 0; 
}

Today's explanation on here, you understand? Remember thumbs up!

Released three original articles · won praise 3 · views 61

Guess you like

Origin blog.csdn.net/amu_yu/article/details/105326358