ZOJ - 3939 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).
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:四百年一轮回,从闰年和平年的判定可以推出。
//2:由上一条可以用程序判断出每四百年有2058个天为1,11,21的星期一,直接用。
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<map>
#include<cmath>
const int maxn=1e5+5;
typedef long long ll;
using namespace std;
int a[13]= {0,31,28,31,30,31,30,31,31,30,31,30,31};
int b[13]= {0,31,29,31,30,31,30,31,31,30,31,30,31};
bool run(int x) {
if((x%4==0&&x%100!=0)||x%400==0) {
return true;
} else {
return false;
}
}
int main() {
int T;
cin>>T;
int year ,month ,day,N;
while(T--) {
scanf("%d%d%d%d",&year,&month,&day,&N);
year +=(N/2058)*400;
N%=2058;
int k=day;
int cnt=0;
int s=1;
while(s<=N) {
if(run(year)) {
while(day<=b[month]) {
if((cnt)%7==0&&(day==1||day==11||day==21)) {
if(s==N)
{
cout<<year<<" "<<month<<" "<<day<<endl;
}
s++;
}
cnt++;
day++;
}
day=1;
month++;
if(month>12) {
month=1;
year++;
}
}
else {
while(day<=a[month]) {
if((cnt)%7==0&&(day==1||day==11||day==21)) {
if(s==N)
{
cout<<year<<" "<<month<<" "<<day<<endl;
}
s++;
}
cnt++;
day++;
}
day=1;
month++;
if(month>12) {
month=1;
year++;
}
}
}
}
return 0;
}
ZOJ - 3939 The Lucky Week(日期循环节+思维)的更多相关文章
- ZOJ 3939 The Lucky Week (暴力找规律)
题意:给定一个幸运日,求第 k 个幸运日是多少. 析:由于闰年,每400肯定会循环一次,所以我们就可以先找出每400年会有多少幸运日,是2058个,然后再暴力. 代码如下: #pragma comme ...
- D - The Lucky Week ZOJ - 3939 (思维)
题目链接: D - The Lucky Week ZOJ - 3939 题目大意:幸运的星期指,星期一为每个月的1 or 11 or 21号.给出第一个幸运星期的时间,问从当前的日起开始.第n个的日 ...
- 蓝桥杯-循环节长度-java
/* (程序头部注释开始) * 程序的版权和版本声明部分 * Copyright (c) 2016, 广州科技贸易职业学院信息工程系学生 * All rights reserved. * 文件名称: ...
- HDU 5895 Mathematician QSC(矩阵乘法+循环节降幂+除法取模小技巧+快速幂)
传送门:HDU 5895 Mathematician QSC 这是一篇很好的题解,我想讲的他基本都讲了http://blog.csdn.net/queuelovestack/article/detai ...
- hdu 2837 Calculation 指数循环节套路题
Calculation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 3746 (KMP求最小循环节) Cyclic Nacklace
题意: 给出一个字符串,要求在后面添加最少的字符是的新串是循环的,且至少有两个循环节.输出最少需要添加字符的个数. 分析: 假设所给字符串为p[0...l-1],其长度为l 有这样一个结论: 这个串的 ...
- Period(KMP,循环节问题)
题意: 求给你个串,前i位子串由某个字符串重复k次得到,求所有的i和k 分析: i-next[i]恰好是一个循环节 #include <map> #include <set> ...
- uva202:循环小数(循环节+抽屉原理)
题意: 给出两个数n,m,0<=n,m<=3000,输出n/m的循环小数表示以及循环节长度. 思路: 设立一个r[]数组记录循环小数,u[]记录每次的count,用于标记,小数计算可用 r ...
- poj 1961 Period【求前缀的长度,以及其中最小循环节的循环次数】
Period Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 14653 Accepted: 6965 Descripti ...
随机推荐
- Python爬虫实战六之抓取爱问知识人问题并保存至数据库
大家好,本次为大家带来的是抓取爱问知识人的问题并将问题和答案保存到数据库的方法,涉及的内容包括: Urllib的用法及异常处理 Beautiful Soup的简单应用 MySQLdb的基础用法 正则表 ...
- Ubuntu下安装配置ScrumWorks
1) 安装JDK6 Ubuntu默认的是OpenJDK,而ScrumWorks不支持使用OpenJDK哦,一次必须装个Oracle的JDK6 2) 下载安装Mysql5 http://dev.my ...
- 创建EDM
在学习linq过程中,我们难免会要创建EDM,这里简单的介绍一下EDM的创建过程 图示如下: 1.右击→添加→新建项→数据→Ado.net实体数据模型 选择适当的数据库,表后点击完成,vs中会自动生成 ...
- select, iocp, epoll,kqueue及各种I/O复用机制
http://blog.csdn.net/heyan1853/article/details/6457362 首先,介绍几种常见的I/O模型及其区别,如下: blocking I/O nonblock ...
- 深入理解java虚拟机(十) Java 虚拟机运行时栈帧结构
运行时栈帧结构 栈帧(Stack Frame) 是用于虚拟机执行时方法调用和方法执行时的数据结构,它是虚拟栈数据区的组成元素.每一个方法从调用到方法返回都对应着一个栈帧入栈出栈的过程. 每一个栈帧在编 ...
- 自制Java中的Mutex类
同步问题中,一个很重要的问题是同步的域,什么是同步的域呢?简单以 synchronized 这个关键字来说,就是它所同步的范围.并发编程中很多时候出现的问题没有选好同步范围所导致的.但现有的同步关键字 ...
- delphi实现窗体组建随窗体大小改变而改变
在网上查了许多资料去论述如何在dephi中去实现组件随窗口大小的变化而变化,然都不尽如人意.有人说用组件的align + anchors 这两个属性去控制,但是我用了之后,让我大惊失色.把anchor ...
- Android自定义组件之自动换行及宽度自适应View:WordWrapView
目的: 自定义一个ViewGroup,里面的子view都是TextView,每个子view TextView的宽度随内容自适应且每行的子View的个数自适应,并可以自动换行 一:效果图 二:代码 整 ...
- 双系统Grub引导下恢复windows引导的方法
此方法适用于windows系统正常,linux和windows 双系统下恢复windows系统引导.需要使用windows安装u盘 1. 启动至windows安装u盘,点击修复计算机 2. 进入命令行 ...
- .NET WCF Return String 字符串有反斜杠的处理
应该是: {"Message":"Hello World"} 结果是:" {\"Message\":\"Hello Wo ...