004.快读快写

为什么需要快读快写?

  1. 极少题目会卡常数时间
  2. 偷懒

常用 IO

  • 默认cin/cout

  • 优化cin/cout

    cin.tie(0)->sync_with_stdio(0)

  • scanf/printf

  • getchar/putchar

  • fread


谨记用'\n'代替endl

旧版read(),write()

#include<bits/stdc++.h>
using namespace std;

//read()
inline int read(){
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')f*=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}

//write()
inline void write(int x)
{
    if(x<0)
        putchar('-'),x=-x;
    if(x>9)
        write(x/10);
    putchar(x%10+'0');
    return;
}

int main(){
    int T=read();
    while(T--){
        int n=read(),m=read();
        write(n+m);
        putchar('\n');
    }
}

优化

一、不止 int

#include<bits/stdc++.h>
using namespace std;

//read(x)
template<typename type>
inline void read(type &x){
    x=0;
    int f=0;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')f=1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=x*10+ch-'0';
        ch=getchar();
    }
    f?x=-x:0;
}

//write(x)
template<typename type>
inline void write(type x)
{
    if(x<0)
        putchar('-'),x=-x;
    if(x>9)
        write(x/10);
    putchar(x%10+'0');
    return;
}

int main(){
    long long T,m,n;
    read(T);
    while(T--){
        read(m),read(n);
        write(m+n);
    }
}

除了字符串均可读写

二、进一步优化read(x)

添加输入缓冲

char buf[1<<20],*p1=buf,*p2=buf;
#define nc() (p1==p2 && (p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)?EOF:*p1++)
template<typename type>
inline void read(type &x){
    x=0;bool f(0);
    char ch=nc();
    while(ch<'0'||ch>'9')f=ch=='-',ch=nc();
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=nc();
    f?x=-x:0;
}
  • 优点:更快!
  • 缺点:对本地调试不友好,但对文件输入输出、OJ判题没有影响

ps: 如果你需要本地调试,可以使用宏定义或者配置你的编译器,最安全的方法是使用文件输入输出

三、进一步优化write(x)

使用stack而不是递归

template<typename type>
inline void write(type x){
    x<0?x=-x,putchar('-'):0;static short stk[50],top(0);
    do stk[++top]=x%10,x/=10;while(x);
    while(top)putchar(stk[top--]+'0');
}

四、增添字符串读写

    void read(string&s){
        s.clear();
        char ch=getchar();
        while(isspace(ch))ch=getchar();
        while(!isspace(ch)&&ch!=EOF){
            s+=ch;
            ch=getchar();
        }
    }
    void wr(const string &s){
        for(char c:s)putchar(c);
    }

模板

简易版

namespace IO{
    template<typename T>void read(T&x){x=0;bool f=0;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=1;ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}if(f)x=-x;}
    void read(string&s){s.clear();char ch=getchar();while(isspace(ch))ch=getchar();while(!isspace(ch)&&ch!=EOF){s+=ch;ch=getchar();}}
    template<typename T>void wr(T x){if(x==0){putchar('0');return;}if(x<0)putchar('-'),x=-x;char stk[20];int top=0;while(x)stk[++top]=x%10+'0',x/=10;while(top)putchar(stk[top--]);}
    void wr(const string &s){for(char c:s)putchar(c);}
    template<typename T>void wr(const T&x,char sep){wr(x);putchar(sep);}
}using namespace IO;

添加输入缓冲

建议文件读写

namespace IO{char buf[1<<20],*p1=buf,*p2=buf;
        #define nc() (p1==p2 && (p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)?EOF:*p1++)
        template<typename type>inline void read(type&x){x=0;bool f(0);char ch=nc();if(ch==EOF)return;while(ch<'0'||ch>'9')f=ch=='-',ch=nc();while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=nc();f?x=-x:0;}
        template<typename type>inline void wr(type x){x<0?x=-x,putchar('-'):0;static short stk[50],top(0);do stk[++top]=x%10,x/=10;while(x);while(top)putchar(stk[top--]+'0');}
        void wr(const string &s){for(char c:s)putchar(c);}
        template<typename T>void wr(const T&x,char sep){wr(x);putchar(sep);}
}using namespace IO;

solution

#include<bits/stdc++.h>
using namespace std;
namespace IO{template<typename T>void read(T&x){x=0;bool f=0;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=1;ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}if(f)x=-x;}void read(string&s){s.clear();char ch=getchar();while(isspace(ch))ch=getchar();while(!isspace(ch)&&ch!=EOF){s+=ch;ch=getchar();}}template<typename T>void wr(T x){if(x==0){putchar('0');return;}if(x<0)putchar('-'),x=-x;char stk[20];int top=0;while(x)stk[++top]=x%10+'0',x/=10;while(top)putchar(stk[top--]);}void wr(const string &s){for(char c:s)putchar(c);}template<typename T>void wr(const T&x,char sep){wr(x);putchar(sep);}}using namespace IO;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int MOD=1000000007;
const int INF=0x3f3f3f3f;
const int MAXN=100005;

void solve(){
}
int main(){
    int T=1;
    read(T);
    while(T--){
        solve();
    }
}

结语

只是辅助工具,锦上添花

感谢观看

posted @ 2025-12-09 13:32  射杀百头  阅读(19)  评论(0)    收藏  举报