纪念我的一次过的题目
啦啦啦啦啦啦啦啦
思路:先计算1850年1月1日到每年year年a月1日的天数days,int weekday = days%7+2就是a月1日为星期weekday。知道了要计算的月份第一天为星期几,再去计算第a个月的第b个星期c,
#include <iostream>
using namespace std;
const int INF = 0x3f3f3f3f;
bool isRun(int x)
{
if(x % 400 == 0) return true;
if(x % 4 == 0 && x % 100 != 0) return true;
return false;
}
int rmonths[13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int months[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int a, b, c, y1, y2;
int get_date(int year)
{
long total_days = 0;
int flag = true; // true 表示今年闰年
if(isRun(year)) for(int i = 1; i<=a-1; i++) total_days += rmonths[i];
else
{
for(int i = 1; i<=a-1; i++) total_days += months[i];
flag = false;
}
for(int i = 1850; i < year; i++)
{
if(isRun(i)) total_days += 366;
else total_days += 365;
}
int week_day = total_days % 7 + 2; // week_day 表示当前月份的 1 号是星期几
if(week_day == 8) week_day = 1;
if(week_day == 9) week_day = 2;
int days = 0;
// if(c >= week_day) days += c - week_day;
// else days += c + 7 - week_day;
days += (c - week_day + 7) % 7;
days = days + (b-1) * 7;
if(flag)
{
if(days + 1 > rmonths[a]) return INF;
}
else if(days + 1 > months[a]) return INF;
return days;
}
int main()
{
cin>>a>>b>>c>>y1>>y2;
if(y1 > y2) swap(y1, y2);
for(int i = y1; i<=y2; i++)
{
int date = get_date(i);
if(date == INF) cout<<"none"<<endl;
else
{
date += 1;
printf("%d/%02d/%02d\n", i, a, date);
}
}
}
网友评论