1.运行结果
2.思路解析
由题意可知只需求出日期day即可,对于每一个year,以1850年1月1号为参考点,计算出year年a月1号是星期几,进而推算出year年a月的第一个星期c是哪个日期day,通过day+=(b-1)*7求出year年a月的第b个星期c的日期。
最后判断一下算出的日期是否合法,根据要求输出即可。
注意细节!!一定要特别考虑闰年二月的情况。
3.满分代码
#include<iostream>
using namespace std;
int m[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
bool is_leap(int year)//判断闰年
{
if(year%400==0 || year%4==0&&year%100!=0)return true;
else
return false;
}
bool isday(int year,int month,int day)//判断是否是合法日期
{
if(day<=0||day>31)return false;
if(month==2)
{
if(is_leap(year)&&day>29)return false;
if(!is_leap(year)&&day>28)return false;
}
else
{
if(day>m[month])return false;
}
return true;
}
int main()
{
int a,b,c,y1,y2;
cin>>a>>b>>c>>y1>>y2;
for(int year=y1;year<=y2;year++)
{
long long sum=0;
for(int i=1850;i<year;i++)
{
if(is_leap(i))sum+=366;
else sum+=365;
}
for(int i=1;i<a;i++)
{
if((i==2)&&is_leap(year))sum+=29; //一定要考虑闰年2月的特殊情况!!!
else sum+=m[i];
}
int tmp=(sum%7+2)%7;//每年 a 月 1号是星期 tmp
if(tmp==0)tmp=7;
int day;//每年 a 月 day 号是星期 c
if(c>=tmp)
day=1+c-tmp;
else
day=1+(7-tmp)+c;
day+=(b-1)*7;
if(isday(year,a,day))
{
cout<<year<<"/";
if(a<=9)
cout<<"0"<<a<<"/";
else
cout<<a<<"/";
if(day<10)
cout<<"0"<<day<<endl;
else
cout<<day<<endl;
}
else
cout<<"none"<<endl;
}
return 0;
}