当前位置: 首页 > news >正文

004.快读快写

为什么需要快读快写?

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

常用 IO

  • 默认cin/cout

  • 优化cin/cout

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

  • scanf/printf

  • getchar/putchar

  • fread


谨记用'\n'代替endl,懂的都懂orz

旧版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,bool 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');
}

我们可以进一步偷懒添加功能

template<typename type>inline void wr1(type x){wr(x),putchar(' ');}
template<typename type>inline void wr2(type x){wr(x),putchar('\n');}

功能很简单,一目了然

四、增添字符串读写

inline void readline(char *s){char ch=nc();int i(0);while(ch=='\n'||ch=='\r')ch=nc();while(ch!='\n'&&ch!='\r'&&ch!=EOF)s[i++]=ch,ch=nc();s[i]='\0';
}
inline void write(const char*s){while(*s)putchar(*s++);
}
inline void writeline(const char*s){write(s);putchar('\n');
}

五、封装

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();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 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');}template<typename type>inline void wr1(type x){wr(x),putchar(' ');}template<typename type>inline void wr2(type x){wr(x),putchar('\n');}inline void readline(char *s){char ch=nc();int i(0);while(ch=='\n'||ch=='\r')ch=nc();while(ch!='\n'&&ch!='\r'&&ch!=EOF)s[i++]=ch,ch=nc();s[i]='\0';}inline void write(const char*s){while(*s)putchar(*s++);}inline void writeline(const char*s){write(s),putchar('\n');}}using namespace IO;

产物(已压行)

简易版

便于本地调试

namespace IO{template<typename type>inline void read(type &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*10+ch-'0';h=getchar();}f?x=-x:0;}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');}template<typename type>inline void wr1(type x){wr(x),putchar(' ');}template<typename type>inline void wr2(type x){wr(x),putchar('\n');}inline void readline(char *s){char ch=nc();int i(0);while(ch=='\n'||ch=='\r')ch=nc();while(ch!='\n'&&ch!='\r'&&ch!=EOF)s[i++]=ch,ch=nc();s[i]='\0';}inline void write(const char*s){while(*s)putchar(*s++);}inline void writeline(const char*s){write(s),putchar('\n');}
}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();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 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');}template<typename type>inline void wr1(type x){wr(x),putchar(' ');}template<typename type>inline void wr2(type x){wr(x),putchar('\n');}inline void readline(char *s){char ch=nc();int i(0);while(ch=='\n'||ch=='\r')ch=nc();while(ch!='\n'&&ch!='\r'&&ch!=EOF)s[i++]=ch,ch=nc();s[i]='\0';}inline void write(const char*s){while(*s)putchar(*s++);}inline void writeline(const char*s){write(s),putchar('\n');}
}using namespace Fast::IO;

更极端

进一步添加输出缓存

可以但没必要

namespace IO{char inbuf[1<<20],outbuf[1<<20],*pin=inbuf,*pout=outbuf;
#define gc() (pin==inbuf+(1<<20)?fread(inbuf,1,1<<20,stdin),pin=inbuf,*pin++:*pin++)
#define pc(ch) (pout==outbuf+(1<<20)?fwrite(outbuf,1,1<<20,stdout),pout=outbuf,*pout++=ch:*pout++=ch)
struct AutoFlush{~AutoFlush(){fwrite(outbuf,1,pout-outbuf,stdout);}}auto_flush;
template<typename T>void read(T&x){x=0;bool f=0;char ch=gc();while(ch<'0'||ch>'9')f=ch=='-',ch=gc();while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=gc();if(f)x=-x;}
template<typename T>void write(T x){if(x<0)x=-x,pc('-');static char stk[50];int top=0;do stk[++top]=x%10+'0',x/=10;while(x);while(top)pc(stk[top--]);}
template<typename T>void write1(T x){write(x);pc(' ');}
template<typename T>void write2(T x){write(x);pc('\n');}
void write(const char*s){while(*s)pc(*s++);}
void writeln(const char*s){write(s);pc('\n');}
void readline(char*s){char ch=gc();while(ch=='\n'||ch=='\r')ch=gc();while(ch!='\n'&&ch!='\r'&&ch!=EOF)*s++=ch,ch=gc();*s='\0';}
}using namespace IO;

