2、顺子日期

题目 顺子日期

image-015ddddd

思路分析

从2022.1.1枚举到2023

看每个日期是否符合三连顺

代码实现

#include <bits/stdc++.h>
using namespace std;
int days[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
bool isleap(int y){
  return y%100 && y%4==0 || y%400==0;
}
int getdays(int y,int m){
  return days[m]+(m==2 && isleap(y));
}
void nextday(int &y,int &m,int &d){
  d++;
  if(d>getdays(y,m)){
    d=1;
    m++;
    if(m>12){
      m=1;
      y++;
    }
  }
}

bool check(char* s){
   for(int i=2;i<8;i++){
     if(s[i-1]-s[i-2]==1 && s[i]-s[i-1]==1)
        return true;
   }
   return false;
}

int main()
{
  int cury=2022,curm=1,curd=1;
  int cnt=0;
  while(cury<2023){
    char s[10];
    sprintf(s,"%04d%02d%02d",cury,curm,curd);
    if(check(s))
       cnt++;
    nextday(cury,curm,curd);
  }
  cout<<cnt;
  return 0;
}
#include<bits/stdc++.h>

using namespace std;

#define endl '\n'

//#define int long long 蓝桥杯用不了signed main

using ll = long long;

using ull = unsigned long long;

using PII = pair<int,int>;

using Pll = pair<ll,ll>;

int dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};

const int inf = 0x3f3f3f3f;

int days[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};

bool is_leap(int y){

	return y%100 && y%4==0 || y%400==0;

}

int get_days(int y,int m){

	return days[m]+(m==2 && is_leap(y));

}

void next_day(int &y,int &m,int &d){

	d++;

	if(d>get_days(y,m)){

		d=1;

		m++;

		if(m>12){

			m=1;

			y++;

		}

	}

}

bool check_date(int y,int m,int d){

	if(m<1 || m>12)	return false;

	if(d<1 || d>get_days(y,m))	return false;

	return true;

}

/*

while((curYear < targetYear) ||

	  (curYear == targetYear && curMonth < targetMonth) ||

	  (curYear == targetYear && curMonth == targetMonth && curDay < targetDay)){

	next_day();

}

*/

bool check(string s){

  for(int i=2;i<s.size();i++){

    if(s[i-1]-s[i-2]==1 && s[i]-s[i-1]==1)  return true;

  }

  return false;

}

int main()

{

	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);

	int curYear=2022,curMonth=1,curDay=1;

  int cnt=0;

  while(curYear<2023){

    char buffer[9];

    sprintf(buffer, "%04d%02d%02d", curYear, curMonth, curDay);

    string s(buffer);

    if(check(s)) cnt++;

    next_day(curYear,curMonth,curDay);

  }

	cout<<cnt;

	return 0;

}

同类题型

视频讲解


⬅️ 1、九进制转十进制 🏠 00-刷题理模型 ➡️ 3、刷题统计