问题描述
试题编号: | 201503-3 |
试题名称: | 节日 |
时间限制: | 1.0s |
内存限制: | 256.0MB |
问题描述: | 问题描述 有一类节日的日期并不是固定的,而是以“a 月的第 b 个星期 c”的形 式定下来的,比如说母亲节就定为每年的五月的第二个星期日。 现在,给你 a, b, c 和 y1, y2(1850 ≤ y1, y2 ≤ 2050),希望你输出 从公元 y1 年到公元 y2年间的每年的 a 月的第 b 个星期 c 的日期。 提示:关于闰年的规则:年份是 400 的整数倍时是闰年,否则年份是 4 的倍数并且不是 100 的倍数时是闰年,其他年份都不是闰年。例如 1900 年 就不是闰年,而 2000 年是闰年。 为了方便你推算,已知 1850 年 1 月 1 日是星期二。 输入格式 输入包含恰好一行,有五个整数 a, b, c, y1, y2。其中 c=1, 2, ……, 6, 7 分别表示星期一、二、 ……、六、日。 输出格式 对于 y1和 y2之间的每一个年份,包括 y1和 y2,按照年份从小到大的顺 序输出一行。 如果该年的 a 月第 b 个星期 c 确实存在,则以"yyyy/mm/dd"的格式输 出,即输出四位数的年份,两位数的月份,两位数的日期,中间用斜杠“/” 分隔,位数不足时前补零。 如果该年的 a 月第 b 个星期 c 并不存在,则输出"none"(不包含双引 号)。 样例输入 5 2 7 2014 2015 样例输出 2014/05/11 2015/05/10 评测用例规模与约定 所有评测用例都满足: 1 ≤ a ≤ 12, 1 ≤ b ≤ 5, 1 ≤ c ≤ 7, 1850 ≤ y1, y2≤ 2050。 |
#include<iostream>
#include<map>
#include<cmath>
using namespace std;
int main(){
int a, b, c, y1, y2;
cin >> a >> b >> c >> y1 >> y2;
if(c == 7)
c = 0;//将星期天标记为0,便于计算
map<int, bool> yeardays;
map<int, int> mondays;
yeardays.clear();
mondays.clear();
for(int i = 1850; i <= 2050; i++){
if((i%400 == 0) || (i%4 == 0 && i%100 != 0))
yeardays[i] = true;
else
yeardays[i] = false;
}
for(int i = 1; i <= 12; i++){
if(i==1 || i==3 || i==5 || i==7 || i==8 || i==10 || i==12)
mondays[i] = 31;
else if(i==2)
mondays[i] = 28;//注意处理这里
else
mondays[i] = 30;
}
int dayscnt = 0;
for(int y = y1; y <= y2; y++){
dayscnt = 0;
//处理到y的1月1日
for(int prey = 1850; prey < y; prey++){
if(yeardays[prey])
dayscnt += 366;
else
dayscnt += 365;
}
//处理到y的指定月份
for(int prem = 1; prem < a; prem++){
dayscnt += mondays[prem];
if(prem == 2 && yeardays[y])
dayscnt += 1;
}
//计算该月第一天为星期几
int firstday = (2 + dayscnt) % 7;//0为星期日
//定位到该月第1个星期c的日期
int firstc;
if(c >= firstday)
firstc = 1 + (c-firstday);
else
firstc = 1 + (7 - (firstday - c));
//定位到该月第b个星期c的日期
int date;
date = firstc + (b-1)*7;
//判断与输出
int thismondays;
if(yeardays[y] && a == 2)
thismondays = 29;
else
thismondays = mondays[a];
if(date > thismondays)
cout << "none" << endl;
else{
cout << y << "/";
if(a < 10)
cout << "0";
cout << a << "/" << date << endl;
}
}
return 0;
}