#A001. 快速输入输出

快速输入输出

当前没有测试数据。

背景

当遇到大数据量量n=106n=10^6输入输出的时候, 通常的cincout就会带来TLETLE的问题

标准输入输出 慢

下面的输入输出代码需要耗时5.8s5.8s

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

const int MOD = 1e9 + 7;

int main() {
	int M, N;
	cin >> M >> N;
	int ans = 0;
	for (int i = 0; i < N; i++) {
		int x;
		cin >> x;
		ans = (ans + x) % MOD;
		if (M == 1) { cout << ans << endl; }
	}
	if (M == 0) { cout << ans << endl; }
}

输入输出 快

使用下面的两行代码, 速度会变很快

ios::sync_with_stdio(false);
cin.tie(nullptr);

下面的代码运行耗时 0.42s0.42s

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

const int MOD = 1e9 + 7;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int M, N;
	cin >> M >> N;
	int ans = 0;
	for (int i = 0; i < N; i++) {
		int x;
		cin >> x;
		ans = (ans + x) % MOD;
		if (M == 1) { cout << ans << "\n"; }
	}
	if (M == 0) { cout << ans << "\n"; }
}

特别注意,不要使用endl, 会严重影响程序的速度,用\n替代endl, 下面的代码耗时5.0s5.0s

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

const int MOD = 1e9 + 7;

int main() {
   ios::sync_with_stdio(false);
   cin.tie(nullptr);

   int M, N;
   cin >> M >> N;
   int ans = 0;
   for (int i = 0; i < N; i++) {
   	int x;
   	cin >> x;
   	ans = (ans + x) % MOD;
   	if (M == 1) { cout << ans << endl; }
   }
   if (M == 0) { cout << ans << endl; }
}

使用scanfprintf的速度也非常快, 下面的代码耗时0.42s0.42s

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

const int MOD = 1e9 + 7;

int main() {
	int M, N;
	scanf("%d%d", &M, &N);

	int ans = 0;
	for (int i = 0; i < N; i++) {
		int x;
		scanf("%d", &x);
		ans = (ans + x) % MOD;
		if (M == 1) { printf("%d\n", ans); }
	}
	if (M == 0) { printf("%d\n", ans); }
}

还有更快的操作 快读快写

下面的代码耗时0.17s0.17s

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

const int MOD = 1e9 + 7;
const int BUF_SZ = 1 << 15;

// BeginCodeSnip{Input}
inline namespace Input {
char buf[BUF_SZ];
int pos;
int len;
char next_char() {
	if (pos == len) {
		pos = 0;
		len = (int)fread(buf, 1, BUF_SZ, stdin);
		if (!len) { return EOF; }
	}
	return buf[pos++];
}

int read_int() {
	int x;
	char ch;
	int sgn = 1;
	while (!isdigit(ch = next_char())) {
		if (ch == '-') { sgn *= -1; }
	}
	x = ch - '0';
	while (isdigit(ch = next_char())) { x = x * 10 + (ch - '0'); }
	return x * sgn;
}
}  // namespace Input
// EndCodeSnip
// BeginCodeSnip{Output}
inline namespace Output {
char buf[BUF_SZ];
int pos;

void flush_out() {
	fwrite(buf, 1, pos, stdout);
	pos = 0;
}

void write_char(char c) {
	if (pos == BUF_SZ) { flush_out(); }
	buf[pos++] = c;
}

void write_int(int x) {
	static char num_buf[100];
	if (x < 0) {
		write_char('-');
		x *= -1;
	}
	int len = 0;
	for (; x >= 10; x /= 10) { num_buf[len++] = (char)('0' + (x % 10)); }
	write_char((char)('0' + x));
	while (len) { write_char(num_buf[--len]); }
	write_char('\n');
}

// auto-flush output when program exits
void init_output() { assert(atexit(flush_out) == 0); }
}  // namespace Output
// EndCodeSnip

int main() {
	init_output();
	int M = read_int();
	int N = read_int();
	int ans = 0;
	for (int i = 0; i < N; i++) {
		ans = (ans + read_int()) % MOD;
		if (M == 1) { write_int(ans); }
	}
	if (M == 0) { write_int(ans); }
}