【算法模板】C++的输入输出优化

瞎扯淡的 前言

恶膜某民秒没命,虽然说NOIP赛场上分秒必争,但是如果用少量的时间换取更快的代码运行速度,即使多用了一点比赛时间 反正我也AC不了 ,但说不定能少TLE几个点qwq~~
在这里插入图片描述
公主殿下镇楼qwq

①好用 简单 的读入优化

我们叫它朴素读入优化qwq
代码如下:

//#include<iostream>
//#include<cstdio>
#include<cctype>

using namespace std;

int read()
{
	int res=0;char ch=getchar();bool XX=false;
	for(;!isdigit(ch);ch=getchar())
	(ch=='-')&&(XX=true);
	for(;isdigit(ch);ch=getchar())
	res=(res<<3)+(res<<1)+(ch^48);
	return XX?-res:res;
}
/* 输入方式为 变量=read();
int main()
{
	int qwq;
	qwq=read();
	cout<<qwq<<endl;
	return 0;
} 
*/

②比楼上好用 麻烦 一点的读入优化

很快 但是我从来不用fread读入优化qwq
代码如下:

//#include<iostream>
//#include<cstdio>
#include<cctype>

using namespace std;

inline char getcha()
{
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}

int read()
{
    int res=0;char ch=getcha();bool XX=false;
    for(;!isdigit(ch);ch=getcha())
	(ch=='-') && (XX=true);
    for(;isdigit(ch);ch=getcha())
    res=(res<<3)+(res<<1)+(ch^48);
    return XX?-res:res;
}
/* 输入方式为 变量=read();
int main()
{
	int qwq;
	qwq=read();
	cout<<qwq<<endl;
	return 0;
} 
*/

③可能会用到 没什么用 的读入优化

其实就是单纯的实数读入优化而已qwq
代码如下:

//#include<iostream>
//#include<cstdio>
#include<cctype>

inline double dbread()
{
    double X=0,Y=1.0;int w=0;char ch=0;
    while(!isdigit(ch)) {w|=ch=='-';ch=getchar();}
    while(isdigit(ch)) X=X*10+(ch^48),ch=getchar();
    ch=getchar();//这里读入小数点 
    while(isdigit(ch)) X+=(Y/=10)*(ch^48),ch=getchar();
    return w?-X:X;
}
/* 输入方式为 变量=dbread();
int main()
{
	double qwq;
	qwq=dbread();
	printf("%lf",qwq);
	return 0;
}
*/

④好用 简单 的输出优化

要是输出的东西特别多的话也可以写一个朴素输出优化qwq
代码如下:

//#include<iostream>
//#include<cstdio>

using namespace std;

void write(int x)
{
	if(x<0) putchar('-'),x=-x;
	if(x>9) write(x/10);
	putchar(x%10+48);
}
/* 输出方法为 write(变量);
int main()
{
	int a=233;
	write(a);
	return 0;
}
*/

⑤比上一个好用 麻烦 的输出优化

这个fwrite输出优化我是真的一次没用过qwq(好叭测试的时候用了一次
代码如下:

//#include<iostream>
//#include<cstdio>

using namespace std;

char pbuf[100000],*pp=pbuf;

void push(const char c)
{
    if(pp-pbuf==100000)
	{
		fwrite(pbuf,1,100000,stdout);
		pp=pbuf;
	}
	*pp++=c;
}

void write(int x)
{
    static int sta[35];
    int top=0;
    do
	{
		sta[top++]=x%10;
		x/=10;
	}while(x);
    while(top) push(sta[--top]+'0');
}
/* 输出方法为 write(变量);
int main()
{
	int a=233;
	write(a);
	fwrite(pbuf,1,pp-pbuf,stdout); pp=pbuf;
	return 0;
}
*/

请大家注意在程序结束前写如下一句,防止出现没输出完成的类似错误qwq

	fwrite(pbuf,1,pp-pbuf,stdout); pp=pbuf;

完全可以不用读的 后记&&推广

后记:NOIP赛场上考的还是大家对暴力算法的掌握度、吃东西码题的熟练度以及思维的活跃度,所以光依赖于暴力硬艹的方式去拿分(说的就是我qwq)一定不可取。快速输入输出的方法米娜桑们可以选择性掌握其中一个,毕竟对于一些输入输出量很大的题,还是能提高一下代码运行速度的。

推广
我的B站(大和不是大河辣):http://space.bilibili.com/12351522
我的知乎(KasuganoYamato):https://www.zhihu.com/people/kasuganoyamato/activities
这里是大和/Upd@te,一个喜欢OI/日本将棋/钢琴/手绘/炉石传说的蒟蒻
交♂友+Q 765676105(上学长弧致歉)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值