H - May Day Holiday

本文介绍了一个简单的算法,用于计算Marjar大学每年五一劳动节连续假期的天数。该算法考虑了周末与五一假期的相邻关系,使得实际连续假期可能延长至九天。通过输入年份,程序能输出该年五一假期的实际长度。

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 <cstring>

using namespace std;

int main()
{
    int a[10]= {6,9,6,5,5,5,5};
    int t,y,p,py,day;
    while(cin>>t)
    {
        while(t--)
        {
            cin>>y;
            day=247;
            p=1928;
            for(int i=p+1; i<=y; i++)
            {
                if((i%4==0&&i%100!=0)||i%400==0)
                {
                    day+=(366%7);
                }
                else
                    day+=(365%7);
            }
            day%=7;;
            cout<<a[day]<<endl;
        }
    }
    return 0;
}

setwd("E:/kaggle data/bike sharing") #loading the required libraries library(rpart) library(rattle) library(rpart.plot) library(RColorBrewer) library(randomForest) # reading the data files train=read.csv("train_bike.csv") test=read.csv("test_bike.csv") str(train) # introducing variables in test to combine train and test # can also be done by removing the same variables from training data test$registered=0 test$casual=0 test$count=0 data=rbind(train,test) # getting some information about the combined data str(data) summary(data) # factoring some variables from numeric data$season=as.factor(data$season) data$weather=as.factor(data$weather) data$holiday=as.factor(data$holiday) data$workingday=as.factor(data$workingday) # extracting hour from the datetime variable data$hour=substr(data$datetime,12,13) data$hour=as.factor(data$hour) # dividing again into train and test train=data[as.integer(substr(data$datetime,9,10))<20,] test=data[as.integer(substr(data$datetime,9,10))>19,] # creating some boxplots on the count of rentals boxplot(train$count~train$hour,xlab="hour", ylab="count of users") boxplot(train$casual~train$hour,xlab="hour", ylab="casual users") boxplot(train$registered~train$hour,xlab="hour", ylab="registered users") # extracting days of week from datetime date=substr(data$datetime,1,10) days<-weekdays(as.Date(date)) data$day=days train=data[as.integer(substr(data$datetime,9,10))<20,] test=data[as.integer(substr(data$datetime,9,10))>19,] # creating boxplots for rentals with different variables to see the variation boxplot(train$registered~train$day,xlab="day", ylab="registered users") boxplot(train$casual~train$day,xlab="day", ylab="casual users") boxplot(train$registered~train$weather,xlab="weather", ylab="registered users") boxplot(train$casual~train$weather,xlab="weather", ylab="casual users") boxplot(train$registered~train$temp,xlab="temp", ylab="registered users") boxplot(train$casual~train$temp,xlab="temp", ylab="casual users") # extracting year from data data$year=substr(data$datetime,1,4) data$year=as.factor(data$year) # ignore the division of data again and again, this could have been done together also train=data[as.integer(substr(data$datetime,9,10))<20,] test=data[as.integer(substr(data$datetime,9,10))>19,] # again some boxplots with different variables # these boxplots give important information about the dependent variable with respect to the independent variables boxplot(train$registered~train$year,xlab="year", ylab="registered users") boxplot(train$casual~train$year,xlab="year", ylab="casual users") boxplot(train$registered~train$windspeed,xlab="year", ylab="registered users") boxplot(train$casual~train$windspeed,xlab="year", ylab="casual users") boxplot(train$registered~train$humidity,xlab="humidity", ylab="registered users") boxplot(train$casual~train$humidity,xlab="humidity", ylab="casual users") data$hour=as.integer(data$hour) # created this variable to divide a day into parts, but did not finally use it data$day_part=0 train=data[as.integer(substr(data$datetime,9,10))<20,] test=data[as.integer(substr(data$datetime,9,10))>19,] data=rbind(train,test) #using decision trees for binning some variables, this was a really important step in feature engineering d=rpart(registered~hour,data=train) fancyRpartPlot(d) d=rpart(casual~hour,data=train) fancyRpartPlot(d) data=rbind(train,test) data$dp_reg=0 data$dp_reg[data$hour<8]=1 data$dp_reg[data$hour>=22]=2 data$dp_reg[data$hour>9 & data$hour<18]=3 data$dp_reg[data$hour==8]=4 data$dp_reg[data$hour==9]=5 data$dp_reg[data$hour==20 | data$hour==21]=6 data$dp_reg[data$hour==19 | data$hour==18]=7 data$dp_cas=0 data$dp_cas[data$hour<=8]=1 data$dp_cas[data$hour==9]=2 data$dp_cas[data$hour>=10 & data$hour<=19]=3 data$dp_cas[data$hour>19]=4 f=rpart(registered~temp,data=train) fancyRpartPlot(f) f=rpart(casual~temp,data=train) fancyRpartPlot(f) data$temp_reg=0 data$temp_reg[data$temp<13]=1 data$temp_reg[data$temp>=13 & data$temp<23]=2 data$temp_reg[data$temp>=23 & data$temp<30]=3 data$temp_reg[data$temp>=30]=4 data$temp_cas=0 data$temp_cas[data$temp<15]=1 data$temp_cas[data$temp>=15 & data$temp<23]=2 data$temp_cas[data$temp>=23 & data$temp<30]=3 data$temp_cas[data$temp>=30]=4 data$year_part[data$year=='2011']=1 data$year_part[data$year=='2011' & data$month>3]=2 data$year_part[data$year=='2011' & data$month>6]=3 data$year_part[data$year=='2011' & data$month>9]=4 data$year_part[data$year=='2012']=5 data$year_part[data$year=='2012' & data$month>3]=6 data$year_part[data$year=='2012' & data$month>6]=7 data$year_part[data$year=='2012' & data$month>9]=8 table(data$year_part) # creating another variable day_type which may affect our accuracy as weekends and weekdays are important in deciding rentals data$day_type=0 data$day_type[data$holiday==0 & data$workingday==0]="weekend" data$day_type[data$holiday==1]="holiday" data$day_type[data$holiday==0 & data$workingday==1]="working day" train=data[as.integer(substr(data$datetime,9,10))<20,] test=data[as.integer(substr(data$datetime,9,10))>19,] plot(train$temp,train$count) data=rbind(train,test) data$month=substr(data$datetime,6,7) data$month=as.integer(data$month) # dividing total data depending on windspeed to impute/predict the missing values table(data$windspeed==0) k=data$windspeed==0 wind_0=subset(data,k) wind_1=subset(data,!k) # predicting missing values in windspeed using a random forest model # this is a different approach to impute missing values rather than just using the mean or median or some other statistic for imputation set.seed(415) fit <- randomForest(windspeed ~ season+weather +humidity +month+temp+ year+atemp, data=wind_1,importance=TRUE, ntree=250) pred=predict(fit,wind_0) wind_0$windspeed=pred data=rbind(wind_0,wind_1) data$weekend=0 data$weekend[data$day=="Sunday" | data$day=="Saturday"]=1 str(data) # converting all relevant categorical variables into factors to feed to our random forest model data$season=as.factor(data$season) data$holiday=as.factor(data$holiday) data$workingday=as.factor(data$workingday) data$weather=as.factor(data$weather) data$hour=as.factor(data$hour) data$month=as.factor(data$month) data$day_part=as.factor(data$dp_cas) data$day_type=as.factor(data$dp_reg) data$day=as.factor(data$day) data$temp_cas=as.factor(data$temp_cas) data$temp_reg=as.factor(data$temp_reg) train=data[as.integer(substr(data$datetime,9,10))<20,] test=data[as.integer(substr(data$datetime,9,10))>19,] # log transformation for some skewed variables, which can be seen from their distribution train$reg1=train$registered+1 train$cas1=train$casual+1 train$logcas=log(train$cas1) train$logreg=log(train$reg1) test$logreg=0 test$logcas=0 boxplot(train$logreg~train$weather,xlab="weather", ylab="registered users") boxplot(train$logreg~train$season,xlab="season", ylab="registered users") # final model building using random forest # note that we build different models for predicting for registered and casual users # this was seen as giving best result after a lot of experimentation set.seed(415) fit1 <- randomForest(logreg ~ hour +workingday+day+holiday+ day_type +temp_reg+humidity+atemp+windspeed+season+weather+dp_reg+weekend+year+year_part, data=train,importance=TRUE, ntree=250) pred1=predict(fit1,test) test$logreg=pred1 set.seed(415) fit2 <- randomForest(logcas ~hour + day_type+day+humidity+atemp+temp_cas+windspeed+season+weather+holiday+workingday+dp_cas+weekend+year+year_part, data=train,importance=TRUE, ntree=250) pred2=predict(fit2,test) test$logcas=pred2 #creating the final submission file test$registered=exp(test$logreg)-1 test$casual=exp(test$logcas)-1 test$count=test$casual+test$registered s<-data.frame(datetime=test$datetime,count=test$count) write.csv(s,file="submit.csv",row.names=FALSE) 修改为python代码
最新发布
12-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值