ZOJ-3939 The lucky week
2017-04-24 21:07
525 查看
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).
Output
For each case, print the date of the Monday of the N-th Lucky Week.
Sample Input
2
2016 4 11 2
2016 1 11 10
Sample Output
2016 7 11
2017 9 11
题意:如果每个月的1/11/21号是周一,那么这一周是lucky week给你某个lucky week的周一的日期,给出的这一天坐位第一个,问你接下来第N个lucky week的周一的日期是多少。
嗯,日期题,要打表。根据常识,日历是400年一循环。只需要从范围1753开始往后打400年的表找出所有的lucky week的周一就可以。
看题的时候猜了一下,这个年份数字很奇怪,从1.1开始,大概也是一个lucky week的周一,百度了一下果然如此~~
往后打表我是模拟了一下,一个一个往后推算了400年。
中间要考虑闰年的2月。
我这样算应该是比较麻烦的吧。
看到有别的代码是用到了直接计算某天是星期几的模板,很短,没怎么看懂 Orz…
思路很好想,但是我中间卡在了算数的问题上,,,
在计算400年的时候,从1753算到了2153,其实这样算完是401年。。。
想了好久都没发现,,,最后才想出来。。。
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).
Output
For each case, print the date of the Monday of the N-th Lucky Week.
Sample Input
2
2016 4 11 2
2016 1 11 10
Sample Output
2016 7 11
2017 9 11
题意:如果每个月的1/11/21号是周一,那么这一周是lucky week给你某个lucky week的周一的日期,给出的这一天坐位第一个,问你接下来第N个lucky week的周一的日期是多少。
嗯,日期题,要打表。根据常识,日历是400年一循环。只需要从范围1753开始往后打400年的表找出所有的lucky week的周一就可以。
看题的时候猜了一下,这个年份数字很奇怪,从1.1开始,大概也是一个lucky week的周一,百度了一下果然如此~~
往后打表我是模拟了一下,一个一个往后推算了400年。
中间要考虑闰年的2月。
我这样算应该是比较麻烦的吧。
看到有别的代码是用到了直接计算某天是星期几的模板,很短,没怎么看懂 Orz…
思路很好想,但是我中间卡在了算数的问题上,,,
在计算400年的时候,从1753算到了2153,其实这样算完是401年。。。
想了好久都没发现,,,最后才想出来。。。
#include<iostream> #include<cstring> #include<string> #include<map> #include<queue> #include<vector> #include<algorithm> #include<stdio.h> #define Max 2058 using namespace std; struct a//用来存储lucky week的周一的日期 { int y, m, d; }; a date[Max]; void f()//打表找lucky week的周一的日期 { int y, m, d, k, t;//y年份,m月份,d具体日期,k用于计算余数判断是否是周一 for (y = 1753, m = 1, d = 1, k = t = 0; y < 1753+400;) { if (d == 1 || d == 11)//每个月的1/11号不要考虑直接往后加10天进行计算 { k %= 7;//计算余数用来判断是星期几 if (k == 0)//如果是周一 { date[t].y = y; date[t].m = m; date[t].d = d; t++; } d += 10; k += 10; } else if (d == 21)//21号往后考虑到1号中间经过了多少天,大月小月分开算 { k %= 7; if (k == 0) { date[t].y = y; date[t].m = m; date[t].d = d; t++; } d = 1; if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) k += 11; else if (m == 2 || m == 4 || m == 6 || m == 9 || m == 11) { if (m == 2)//2月单独计算 { if (y % 100 == 0) { if (y % 400 == 0)//闰年 k += 9; else//平年 k += 8; } else { if (y % 4 == 0)//闰年 k += 9; else//平年 k += 8; } } else k += 10; } m++; } if (m == 13)//月份计数到了13月,,,嗯,第二年的一月 { y++; m = 1; } } } int main() { int T, n; int x, y; int i, j, k, t; int Y, M, D, N; f(); bool flag1, flag2; while (cin >> T) { for (i = 0; i < T; i++) { scanf("%d %d %d %d", &Y, &M, &D, &N); n = (Y - 1753) / 400;//计算过了几轮400年 Y = Y - n * 400; for (j = 0; j < Max; j++) { if (date[j].y == Y&&date[j].m == M&&date[j].d == D) { y = date[(j + N - 1) % Max].y + 400 * ((j + N - 1) / Max + n); printf("%d %d %d\n", y, date[(j + N - 1) % Max].m, date[(j + N - 1) % Max].d); break; } } } } return 0; }
相关文章推荐
- 每日三题-Day6-B(ZOJ 3939 The Lucky Week 找寻环节打表)
- ZOJ 3939 The Lucky Week (暴力找规律)
- ZOJ 3939 The Lucky Week 2016省赛
- ZOJ 3939 The Lucky Week
- ZOJ3939 The Lucky Week
- ZOJ 3939 The Lucky Week
- ZOJ-3939-The Lucky Week(日期循环节)
- ZOJ-3939 The Lucky Week
- ZOJ 3939 The Lucky Week (打表预处理/找规律)
- ZOJ3939-The Lucky Week
- zoj 3939 The Lucky Week(打表找循环节)
- ZOJ-3939-The Lucky Week【13th浙江省赛】【找规律】
- ZOJ 3939 The Lucky Week (400年一个周期)
- The 13th Zhejiang Provincial Collegiate Programming Contest D The Lucky Week
- zoj3939 The Lucky Week 找规律
- ZOJ3944 People Counting ZOJ3939 The Lucky Week (模拟)
- The Lucky Week
- 【ZOJ3939 The 13th Zhejiang Provincial Collegiate Programming ContestD】【日期前缀和预处理】The Lucky Week 第n个幸运
- D The Lucky Week
- poj 3487 zoj 3120 The Stable Marriage Problem 延迟认可算法(Gale-Shapley算法)