纯模拟80分?

P2010 [NOIP2016 普及组] 回文日期

LEXUGE @ 2017-09-15 20:19:16

#include <stdio.h>

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

int find_date(int date_1,int date_2);
int j_leap(int date);
int j_reverse(int date);

int j_leap(int date)
{
  int date_y=0;
  date_y=date/10000;
  if (((date_y%4==0)&&(date_y%100!=0))||(date_y%400==0)) return 1; // date_y is a leap year
  return 0;// not a leap year
}

int j_reverse(int date)
{
  char a[9]={0};
  int i=0;
  sprintf(a, " %d" , date);
  for (i=1;i<=4;i++)
  {
    if (a[i]!=a[8-i+1]) return 0;//not reverse
  }
  return 1;//reverse
}

int find_date(int date_1,int date_2)
{
  int date_m_1=0,date_d_1=0,date_m_2=0,date_d_2=0,date_y_1=0,date_y_2=0,flag=0,date_temp,s=0;

  date_y_1=date_1/10000;
  date_m_1=(date_1%10000)/100;
  date_d_1=date_1%100;
  date_y_2=date_2/10000;
  date_m_2=(date_2%10000)/100;
  date_d_2=date_2%100;

  while (flag==0)
  {
    date_temp=date_y_1*10000+date_m_1*100+date_d_1;
    if ((date_m_1==2)&&(j_leap(date_temp)==1)) delta[2]++;
    if (date_d_1>delta[date_m_1])
    {
      date_d_1=1;
      if (date_m_1+1>12)
      {
        date_m_1=0;
        date_y_1++;
      }
      date_m_1++;
    }
    delta[2]=28;

    if (j_reverse(date_temp)==1) s++;

    if (date_temp<(date_y_2*10000+date_m_2*100+date_d_2))  flag=0;
    else flag=1;

    date_d_1++;
  }
  return s;
}

int main()
{
  int date_1=0,date_2=0;
  scanf("%d %d",&date_1,&date_2);
  printf("%d\n",find_date(date_1,date_2));
  return 0;
}

不知道为什么

第二组数据我输出330,答案是331


10000101

99991231

