D - The Lucky Week
Edward, the headmaster of the Marjar University, is very busy every day and always forgets the date.
There was one day Edward suddenly found that if Monday was the 1st, 11th or 21st day of that month, he could remember the date clearly in that week. Therefore, he called such week "The Lucky Week".
But now Edward only remembers the date of his first Lucky Week because of the age-related memory loss, and he wants to know the date of the N-th Lucky Week. Can you help him?
Input
There are multiple test cases. The first line of input is an integer T indicating the number of test cases. For each test case:
The only line contains four integers Y, M, D and N (1 ≤ N ≤ 109) indicating the date (Y: year, M: month, D: day) of the Monday of the first Lucky Week and the Edward's query N.
The Monday of the first Lucky Week is between 1st Jan, 1753 and 31st Dec, 9999 (inclusive).
<h4< dd="">Output
For each case, print the date of the Monday of the N-th Lucky Week.
<h4< dd="">Sample Input
2 2016 4 11 2 2016 1 11 10
<h4< dd="">Sample Output
2016 7 11 2017 9 11
题目链接:ZOJ-3939
题目大意:幸运的星期指,星期一为每个月的1 or 11 or 21号。给出第一个幸运星期的时间,问第n个的日期
题目思路:比赛的时候,这道题卡了最久时间。因为刚开始题意理解错了,以为第n个最大是9999年的,于是SF了。
然后是循环节找了很久。。 循环节为400 知道了循环节就比较好写了,还有就是求年份比较绕。每400年里有2058这样的特殊天。
#include<iostream> #include<algorithm> #include<stack> #include<cmath> #include<string> #include<cstring> #include<cstdio> #include<vector> using namespace std; struct node { int y, m, d; }; vector<node>v; int xq(int x) { x = x % 7; if (x == 0) return 7; else return x; } int f(int yy) { if (yy % 400 == 0 || (yy % 100 != 0 && yy % 4 == 0)) { return 1; } else return 0; } int main() { int y = 1753; int m = 1, d = 1; int x = 1; int k = 1; node a; a.y = 1753; a.m = 1; a.d = 1; v.push_back(a); for (int i = 1; i <= 6060; i++)//打表 { while (1) { if (d == 21) { if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) { x = xq(x + 11); } else if (m != 2) { x = xq(x + 10); } else { if (f(y)) x = xq(x + 9); else x = xq(x + 8); } if (m == 12) { m = 1; d = 1; y++; } else { m++; d = 1; } if (x == 1) { node a; a.y = y; a.m = m; a.d = d; v.push_back(a); break; } continue; } if (d == 1 || d == 11) { d = d + 10; x = xq(x + 10); if (x == 1) { node a; a.y = y; a.m = m; a.d = d; v.push_back(a); break; } continue; } } } /*for (int i = 0; i < v.size(); i++) { cout << i + 1 << " " << v[i].y << " " << v[i].m << " " << v[i].d << endl; }*/ int t; scanf("%d", &t); while (t--) { int y, m, d, n; scanf("%d %d %d %d",&y,&m,&d,&n); int x; n--; x = n / 2058; n= n % 2058; int yy = y; while (y >= 2153) { y = y - 400; } for (int i = 0; i < v.size(); i++) { if (v[i].y == y&&v[i].m==m&&v[i].d==d) { printf("%d %d %d ", v[i+n].y+yy-y+x*400, v[i + n].m, v[i + n].d); break; } } } return 0; }