日期集合
遍历两个日期之间的日期
#include <bits/stdc++.h>
using namespace std;
int main()
{
for (int year = 2000; year <= 2020; year++)
{
for (int month = 1; month <= 12; month++)
{
for (int day = 1; day <= 31; day++)
{
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
{
;
}
else if (month == 2)
{
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
{
if (day > 29) break;
}
else
{
if (day > 28) break;
}
}
else
{
if (day > 30)
{
break;
}
}
printf("%d-%02d-%02d\n", year, month, day);
}
}
}
return 0;
}
日期统计
#include<bits/stdc++.h>
using namespace std;
int months[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
int a[100] = {
5, 6, 8, 6, 9, 1, 6, 1, 2, 4, 9, 1, 9, 8, 2, 3, 6, 4, 7, 7,
5, 9, 5, 0, 3, 8, 7, 5, 8, 1, 5, 8, 6, 1, 8, 3, 0, 3, 7, 9,
2, 7, 0, 5, 8, 8, 5, 7, 0, 9, 9, 1, 9, 4, 4, 6, 8, 6, 3, 3,
8, 5, 1, 6, 3, 4, 6, 7, 0, 7, 8, 2, 7, 6, 8, 9, 5, 6, 5, 6,
1, 4, 0, 1, 0, 0, 9, 4, 8, 0, 9, 1, 2, 8, 5, 0, 2, 5, 3, 3
};
int main()
{
int res = 0;
for(int m = 1; m<=12; m++){
for(int d = 1; d <= months[m]; d++){
int s[8]={2, 0, 2, 3, m/10, m%10, d/10, d%10};
int j=0;
for(int i=0;i<100;i++){
if(a[i]==s[j]){
j++;
if(j==8){
res++;
break;
}
}
}
}
}
cout << res;
return 0;
}
日期差值
#include<bits/stdc++.h>
using namespace std;
int a[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
int cal(int date)
{
int y = date/10000;
int m = date%10000/100;
int d = date%10000%100;
a[2] = (y%4==0&&y%100!=0||y%400==0?29:28);
while (m -- ) d += a[m];
while(y--) d+=(y%4==0&&y%100!=0||y%400==0?366:365);
return d;
}
int main()
{
int a, b;
while(cin>>a>>b)cout<<abs(cal(a)-cal(b))+1<<endl;
return 0;
}
顺子日期
#include<bits/stdc++.h>
using namespace std;
int months[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
bool check_valid(int date)
{
int y = date/10000;
int m = date%10000/100;
int d = date%10000%100;
if(m<=0 || m>12) return false;
if(d<=0 || m!=2 && d>months[m]) return false;
if(m == 2)
{
if(y%4==0&&y%100!=0||y%400==0)
months[2] = 29;
if(d>months[2]) return false;
}
return true;
}
bool check(string s)
{
for (int i = 0; i < s.size(); i ++ )
{
if(s[i] - '0' + 1 == s[i + 1] - '0' && s[i + 1] - '0' + 1 == s[i + 2] - '0')
return true;
}
return false;
}
int main()
{
int res = 0;
for (int i = 20220101; i <= 20221231; i ++ )
{
if(check_valid(i))
{
string s = to_string(i);
if(check(s))
res++;
}
}
cout << res;
return 0;
}
某个日期是第几天
#include<bits/stdc++.h>
using namespace std;
int months[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
int main()
{
int y = 2000, m = 5, d = 4;
int res = 0;
if(y%4==0 && y%100!=0 || y%400==0)
months[2] = 29;
else
months[2] = 28;
for (int i = 1; i < m; i ++ )
res += months[i];
res += d;
cout << res;
return 0;
}
日期累加
#include<bits/stdc++.h>
using namespace std;
int months[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
int main()
{
int y = 2014, m = 11, d = 9, total = 1000;
while(total--)
{
d ++;
if(y%4==0 && y%100!=0 || y%400==0)
months[2] = 29;
else
months[2] = 28;
if(d > months[m])
{
d = 1;
m++;
}
if(m > 12)
{
m = 1;
y++;
}
}
printf("%d %d %d",y, m, d);
return 0;
}
#include <iostream>
using namespace std;
const int months[13] = {
0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
int is_leap_year(int year) {
if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
return 1;
return 0;
}
int days_of_month(int year, int month) {
if(month == 2) return months[month] + is_leap_year(year);
return months[month];
}
int main() {
int T;
cin >> T;
for(int i = 0; i < T; i ++) {
int y, m, d, a;
cin >> y >> m >> d >> a;
d += a;
while(d > days_of_month(y, m)) {
d -= days_of_month(y, m);
m ++;
if(m == 13) m = 1, y++;
}
printf("%4d-%02d-%02d\n", y, m, d);
}
return 0;
}