1.快读快输
输入,用自己写的getc,比getchar还要快.
输出,用char数组建立一个
2
21
2^{21}
221空间的缓冲区,一个题目输出不会超过这么多,足够用
所有的输出全部打入char数组,最后输出时一次性写入标准读入.大数据输出时速度比printf快几倍
#include <cctype>
#include <cstdio>
#include <ctime>
#include <iostream>
using namespace std;
namespace FastIO {
char buf[1 << 21], buf2[1 << 21], a[20], *p1 = buf, *p2 = buf, hh = '\n';
//一共占了4M 1M=2^20B,2个2M的char数组
//1char占1B(字节)
//a用来临时存每个整数,不会超过20位
int p, p3 = -1; //p临时指针
void read() {}
void print() {} //为空的时候就会调用这玩意而不是下面那个print,于是就退出了循环
inline int getc() { //超快速读入的getchar
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++;
}
inline void flush() {
fwrite(buf2, 1, p3 + 1, stdout), p3 = -1;
// 从buf2写,每个写入的元素占1个字节,写入p3+1个,写到标准输出stdout里面
}
template <typename T, typename... T2>
inline void read(T &x, T2 &...oth) {
int f = 0;
x = 0;
char ch = getc();//更快的读入
while (!isdigit(ch)) {
if (ch == '-') f = 1;
ch = getc();
}
while (isdigit(ch)) {
x = x * 10 + ch - 48;
ch = getc();
}
x = f ? -x : x;
read(oth...);
}
template <typename T, typename... T2>
inline void print(T x, T2... oth) { //没有引用
if (p3 > 1 << 20) //接近超空间自动刷新
flush();
if (x < 0)
buf2[++p3] = 45, x = -x; //45ascii是负号
do {
a[++p] = x % 10 + 48;
} while (x /= 10);
do {
buf2[++p3] = a[p];
} while (--p);
buf2[++p3] = hh; //换行
print(oth...);
}
}
#define read FastIO::read
#define print FastIO::print
int main() {
#ifndef ONLINE_JUDGE //如果未定义在线评测就执行下面语句
freopen("in.in", "r", stdin);
freopen("out.out", "w", stdout);
#endif
clock_t c1 = clock();
print(3, 4);
//======================================
int t;
read(t);
while (t--) {
int opt, x;
read(opt, x);
}
//======================================
FastIO::flush(); //手动输出
cerr << "Time:" << clock() - c1 << "ms" << std::endl;
return 0;
}
2.文件读入读出
从in.in读入,out.out读出,用于多次测试输入数据.
#include <iostream>
#include <ctime>
using namespace std;
int a[100];
int main() {
#ifndef ONLINE_JUDGE//如果未定义在线评测则读取
//重定向的标准输入和标准输出流到文件
freopen("in.in", "r", stdin);
freopen("out.out", "w", stdout);//
#endif
clock_t c1 = clock();
int n;
cin >> n;
for(int i = 0; i < n; ++ i){
cin >> a[i];
}
for(int i = 0; i < n; ++ i){
cout << a[i] << ' ';
}
cerr << "Time:" << clock() - c1 << "ms" << std::endl;//输出调试信息,标准输出已经被重定向
fclose(stdin);
fclose(stdout);
return 0;
}