# AC了,但可不可以更优化呢?

P5737 【深基7.例3】闰年展示

xujialin268 @ 2021-07-22 14:01:02

可不可以只用一个循环做到计数和输出闰年嘞?```

#include<cstdio>
using namespace std; 
bool rn(int k);
int main()
{
    int x,y;scanf("%d%d",&x,&y);
    int t=0;
    for(int i=x;i<=y;i++)
    {
        if(rn(i)==true) t++;
    }
    printf("%d\n",t);
    for(int i=x;i<=y;i++)
    {
        if(rn(i)==true) printf("%d ",i);
    }
    return 0;
}
bool rn(int k)
{
  if(k%4==0&&k%100!=0||k%400==0) return true;
  else return false;
}

by ZBAA_MKC @ 2021-07-22 14:02:33

@xujialin268 好像不行()


by MarSer020 @ 2021-07-22 14:03:01

@xujialin268 不可以


by xujialin268 @ 2021-07-22 14:04:07

@kkksc020 ok~


by xujialin268 @ 2021-07-22 14:04:48

@mkc20230729 欧克


by impuk @ 2021-07-22 14:06:10

可以的,有点麻烦


by 信守天下 @ 2021-07-22 14:07:16

可以,我就是这么过的


by _caiji_ @ 2021-07-22 14:13:50

可以的,给个代码实现吧:

#include <cstdio>
using namespace std;
char o[80000],*p=o;
int l,r,ans;
bool check(int x){return x%400==0||(x%100!=0&&x%4==0);}
void write(int x){if(x>9){write(x/10);}*p++=x%10+48;}
int main(){
    scanf("%d%d",&l,&r);
    for(int i=l;i<=r;i++) if(check(i)) ans++,write(i),*p++=32;
    printf("%d\n",ans),fwrite(o,1,p-o,stdout);
    return 0;
}

by _caiji_ @ 2021-07-22 14:14:23

@xujialin268


by xujialin268 @ 2021-07-23 09:11:00

@caijianhong 芜湖~谢谢!


|