手动O2优化

#pragma GCC optimize(2)

贴个Solution.cpp

#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize(2)
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();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 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');}template<typename type>inline void wr1(type x){wr(x),putchar(' ');}template<typename type>inline void wr2(type x){wr(x),putchar('\n');}inline void readline(char *s){char ch=nc();int i(0);while(ch=='\n'||ch=='\r')ch=nc();while(ch!='\n'&&ch!='\r'&&ch!=EOF)s[i++]=ch,ch=nc();s[i]='\0';}inline void write(const char*s){while(*s)putchar(*s++);}inline void writeline(const char*s){write(s),putchar('\n');}}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 ll INF=0x3f3f3f3f3f3f3f3fll;
const int MAXN=100005;void solve(){
}
int main(){int T;read(T);while(T--){solve();}
}

结语

只是辅助工具,锦上添花

感谢观看

http://www.jsqmd.com/news/68620/

相关文章:

  • 2025橡胶制品定制服务商TOP5权威推荐:金恒橡塑,解决工
  • 国内小程序开发公司精选前十名,2025年12月小程序开发制作公司榜单出炉,标杆级品质
  • 行业指南|2025年12月按摩推拿培训学校综合评估:基于师资、课程与就业网络的实力甄选
  • 2025年太阳能热水器靠谱供应商推荐,太阳能热水器厂商全解析
  • Web安全——PHP语法与安全
  • 2025中餐世锦赛的品质之选王麻子厨刀的破圈之路
  • 2025开关柜局部放电监测设备生产厂TOP5权威推荐:技术实
  • 责任驱动,美好同行-小赢科技2024-2025年度社会责任报告发布
  • 2025钻石斗齿技术领先厂家TOP5权威推荐:甄选设备先进加
  • 2025年11月聚焦全国按摩推拿培训学校的教学实力与口碑排名
  • 2025年度中国五大定制橡塑制品企业推荐:金恒橡塑在行业内地
  • 2025年中国财税服务行业招聘口碑排行榜:九洲财务招聘满意度
  • GitLab IP地址更换
  • 2025年度泡菜坛工厂TOP5权威推荐:甄选靠谱加工厂助力企
  • 成都离婚律师TOP推荐:成都黎达丞律师团队如何运用“三师会审”制,守护企业主核心资产
  • 实操导向与全国性就业网络:2025年11月全国按摩推拿培训学校竞争力排名深度解码
  • 【签约喜讯】携手3亿规模实力派!利驰软件与浙江特意电气签约,赋能高低压柜数字化升级!
  • 2025压滤机行业售后与口碑TOP5权威测评:京源压滤机在行
  • Dify+ADB Supabase+LLM 实现 AI 客服系统
  • RL基础概念,多臂bandit
  • 按摩推拿培训学校2025年12月实力榜:教学体系、师资水平与行业认可度深度解析
  • uv安装配置
  • 2025年比较好的防爆柴油机履带式搬运/矿用防爆柴油机车厂家推荐及采购指南
  • 2025年知名的新中式香氛五金行业内口碑厂家排行榜
  • 2025年十大耐磨挖掘机斗齿厂商排行榜,原装挖掘机斗齿有实
  • 2025年12月按摩推拿培训学校优选指南:聚焦教学实力与全国就业保障的分析报告
  • 2025年优质抗磨斗齿品牌五大推荐,不错的耐磨斗齿工厂与正规
  • 2025年斗齿认证厂家TOP5权威推荐:破解重型机械配件痛点
  • 2025年河北十大橡塑定制企业推荐,金恒橡塑生产实力与行业前
  • 2025年五大斗齿品牌厂家排行榜,新测评精选斗齿优质生产厂家