H - May Day Holiday

本文介绍了一个算法,用于计算Marjar大学每年五一劳动节连续假期的天数。该算法考虑了闰年和平年的区别,并根据实际情况计算出最长可达九天的连续假期。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

As a university advocating self-learning and work-rest balance, Marjar University has so many days of rest, including holidays and weekends. Each weekend, which consists of Saturday and Sunday, is a rest time in the Marjar University.

The May Day, also known as International Workers' Day or International Labour Day, falls on May 1st. In Marjar University, the May Day holiday is a five-day vacation from May 1st to May 5th. Due to Saturday or Sunday may be adjacent to the May Day holiday, the continuous vacation may be as long as nine days in reality. For example, the May Day in 2015 is Friday so the continuous vacation is only 5 days (May 1st to May 5th). And the May Day in 2016 is Sunday so the continuous vacation is 6 days (April 30th to May 5th). In 2017, the May Day is Monday so the vacation is 9 days (April 29th to May 7th). How excited!

Edward, the headmaster of Marjar University, is very curious how long is the continuous vacation containing May Day in different years. Can you help him?

Input

There are multiple test cases. The first line of input contains an integer Tindicating the number of test cases. For each test case, there is an integer y(1928 <= y <= 9999) in one line, indicating the year of Edward's query.

Output

For each case, print the number of days of the continuous vacation in that year.

Sample Input
3
2015
2016
2017
Output
5
6

9

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
int a[10000];
int main()
{
    int t,n,i;
    a[2015] = 5;
    for(i=2016;i<=9999;i++){
        if(i%4==0&&i%100!=0||i%400==0)
            a[i] = a[i-1]+2;
        else
            a[i] = a[i-1]+1;
        if(a[i]>7)
            a[i] -= 7;
    }
    for(i=2014;i>=1928;i--){
        if((i+1)%4==0&&(i+1)%100!=0||(i+1)%400==0)
            a[i] = a[i+1] - 2;
        else
            a[i] = a[i+1] -1;
        if(a[i]<=0)
            a[i]+=7;
    }
    while(cin>>t){
        while(t--){
            cin>>n;
            if(a[n]==1)
                cout<<"9"<<endl;
            else if(a[n]==7||a[n]==2)
                cout<<"6"<<endl;
            else
                cout<<"5"<<endl;
        }
    }
    return 0;
}

%% 1. 读入数据 data = readtable('附件2.csv','ReadVariableNames',false); data.Properties.VariableNames = {'VehicleType','Timestamp','ID','Location'}; %% 2. 时间解析 ts = datetime(data.Timestamp,'InputFormat','yyyy-MM-dd''T''HH:mm:ss.SSS'); dateOnly = dateshift(ts,'start','day'); hr = hour(ts); %% 3. 构造日期列表 startDate = datetime(2024,4,1); endDate = datetime(2024,5,6); allDates = startDate:endDate; % 36×1 nDay = numel(allDates); %% 4. 日期类型标记 1=工作日 2=周末 3=节假日 qingMing = datetime(2024,4,4):datetime(2024,4,6); mayDay = datetime(2024,5,1):datetime(2024,5,5); holidayDates = [qingMing,mayDay]; dateType = ones(nDay,1); % 先全置为工作日 for k = 1:nDay if ismember(allDates(k),holidayDates) dateType(k) = 3; % 节假日 elseif weekday(allDates(k))==1 || weekday(allDates(k))==7 dateType(k) = 2; % 周末 end end %% 5. 初始化计数矩阵:日期 × 小时 count24 = zeros(nDay,24); %% 6. 逐条记录累加 for r = 1:height(data) d = dateOnly(r); h = hr(r) + 1; % 0→1,…,23→24 idx = find(allDates==d,1); if ~isempty(idx) && h>=1 && h<=24 count24(idx,h) = count24(idx,h) + 1; end end %% 7. 按日期类型求平均 isW = dateType==1; isE = dateType==2; isH = dateType==3; avgW = mean(count24(isW,:),1,'omitnan'); % 1×24 avgE = mean(count24(isE,:),1,'omitnan'); avgH = mean(count24(isH,:),1,'omitnan'); %% 8. 画图 t = 0:23; figure('Position',[200 200 800 350]); plot(t,avgW,'-o','LineWidth',2); hold on; plot(t,avgE,'-s','LineWidth',2); plot(t,avgH,'-^','LineWidth',2); xlabel('小时'); ylabel('平均车辆数'); legend('工作日','周末','节假日','Location','best'); grid on; xlim([0 23]); xticks(0:23); title('各时段平均交叉口流量(4.1-5.6)'); %% 9. 输出 Excel out = table(t.', avgW.', avgE.', avgH.', ... 'VariableNames',{'Hour','Workday','Weekend','Holiday'}); writetable(out,'Hourly_Average_Traffic.xlsx'); disp('完成!结果已保存到 Hourly_Average_Traffic.xlsx');给这个代码加上柱状图
最新发布
07-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值