C++程序设计:今夕何夕,见此良人(经典题目)
【问题描述】
今天是2017年8月6日,农历闰六月十五。
小度独自凭栏,望着一轮圆月,发出了“今夕何夕,见此良人”的寂寞感慨。
为了排遣郁结,它决定思考一个数学问题:接下来最近的哪一年里的同一个日子,和今天的星期数一样?比如今天是8月6日,星期日。下一个也是星期日的8月6日发生在2023年。
小贴士:在公历中,能被4整除但不能被100整除,或能被400整除的年份即为闰年。
【输入形式】
第一行为T,表示输入数据组数。
每组数据包含一个日期,格式为YYYY-MM-DD。其中1 ≤ T ≤ 10000, YYYY ≥ 0。
日期一定是个合法的日期。
【输出形式】
对每组数据输出答案年份,题目保证答案不会超过四位数。
【样例输入】
3
2017-08-06
2017-08-07
2018-01-01
【样例输出】
2023
2023
2024
#include <iostream>
using namespace std;
bool answer(int year) {
bool x=0;
if(year%4==0&&year%100!=0)
x=1;
if(year%400==0)
x=1;
return x;
}
void next(int year,bool x,bool y) {
int sum=0;
int count=0;
do {
if(answer(year+count)==false) {
if (y==false) {
if(answer(year+count+1)) {
sum+=366;
goto here;
} else {
sum+=365;
goto here;
}
} else {
sum+=365;
goto here;
}
} else {
if (y==false) {
sum+=365;
goto here;
} else {
sum+=366;
goto here;
}
}
here:
count++;
} while(sum%7!=0);
cout<<year+count<<endl;
}
int main() {
bool x,y;
int n=0,i,p1;
char p2;
cin>>n;
int year[n];
int month[n];
for(i=0; i<n; i++) {
cin>>year[i]>>p2>>month[i]>>p2>>p1;
}
for(i=0; i<n; i++) {
y=false;
x=answer(year[i]);
if(month[i]<=2) y=true;
next(year[i],x,y);
}
return 0;
}