【模板】速度优化·快读快写(较详)

“ Ctrl AC!一起 AC!”

目录

前言

整数的快读快写

浮点数的快读快写

字符串的快读快写


前言

输入输出速度比较(慢->快):

cin/cout -> scanf/printf 约等于 ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)+cin/cout -> getchar/putchar

endl -> '\n'

基于getchar/putchar实现灰常快的“快读快写”

整数的快读快写

#include<bits/stdc++.h>
using namespace std;
inline int read() { //inline内联函数进一步优化速度
	int x = 0, f = 0; //x是返回值,f表示正负
	char ch = 0; //初始化为空格
	while (!isdigit(ch)) { //当输入不是数字时,一直读入但不记录到x中(建议直接使用isdigit判断)
		f |= (ch == '-');
		ch = getchar();
	}
	while (isdigit(ch)) { //当输入的是数字时,一直记录到x中
		x = (x << 3) + (x << 1) + (ch ^ 48); //(x<<3)+(x<<1)->x*10  ch^48->ch-'0'
		ch = getchar();
	}
	return f? -x: x;
}
inline void write(int x) {
	char num[30]; //存放翻转后的数字,方便输出
	int cnt = 0; //num数组的大小
	if (x == 0) { putchar('0'); return; }
	if (x < 0) {
		putchar('-'); 
		x = -x;
	}
	while (x > 0) { //将数字分解成字符
		num[cnt++] = x % 10 + '0'; 
		x /= 10;
	}
	while (cnt > 0) {
		putchar(num[--cnt]);
	}
}
int main() {
	int x;
	cout << "输入:" << endl;
	x = read();
	cout << "输出:" << endl;
	write(x);
	return 0;
}

浮点数的快读快写

主要思想就是,先处理整数部分,再处理小数部分

#include<bits/stdc++.h>
using namespace std;
inline double readdou() { //inline内联函数进一步优化速度
	double x = 0; int flag = 0;//x是返回值,flag表示正负
	char ch = 0; //初始化为空格
	while (!isdigit(ch)) { //当输入不是数字时,一直读入但不记录到x中(建议直接使用isdigit判断)
		flag |= (ch == '-');
		ch = getchar();
	}
	while (isdigit(ch)) { //当输入的是数字时,一直记录到x中
		x = x * 10 + (ch - '0'); //double不能用位运算了
		ch = getchar();
	}
	if (ch != '.') return flag ? -x : x; //如果后面跟的不是小数点,则直接结束
	int f = 1; //f用于设置某数字所对应的值
	ch = getchar();
	while (isdigit(ch)) {
		x = x + (ch - '0') * pow(10, -f); //pow!
		f++; //乘10^-1表示小数点后的第一位的值,10^-2表示小数点后的第二位的值...
		ch = getchar();
	}
	return flag ? -x : x;
}
inline void write(int x) {
	char num[30]; //存放翻转后的数字,方便输出
	int cnt = 0; //num数组的大小
	if (x == 0) { putchar('0'); return; }
	if (x < 0) {
		putchar('-'); 
		x = -x;
	}
	while (x > 0) { //将数字分解成字符
		num[cnt++] = x % 10 + '0'; 
		x /= 10;
	}
	while (cnt > 0) {
		putchar(num[--cnt]);
	}
}
inline void writedou(double x) {
	write(int(x)); //先输出x的整数部分
	//减去x的整数部分
	if (x < 0) x = -x;
	x -= int(x);
	if (x!=0) putchar('.');
	while (x) { //循环输出小数部分
		x *= 10; 
		putchar(int(x) + '0');
		x -= int(x);
	}
}
int main() {
	double x;
	cout << "输入:" << endl;
	x = readdou();
	cout << "输出:" << endl;
	writedou(x);
	return 0;
}

字符串的快读快写

字符串的快读快写 其实 速度比那些 字符串输入函数 快不到哪里去。

不如直接使用函数输入。

字符数组 a[m] :cin.get(a,m) , gets(s) 等

string字符串 s :getline(cin,s) 等

(上述函数都可读空格,以下快读也可读空格)!

#include<bits/stdc++.h>
using namespace std;
inline string readstr() {
	string s = "";
	char ch;
	while ((ch = getchar()) != '\n') {
		s += ch;
	}
	return s;
}
inline void writestr(string s) {
	for (int i = 0; i < s.size(); i++) putchar(s[i]);
}
int main() {
	string s;
	cout << "输入:" << endl;
	s = readstr();
	cout << "输出:" << endl;
	writestr(s);
}

感谢阅读!!!

“ Ctrl AC!一起 AC!”

  • 7
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ctrl AC

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值