One question per day (the seventh programming question in the second provincial competition of group B of C / C + + in 2020)

Posted by adamriley on Mon, 24 Jan 2022 12:07:42 +0100

Here is the title of the blue bridge cup over the years. Welcome to pay attention to me

[problem description]

During the Spring Festival in 2020, a special date has attracted everyone's attention: February 2, 2020. Because if this date is written in the format of "yyyymmdd", it is 20200202, which is exactly a palindrome number. We call such a date palindrome date.

Some people say that 20200202 is a special day of "once in a millennium". Xiao Ming disagrees with this, because the next palindrome date is less than two years later: 20211202, that is, December 2, 2021.

Others said that 20200202 is not only a palindrome date, but also an abbaba palindrome date. Xiao Ming also disagrees with this, because about 100 years later, he will encounter the next Ababa palindrome date: 21211212, that is, December 12, 2121. It is not "once in a thousand years", but "twice in a thousand years" at most.

Given an 8-digit date, please calculate the next palindrome date and the next Ababa palindrome date after the date.

[input format]

The input contains an eight digit integer N representing the date.

[output format]

Output two lines with one eight digit in each line. The first line represents the next palindrome date, and the second line represents the next Ababa palindrome date.

[sample input]

  20200202

[sample output]

  ​​​ 20211202

 21211212

[evaluation case scale and agreement]

For all evaluation cases, 10000101 ≤ N ≤ 89991231, ensure that N is an 8-digit representation of a legal date.

Topic analysis:

1. Through violence, traverse down in turn and output the number that meets the conditions

2. Use three user-defined functions to judge (date judgment, palindrome sequence judgment, AB palindrome sequence judgment, etc.)

3. The first output meets: Date judgment and palindrome sequence judgment are valid at the same time

The second output is satisfied: Date judgment and AB palindrome sequence judgment are established at the same time

Title Code:

#include<iostream>
using namespace std;

bool judge(int n);//Judge whether it is a normal date 

bool huiwen(int n);//Determine whether it is palindrome sequence 

bool abhuiwen(int n);//Determine whether it is type AB palindrome sequence 

int main ()
{
  int n,ans1,ans2;
  cin>>n;
  for(int i=n+1;i<=89991231;i++)
  {
    if(judge(i)&&huiwen(i))
    {//Meet the date requirements and palindrome sequence at the same time, and output 
    cout<<i<<endl;
    break;
    }
  }
  for(int i=n+1;i<=89991231;i++)
  {
    if(judge(i)&&abhuiwen(i))
    {//Meet the date requirements and type AB palindrome sequence at the same time, and output
    cout<<i<<endl;
    break;
    }
  }
  return 0;
}

bool judge(int n)//Judge whether it is a normal date 
{
  int nian,yue,ri;
  int yue_[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
  nian=n/10000;//Get year 
  yue=n/100%100;//Get month 
  ri=n%100;//Get date 
  if(yue>12||yue==0)
    return false;
  //If the month screening is not in the range of 1-12, it does not meet the conditions   
  
  if(nian%4==0&&nian%100!=0||nian%400==0)  
    yue_[2]=29;
  else
    yue_[2]=28;
  //Judge the leap year and update the value of February
      
  if(ri>yue_[yue]||ri==0)
    return false;
  //Filter the date. If it is not in the range of 1 - month end, it does not meet the conditions
    
  return true;    
  //The remaining conditions meet the requirements 
}

bool huiwen(int n)//Determine whether it is palindrome sequence 
{
  int a,b,c,d,d1,c1,b1,a1;//Corresponding to 8 digits in sequence 
  for(int i=n;i<=89991231;i++)
  {
    a=n/10000000;
    //Get first place 
    b=n/1000000%10;
    //Get 2nd place 
    c=n/100000%10;
    //Get 3rd place 
    d=n/10000%10;
    //Get 4th place 
    d1=n/1000%10;
    //Get 5th place 
    c1=n/100%10;
    //Get 6th place 
    b1=n/10%10;
    //Get 7th place 
    a1=n%10;
    //Get the 8th place 
    if(a!=a1||b!=b1||c!=c1||d!=d1)
      return false;
    //Judge 1 and 8, 2 and 7, 3 and 6, 4 and 5 in turn 
    //But if there is one difference, it does not meet the requirements 
    else
      return true;  
  }  
}

bool abhuiwen(int n)//Determine whether it is an AB palindrome sequence 
{
  int a,b,c,d,d1,c1,b1,a1;
  for(int i=n;i<=89991231;i++)
  {
    a=n/10000000;
    b=n/1000000%10;
    c=n/100000%10;
    d=n/10000%10;
    d1=n/1000%10;
    c1=n/100%10;
    b1=n/10%10;
    a1=n%10;
    if(a==a1&&a==c&&c==c1&&d==d1&&d1==b&&b==b1)
      return true;
    //Judge whether 1 and 8, 1 and 3, 3 and 6 are equal 
    //Judge whether 2 and 7, 2 and 4, 4 and 5 are equal    
    else
      return false;  
  }  
}

Operation results:

Test results:

Topics: C++ Algorithm