一、C++ 输入输出及相关处理操作
C++ 是算法竞赛中常用的语言,因其高效性适合处理大数据或严格时间限制的题目。输入输出是基础,但细节处理(如速度优化、格式要求)对比赛结果影响很大。
1. 基本输入输出
C++ 使用 <iostream>
库的 cin
和 cout
进行标准输入输出。
输入
-
单变量输入 :
cin
用于读取基本数据类型(如整数、浮点数、字符)。cpp#include <iostream> using namespace std; int main() { int x; cin >> x; // 输入:5 cout << x << endl; // 输出:5 return 0; }
cin
自动跳过空白字符(空格、换行、制表符)。- 支持类型:
int
,long long
,double
,char
,string
。
-
多变量输入 :
连续读取多个变量,用空格或换行分隔。
cppint a, b; cin >> a >> b; // 输入:3 4 cout << a + b << endl; // 输出:7
-
读取字符串:
-
读取不含空格的字符串:
cppstring s; cin >> s; // 输入:hello cout << s << endl; // 输出:hello
-
读取整行(含空格):
cppstring s; getline(cin, s); // 输入:hello world cout << s << endl; // 输出:hello world
-
注意:如果之前用过
cin >>
,需清除换行符:cppint x; cin >> x; // 输入:5 cin.ignore(); // 清除换行符 string s; getline(cin, s); // 输入:test case cout << x << " " << s << endl; // 输出:5 test case
-
-
输出
-
基本输出 :
cout
支持多种类型,endl
换行。cppint x = 123; cout << "Value: " << x << endl; // 输出:Value: 123
-
格式化输出 :
使用
<iomanip>
控制精度、宽度等。cpp#include <iomanip> double pi = 3.14159; cout << fixed << setprecision(2) << pi << endl; // 输出:3.14 int x = 42; cout << setw(5) << setfill('0') << x << endl; // 输出:00042
fixed
:固定小数点格式。setprecision(n)
:小数点后 n 位。setw(n)
:输出宽度 n。setfill(c)
:填充字符 c。
注意事项
cin
/cout
速度较慢,处理大数据时需优化。using namespace std;
简化代码,比赛中常用。
2. 加速输入输出
比赛中大数据量(如 10^6)可能导致 cin
/cout
超时,需优化。
-
关闭同步 :
禁用 C++ 和 C 风格输入输出的同步,提升速度。
cpp#include <iostream> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; // 输入:1000000 cout << n << endl; // 输出:1000000 return 0; }
cin.tie(nullptr)
解除cin
和cout
绑定。- 注意:启用后不能混用
scanf
/printf
。
-
使用 scanf/printf :
C 风格输入输出更快。
cpp#include <cstdio> int main() { int x; scanf("%d", &x); // 输入:123 printf("%d\n", x); // 输出:123 return 0; }
-
常用格式:
%d
:整数。%lld
:长整型。%lf
:双精度浮点数。%c
:字符。%s
:字符串。
-
示例(多变量):
cppint a, b; scanf("%d %d", &a, &b); // 输入:5 6 printf("%d\n", a + b); // 输出:11
-
读取字符串:
cppchar s[100]; scanf("%s", s); // 输入:hello printf("%s\n", s); // 输出:hello
-
含空格的字符串:
cppchar s[100]; scanf(" %[^\n]", s); // 输入:hello world printf("%s\n", s); // 输出:hello world
-
-
-
读取整行 :
使用
fgets
:cppchar s[100]; fgets(s, 100, stdin); // 输入:test line printf("%s", s); // 输出:test line
3. 常见输入模式
比赛中输入格式多样,以下是典型场景及示例:
-
固定数量输入 :
读取 n 个数到数组。
cppint n, a[100]; cin >> n; // 输入:5 for (int i = 0; i < n; i++) { cin >> a[i]; // 输入:1 2 3 4 5 } for (int i = 0; i < n; i++) { cout << a[i] << " "; // 输出:1 2 3 4 5 }
-
不定数量输入 :
读取直到文件末尾(EOF)。
cppint x; while (cin >> x) { // 输入:1 2 3(手动结束或 EOF) cout << x << endl; // 输出:1\n2\n3 }
-
使用
scanf
:cppint x; while (scanf("%d", &x) != EOF) { // 输入:4 5 6 printf("%d\n", x); // 输出:4\n5\n6 }
-
-
多组测试数据 :
处理 T 组数据。
cppint T; cin >> T; // 输入:2 while (T--) { int n; cin >> n; // 输入:3 cout << n * 2 << endl; // 输出:6 // 第二组输入:4,输出:8 }
-
矩阵输入 :
读取 n × m 矩阵。
cppint n, m, a[100][100]; cin >> n >> m; // 输入:2 3 for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> a[i][j]; // 输入:1 2 3 4 5 6 } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cout << a[i][j] << " "; // 输出:1 2 3 4 5 6 } cout << endl; }
4. 处理特殊输入
-
大整数 :
使用
long long
:cpplong long x; scanf("%lld", &x); // 输入:1234567890123 printf("%lld\n", x); // 输出:1234567890123
-
超大整数需字符串处理:
cppstring s; cin >> s; // 输入:999999999999999 cout << s << endl; // 输出:999999999999999
-
-
浮点数:
cppdouble x; scanf("%lf", &x); // 输入:3.14159 printf("%.2lf\n", x); // 输出:3.14
-
字符:
cppchar c; scanf(" %c", &c); // 输入:A(空格跳过空白) printf("%c\n", c); // 输出:A
-
字符串数组:
cppint n; cin >> n; // 输入:3 string s[100]; for (int i = 0; i < n; i++) { cin >> s[i]; // 输入:apple banana cherry } for (int i = 0; i < n; i++) { cout << s[i] << endl; // 输出:apple\nbanana\ncherry }
5. 常见输出技巧
-
多解输出(空格分隔):
cppint n = 3, ans[] = {1, 2, 3}; for (int i = 0; i < n; i++) { cout << ans[i]; if (i < n - 1) cout << " "; // 最后一个无空格 } cout << endl; // 输出:1 2 3
-
严格格式 :
避免多余空格或换行。
cppint n = 4, ans[] = {10, 20, 30, 40}; printf("%d", ans[0]); for (int i = 1; i < n; i++) { printf(" %d", ans[i]); } printf("\n"); // 输出:10 20 30 40
6. 比赛注意事项
-
文件输入输出:
cppfreopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); int x; cin >> x; // 从 input.txt 读 cout << x << endl; // 写到 output.txt
- 提交时注释掉
freopen
。
- 提交时注释掉
-
边界情况:
-
示例(检查空输入):
cppint n; cin >> n; if (n == 0) { cout << "Empty" << endl; return 0; }
-
检查负数:
cppint x; cin >> x; // 输入:-5 cout << (x >= 0 ? x : -x) << endl; // 输出:5
-
-
调试:
cppint x = 10; cout << "Debug: x = " << x << endl; // 输出:Debug: x = 10
二、Python 输入输出及相关处理操作
Python 因语法简洁和内置函数强大,适合初学者快速实现算法逻辑,但运行速度较慢,需注意优化。以下是详细讲解,每点附示例。
1. 基本输入输出
使用 input()
和 print()
进行输入输出。
输入
-
单变量:
pythonx = int(input()) # 输入:7 print(x) # 输出:7
-
input()
返回字符串,需转换类型。 -
浮点数:
pythonx = float(input()) # 输入:2.5 print(x) # 输出:2.5
-
-
多变量:
pythona, b = map(int, input().split()) # 输入:3 4 print(a + b) # 输出:7
-
读取整行:
pythons = input() # 输入:hello world print(s) # 输出:hello world
-
读取数组:
pythonarr = list(map(int, input().split())) # 输入:1 2 3 4 print(arr) # 输出:[1, 2, 3, 4]
输出
-
基本输出:
pythonx = 123 print("Value:", x) # 输出:Value: 123
-
控制分隔符和换行:
pythonprint(1, 2, 3, sep=',', end='!') # 输出:1,2,3!
-
格式化输出:
-
f-string:
pythonpi = 3.14159 print(f"{pi:.2f}") # 输出:3.14
-
宽度控制:
pythonx = 42 print(f"{x:05d}") # 输出:00042
-
2. 加速输入输出
Python 输入输出较慢,需优化。
-
快速输入 :
使用
sys.stdin.readline
:pythonimport sys input = sys.stdin.readline x = int(input()) # 输入:1000000 print(x) # 输出:1000000
-
读取数组:
pythonarr = list(map(int, input().split())) # 输入:1 2 3 print(arr) # 输出:[1, 2, 3]
-
去除换行符:
pythons = input().strip() # 输入:test\n print(s) # 输出:test
-
-
读取多行:
pythonimport sys lines = sys.stdin.readlines() for line in lines: print(line.strip()) # 输入:line1\nline2\n,输出:line1\nline2
-
输出优化 :
集中输出:
pythonans = [1, 2, 3] print(' '.join(map(str, ans))) # 输出:1 2 3
3. 常见输入模式
-
固定数量:
pythonn = int(input()) # 输入:4 arr = list(map(int, input().split())) # 输入:5 6 7 8 print(sum(arr)) # 输出:26
-
不定数量:
pythonwhile True: try: x = int(input()) # 输入:1 2 3(手动结束) print(x) # 输出:1\n2\n3 except EOFError: break
-
多组测试数据:
pythonT = int(input()) # 输入:2 for _ in range(T): n = int(input()) # 输入:5 print(n * 2) # 输出:10 # 第二组输入:3,输出:6
-
矩阵输入:
pythonn, m = map(int, input().split()) # 输入:2 3 matrix = [list(map(int, input().split())) for _ in range(n)] # 输入:1 2 3 4 5 6 print(matrix) # 输出:[[1, 2, 3], [4, 5, 6]]
4. 处理特殊输入
-
大整数 :
Python 无整数范围限制:
pythonx = int(input()) # 输入:999999999999 print(x) # 输出:999999999999
-
浮点数:
pythonx = float(input()) # 输入:3.14159 print(f"{x:.2f}") # 输出:3.14
-
字符串与字符:
pythons = input() # 输入:abc for c in s: print(c) # 输出:a\nb\nc
5. 常见输出技巧
-
多解输出:
pythonans = [1, 2, 3] print(*ans) # 输出:1 2 3
-
严格格式:
pythonans = [10, 20, 30] print(' '.join(map(str, ans))) # 输出:10 20 30
6. 比赛注意事项
-
文件输入输出:
pythonimport sys sys.stdin = open("input.txt", "r") sys.stdout = open("output.txt", "w") x = int(input()) # 从 input.txt 读 print(x) # 写到 output.txt
-
效率:
-
避免字符串拼接:
pythonans = [] for i in range(3): ans.append(str(i)) print(' '.join(ans)) # 输出:0 1 2
-
-
异常处理:
pythonwhile True: try: n = int(input()) # 输入:5 print(n) # 输出:5 except: break
-
调试:
pythonx = 10 print(f"Debug: x = {x}", file=sys.stderr) # 输出到错误流
三、比赛中常见技巧与注意事项
-
模板化:
-
准备常用输入输出模板,比赛时直接调用,节省时间。
-
示例(C++):
cpp#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; // 逻辑代码 return 0; }
-
示例(Python):
pythonimport sys input = sys.stdin.readline n = int(input()) arr = list(map(int, input().split())) # 逻辑代码
-
-
边界检查:
- 输入是否为空。
- 数据范围是否超出
int
(需用long long
或 Python)。 - 数组是否越界。
-
格式要求:
- 注意空格、换行、末尾是否多余。
- 严格按题目要求输出(如无多余空格)。
-
时间优化:
- 使用快速输入输出。
- 避免不必要的循环或递归。