1341. 十三号星期五 枚举

文章描述了一个编程问题,要求通过编程计算从1900年到指定年份的每个月13号是星期几的频率,包括星期六、日、一至周五。提供了两种方法,一种是按月份枚举,另一种是使用基姆拉尔森公式。
摘要由CSDN通过智能技术生成

十三号星期五真的很不常见吗?

每个月的十三号是星期五的频率是否比一周中的其他几天低?

请编写一个程序,计算 N年内每个月的 13号是星期日,星期一,星期二,星期三,星期四,星期五和星期六的频率。

测试的时间段将会开始于 1900年 1 月 1 日,结束于 1900+N−1 年 12 月 31日。

一些有助于你解题的额外信息:1900年 1 月 1日是星期一。在一年中,4月、6 月、9 月、11 月每个月 30 天,2 月平年 28 天,闰年 29天,其他月份每个月31天。公历年份是 4的倍数且不是 100 的倍数的年份为闰年,例如 1992 年是闰年,1990年不是闰年。公历年份是整百数并且是 400的倍数的也是闰年,例如1700年,1800年,1900年,2100年不是闰年,2000年是闰年。

输入格式

共一行,包含一个整数 N。

输出格式

共一行,包含七个整数,整数之间用一个空格隔开,依次表示星期六,星期日,星期一,星期二,星期三,星期四,星期五在十三号出现的次数。

数据范围

1≤N≤400

输入样例:
20
输出样例:
36 33 34 33 35 35 34

按月份枚举

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};  // 打表
int weekday[7];//存储每个一共多少

int main()
{
    int n;
    cin >> n;//读入多少年

    int days = 0;//来表示当前一月一号距离1900年1月1号过了多沙天
    for (int year = 1900; year < 1900 + n; year ++ )//枚举年
    {
        for (int i = 1; i <= 12; i ++ )//枚举月
        {
            weekday[(days + 12) % 7] ++ ;//判断13号是周几存进去
            days += month[i];
            if (i == 2)//判断闰年
            {
                if (year % 100 && year % 4 == 0 || year % 400 == 0)
                    days ++ ;
            }
        }
    }

    for (int i = 5, j = 0; j < 7; i = (i + 1) % 7, j ++ )
        cout << weekday[i] << ' ';
    cout << endl;

    return 0;
}

按天枚举

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int months[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};  // 打表
int weekday[7];

int get(int year, int m)
{
    if (m != 2) return months[m];
    if (year % 100 && year % 4 == 0 || year % 400 == 0)
        return 29;
    return 28;
}

int main()
{
    int n;
    cin >> n;

    int week = 0;
    int year = 1900, month = 1, day = 1;
    while (year < 1900 + n)
    {
        if (day == 13) weekday[week] ++ ;
        week = (week + 1) % 7;
        day ++ ;
        if (get(year, month) < day) month ++, day = 1;
        if (month > 12) month = 1, year ++ ;
    }

    for (int i = 5, j = 0; j < 7; i = (i + 1) % 7, j ++ )
        cout << weekday[i] << ' ';
    cout << endl;

    return 0;
}

//cpp
#include <iostream>
using namespace std;

int main()
{
    int n;
    cin >> n;
    int a[7] = {0};//记录星期出现次数
    int week = 1;
    for (int y = 1900; y < 1900 + n ; y++)
    {
        for (int m = 1; m <= 12; m++)
        {
            int d;
            if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)//根据年与月算该月有多少天
            {
                d = 31;
            }
            else if (m == 2)
            {
                if ((y % 100 != 0 && y % 4 == 0) || (y % 400 == 0))
                    d = 29;
                else d = 28;
            }
            else
                d = 30;
            for (int i = 1; i <= d; i++)
            {
                week++;
                if (i == 13)//如果是 13 号,对应的星期出现次数加 1 
                    a[week % 7] ++;

            }
        }
    }
    for (int i = 0; i < 7; i++) cout << a[i] << " ";
    cin >> n;
}

作者:Hasity
链接:https://www.acwing.com/solution/content/30656/
来源:AcWing

公式法:

基母拉尔森公式:

https://baike.so.com/doc/2738265-2890169.html

W= (d+2m+3(m+1)/5+y+y/4-y/100+y/400+1)%7 //C++计算公式

在公式中d表示日期中的日数,m表示月份数,y表示年数。

注意:在公式中有个与其他公式不同的地方:

把一月和二月看成是上一年的十三月和十四月,例:如果是2004-1-10则换算成:2003-13-10来代入公式计算。
输入年月日到公式,返回当天的星期数。

//cpp
#include <iostream>
using namespace std;
int week(int y, int m, int d)
{
    if (m == 1 || m == 2)
        m = m + 12, y--;
    return (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400 + 1) % 7;//公式
}
int main()
{
    int n = 0;
    cin >> n;
    int weekday[7] = {0};//存储星期的天数
    for (int y = 1900; y < 1900 + n; y++)
        for (int m = 1; m <= 12; m++)
        {
            int w = week(y, m, 13);//公式计算每一年,每一月的13号是星期几
            weekday[w]++;//对应星期天数加 1
        }

    for(int i = 6; i < 6 + 7; i++)
        cout << weekday[i % 7] << " ";
    return 0;
}

作者:Hasity
链接:https://www.acwing.com/solution/content/30656/
来源:AcWing

  • 12
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值