• 读入输出优化


     1.读入优化

      完整理解:

     1 void read(int &x)//'&'表示引用,也就是说x是一个实参,在函数中改变了x的值就意味着在外面x的值也会被改变
     2 {
     3     int f=1;//标记正负
     4     x=0;//归零(这就是潜在bug,有可能传进来时x没有归零)
     5     char s=getchar();//读入第一个字符
     6     while(s<'0'||s>'9')//不是数字字符
     7     {
     8         if(s=='-')//不能直接把f=-1,有可能输入的不是'-'而是其他乱七八糟的东西
     9             f=-1;
    10         s=getchar();//继续读
    11     }
    12     while(s>='0'&&s<='9')//是字符(一旦不是字符就意味着输入结束了)
    13     {
    14         x=x*10+s-'0';
    15         s=getchar();
    16     }
    17     x*=f;//改变正负
    18 }

     2.输出优化

      完整理解:

     1 void print(int x)//这里不用实参
     2 {
     3     if(x<0)//负数
     4     {
     5         putchar('-');
     6         x=-x;
     7     }
     8     if(x>9)//只要x还是2位数或更多就继续分解
     9         print(x/10);//这里递归完后栈里面x的每一位是倒过来的
    10     putchar(x%10+'0');//输出(要把int型变为char型,加'0'即可)
    11 }
    • 所以整理一下就长这样

    void read(int &x)
    {
        int f=1;x=0;char s=getchar();
        while(s<'0'||s>'9') {if(s=='-') f=-1;s=getchar();}
        while(s>='0'&&s<='9') {x=x*10+s-'0';s=getchar();}
        x*=f;
    }
    
    void print(int x)
    {
        if(x<0) putchar('-'),x=-x;
        if(x>9) print(x/10);
        putchar(x%10+'0');
    }

      最后还有一个buff,但不能本地调试

    inline char nc(){
        static char buf[100000],*p1=buf,*p2=buf;
        return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
    }
    inline int red(){
        char ch=nc();int sum=0;
        while(!(ch>='0'&&ch<='9'))ch=nc();
        while(ch>='0'&&ch<='9')sum=sum*10+ch-48,ch=nc();
        return sum;
    }

    其它什么的详见这里

    upd:

    我们最后得到了一个健康的快读+buff

     1 inline int gc()
     2 {
     3     static char buf[1008611];
     4     static int len=0,pos=0;
     5     if(pos==len) pos=0,len=fread(buf,1,1008611,stdin);
     6     if(pos==len) return EOF;
     7     return buf[pos++];
     8 }
     9 inline void red(int &x)
    10 {
    11     int c=gc();x=0;int f=1;
    12     while(!isdigit(c)){ if(c=='-') f=-1;c=gc(); }
    13     while(isdigit(c)){ x=x*10+c-'0'; c=gc(); }
    14     x*=f;
    15 }
  • 相关阅读:
    关于DOM与BOM
    常用单位及颜色表
    关于Grid
    form表单
    自我介绍
    python——3种字符串反转方法
    JavaScript 正则表达式
    关于JavaScript的DOM和BOM
    CSS颜色表示的几种方式
    CSS的常用单位介绍
  • 原文地址:https://www.cnblogs.com/kylara/p/9813352.html
Copyright © 2020-2023  润新知