核心思想
日期问题是一个在模拟题里是一个比较常考的考点, 有多种考法!其中有一个核心思想就是尽量把日期转化成单维的天数
日期基础知识
日期 年月日, y, m, d 年分为闰年和平年, 闰年的判断方法: year % 400 == 0 year % 4 == 0 && year % 100 != 0 月份, 大月31天, 小月30天 {31, 28/29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
日期代码模板
bool isleap(int year) {
if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
return true;
return false;
}
int sum = 0;
int month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
for (int i = 1; i < m; i++)
sum += month[i];
if (isleap(y) && m > 2) adays += 1;
sum += d;
日期题的小分类(其他的类似)
1, 求一年中的第几天 2, 给天数求日期 3, 给日期, 加或者减xx天数, 另算日期 4, 给日期, 计算周几 …等等 我相信吃透这几道类型大致就可以告别模拟题-日期类
KY19 今年的第几天? KY222 打印日期 KY258 日期累加 KY111 日期差值 KY108 Day of Week KY250 日期类
Accept
[KY 19 第几天?]
#include <bits/stdc++.h>
using namespace std;
int month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int year, mon, day;
int main() {
while (cin >> year >> mon >> day) {
int sum = 0;
if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) {
if (mon > 2) sum += 1;
}
for (int i = 1; i < mon; i++) sum += month[i];
cout << sum+day << endl;
}
return 0;
}
[KY222 打印日期]
#include <bits/stdc++.h>
using namespace std;
int month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int year, days;
int main() {
while (cin >> year >> days) {
int mon = 1;
bool tag = false;
if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) {
month[2] += 1;
tag = true;
}
for (int i = 1; i <= 12; i++) {
if (days > month[i]) {
days -= month[i];
mon += 1;
} else {
break;
}
}
if (tag)
month[2] -= 1;
printf("%4d-%02d-%02d\n", year, mon, days);
}
return 0;
}
[KY258 日期累加]
#include <bits/stdc++.h>
using namespace std;
int month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int year, mon, day, acdays;
bool isleap(int year) {
if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
return true;
return false;
}
int main() {
int m; cin >> m;
for (int j = 0; j < m; j++) {
month[2] = 28;
int yk = 365;
cin >> year >> mon >> day >> acdays;
int sum = 0;
if (isleap(year) && mon > 2) {
month[2] = 29;
yk = 366;
}
for (int i = 1; i < mon; i++) {
sum += month[i];
}
sum += (day + acdays);
while (sum > yk) {
year += 1;
sum -= yk;
if (isleap(year))
yk = 366;
else yk = 365;
}
int index = 1;
while (sum > month[index]) {
sum -= month[index];
index ++;
if (index == 2 && isleap(year)) month[index] = 29;
else if (index == 2 && !isleap(year)) month[index] = 28;
}
printf("%4d-%02d-%02d\n", year, index, sum);
}
return 0;
}
[KY111 日期差值]
#include <bits/stdc++.h>
using namespace std;
int ay,by,am,bm,ad,bd;
int month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool isleap(int year) {
if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
return true;
return false;
}
int main() {
while (scanf("%4d%2d%2d%4d%2d%2d", &ay, &am, &ad, &by, &bm, &bd) != EOF) {
if (ay <= by) {
int adays = 0;
for (int i = 1; i < am; i++)
adays += month[i];
if (isleap(ay) && am > 2) adays += 1;
adays += ad;
int bdays = 0;
while (ay < by) {
if (isleap(ay)) bdays += 366;
else bdays += 365;
ay++;
}
for (int i = 1; i < bm; i++)
bdays += month[i];
if (isleap(by) && bm > 2) bdays += 1;
bdays += bd;
printf("%d\n", bdays-adays+1);
}
else {
int bdays = 0;
for (int i = 1; i < bm; i++)
bdays += month[i];
if (isleap(by) && bm > 2) bdays += 1;
bdays += bd;
int adays = 0;
while (by < ay) {
if (isleap(by)) adays += 366;
else adays += 365;
by++;
}
for (int i = 1; i < am; i++)
adays += month[i];
if (isleap(ay) && am > 2) adays += 1;
adays += ad;
printf("%d\n", adays-bdays);
}
}
return 0;
}
[KY108 Day of Week]
#include <bits/stdc++.h>
using namespace std;
int ay,by,am,bm,ad,bd;
int month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool isleap(int year) {
if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
return true;
return false;
}
int num(string s) {
if (s == "January") return 1;
else if (s == "February") return 2;
else if (s == "March") return 3;
else if (s == "April") return 4;
else if (s == "May") return 5;
else if (s == "June") return 6;
else if (s == "July") return 7;
else if (s == "August") return 8;
else if (s == "September") return 9;
else if (s == "October") return 10;
else if (s == "November") return 11;
else if (s == "December") return 12;
return 0;
}
void numweek(int w) {
if (w == 1) cout << "Monday" << endl;
else if (w == 2) cout << "Tuesday" << endl;
else if (w == 3) cout << "Wednesday" << endl;
else if (w == 4) cout << "Thursday" << endl;
else if (w == 5) cout << "Friday" << endl;
else if (w == 6) cout << "Saturday" << endl;
else if (w == 0) cout << "Sunday" << endl;
}
int main() {
int d, y; string mon;
while (cin >> d >> mon >> y) {
int sum = 0;
int m = num(mon);
int ky = 2001;
while (ky < y) {
if (isleap(ky)) sum += 366;
else sum += 365;
ky++;
}
for (int i = 1; i < m; i++)
sum += month[i];
if (isleap(y) && m > 2) sum += 1;
sum += d;
int week = sum % 7;
numweek(week);
}
return 0;
}
[KY250 日期类]
#include <bits/stdc++.h>
using namespace std;
int month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool isleap(int year) {
if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
return true;
return false;
}
int main() {
int n; cin >> n;
int y, m, d;
for (int i = 0; i < n; i++) {
cin >> y >> m >> d;
month[2] = 28;
if (isleap(y)) month[2] = 29;
d++;
if (d > month[m]) {
d = 1;
m += 1;
}
if (m > 12) {
y += 1;
m = 1;
}
printf("%04d-%02d-%02d\n", y, m, d);
}
return 0;
}
|