zoukankan      html  css  js  c++  java
  • 读入与输出

    读入与输出

    scanf

    scanf读入char数组不需要写&

    scanf读入longlong类型时在unix(linux ububt mac osx)下用lld,在WIN32下用I64d,WIN64下以上两种都可以。为了考试时避免忘记更改可以在文件开头加上

      #ifdef WIN32
      #define LL "%I64d"
      #else
      #define LL "%lld"
      #endif
    

    调用时

      scanf(LL,&a);
    

    cin

    cin正常状态下比scanf读入慢,但是可以在文件开头加入

    ios::sync_with_stdio(false)
    

    可以使速度大大提升,但是写上这句话之后不再能够使用scanf

    读入优化

    getint

    void read(int &x)
    {
        x=0;int f=1;
        char c=getchar();
        while(!isdigit(c))
        {
        if(c=='-') f=-1;
        c=getchar();
        }
        while(isdigit(c))
        {
        x=x*10+c-'0';
        c=getchar();
        }
        x*=f;
    }
    

    ifread

    char ch;
    char buf[100000],*p1 = buf,*p2 = buf;
    #define nc() 
        p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2) ? EOF :*p1++;
    #define read(x)  
        x=0;ch=nc(); 
        while(!isdigit(ch)) ch=nc();
        while(isdigit(ch))x=x*10+ch-'0',ch=nc();
    

    输出优化

      char s[80000000]
      void print(int v)
      {
          int x=0,y=0
          while(v)
          {
            x++;
            y=y*10+v%10;
            v/=10;
          }     
          for(int i=1;i<=x;++i)
          {
            s[++l]=y%10+'0';
            y/=10;
          }
          l++;
          s[l]='
    ';
      }
    

    速度比较

    读入1e7个int

    标准cin: 12.23s

    scanf: 9.718s

    getint: 2.996s

    ios::sync_with_stdio(false)+cin:2.18s

    ifread:0.6948s

    输出1e7个数

    printf: 30s

    cout: 20s

    输出优化:1.47s

  • 相关阅读:
    trueStudio笔记
    C笔记
    printf打印输出
    DB9针和DB25针串口的引脚定义
    通信单位
    简单工厂
    不同进程之间发送消息将指定界面置顶
    Delegate event 委托事件---两个From窗体使用委托事件
    Winfrom窗体无法关闭问题--检查是否存在重写
    自定义控件添加事件
  • 原文地址:https://www.cnblogs.com/wxyww/p/9487034.html
Copyright © 2011-2022 走看看