```cpp

by d3NtMDAw @ 2017-09-15 20:45:47

13211231

可能不过


by davywxy2003 @ 2017-09-15 21:06:44

嗯!


by LEXUGE @ 2017-09-15 21:15:17

谢谢各位的指教,先贴一个刚刚看了题解的思路:

#include <stdio.h>

int delta[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
struct date {
  int year;
  int month;
  int day;
};

int find_date(int date_1,int date_2);
int convert_date2struct(int date_1,int date_2,struct date *a,struct date *b);
int generate_reverse_by_year(int year);
int is_legal(int date);

int is_legal(struct date date)
{
  if ((date.month>12)||(date.month<1)) return 1;
  if ((date.day>delta[date.month])||(date.day<0)) return 1;
  return 0;
}

int generate_reverse_by_year(int year)
{
  char a[9]={0};
  int i=0;
  sprintf(a, " %d" , year);
  for (i=1;i<=4;i++)
    a[i]=a[i]-'0';
  return year*10000+a[4]*1000+a[3]*100+a[2]*10+a[1];
}

int convert_date2struct(int date,struct date *a)
{
  (*a).year=date/10000;
  (*a).month=date%10000/100;
  (*a).day=date%100;
  return 0;
}

int find_date(int date_1,int date_2)
{
  int i=0,temp=0,sum=0;
  struct date date_start,date_end,date_temp;
  convert_date2struct(date_1,&date_start);
  convert_date2struct(date_2,&date_end);
  for (i=date_start.year;i<=date_end.year;i++)
  {
    temp=generate_reverse_by_year(i);
    convert_date2struct(temp,&date_temp);
    if (is_legal(date_temp)==0)
      if ((temp>=date_1)&&(temp<=date_2))
        sum++;
  }
  return sum;
}

int main()
{
  int date_1=0,date_2=0;
  scanf("%d %d",&date_1,&date_2);
  printf("%d\n",find_date(date_1,date_2));
  return 0;
}

by LEXUGE @ 2017-09-15 21:17:15

@wusimin000 13211231可以过啊


by LEXUGE @ 2017-09-15 21:43:10

此帖终结

修改之后过了,原因是date_temp与日期变换的实现顺序不对,所以不行

#include <stdio.h>

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

int find_date(int date_1,int date_2);
int j_leap(int date);
int j_reverse(int date);

int j_leap(int date)
{
  int date_y=0;
  date_y=date/10000;
  if (((date_y%4==0)&&(date_y%100!=0))||(date_y%400==0)) return 1; // date_y is a leap year
  return 0;// not a leap year
}

int j_reverse(int date)
{
  char a[9]={0};
  int i=0;
  sprintf(a, " %d" , date);
  for (i=1;i<=4;i++)
  {
    if (a[i]!=a[8-i+1]) return 0;//not reverse
  }
  return 1;//reverse
}

int find_date(int date_1,int date_2)
{
  int date_m_1=0,date_d_1=0,date_m_2=0,date_d_2=0,date_y_1=0,date_y_2=0,flag=0,date_temp,s=0;

  date_y_1=date_1/10000;
  date_m_1=(date_1%10000)/100;
  date_d_1=date_1%100;
  date_y_2=date_2/10000;
  date_m_2=(date_2%10000)/100;
  date_d_2=date_2%100;
  while (flag==0)
  {
    date_temp=date_y_1*10000+date_m_1*100+date_d_1;

    if (j_reverse(date_temp)==1) s++;

    if (date_temp<(date_y_2*10000+date_m_2*100+date_d_2))  flag=0;
    else flag=1;

    date_d_1++;
    if ((date_m_1==2)&&(j_leap(date_temp)==1)) delta[2]++;
    if (date_d_1>delta[date_m_1])
    {
      date_d_1=1;
      if (date_m_1+1>12)
      {
        date_m_1=0;
        date_y_1++;
      }
      date_m_1++;
    }
    delta[2]=28;
  }
  return s;
}

int main()
{
  int date_1=0,date_2=0;
  scanf("%d %d",&date_1,&date_2);
  printf("%d\n",find_date(date_1,date_2));
  return 0;
}

by TCFXY(98k) @ 2017-09-24 00:08:52

#include<stdio.h>
#include<iostream>
using namespace std;
int huiwen(int n)
{
    if(n%10==n/10000000&&n%100/10==(n/1000000)%10&&n%1000/100==(n/100000)%10&&n%10000/1000==n/10000%10)
    return true;
    return false;
}
int judge(int n)
{int m=n/10000;
int k=n%10000/100;
int j=n%100;
if(huiwen(n)){
    if((m%4==0&&m%100!=0)||(m%100==0&&m%400==0))
    {
            if((k==2&&j<=29)||(k==1&&j<=31)||(k==3&&j<=31)||(k==5&&j<=31)||(k==7&&j<=31)||(k==8&&j<=31)||(k==10&&j<=31)||(k==12&&j<=31)||(k==4&&j<=30)||(k==6&&j<=30)||(k==91&&j<=30)||(k==11&&j<=30)){
            return true;
            }
        }
        else {
            if((k==2&&j<=28)||(k==1&&j<=31)||(k==3&&j<=31)||(k==5&&j<=31)||(k==7&&j<=31)||(k==8&&j<=31)||(k==10&&j<=31)||(k==12&&j<=31)||(k==4&&j<=30)||(k==6&&j<=30)||(k==91&&j<=30)||(k==11&&j<=30)){
            return true;
            }
    }
}
return false;
}
int main(){
    int n,m,ans=0;
    scanf("%d%d",&n,&m);
/*    if(n==m){
    if(huiwen(n)) 
    ans=1;
}*/
    for(int i=n;i<=m;i++)
    {if(judge(i))
    ans++;
    }
    printf("%d",ans);
}

为何不通过?? 80分 2,10两点超时

在vijos上通过。。。


|