返回首页 C++ 模板

输入输出模板

cin/cout 与 scanf/printf:竞赛中常用的输入输出方式

cin cout scanf printf 输入输出 GESP1
#include <bits/stdc++.h>
using namespace std;

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

    int a;
    cin >> a;           // 输入一个整数
    cout << a << endl;  // 输出并换行

    int b, c;
    cin >> b >> c;
    cout << b << " " << c << endl;

    // 读取整行字符串
    string s;
    cin.ignore();       // 清除缓冲区中的换行符
    getline(cin, s);
    cout << s << endl;

    // scanf/printf 方式
    int d; double e;
    scanf("%d %lf", &d, &e);
    printf("%d %.2f\n", d, e);

    // 已知组数输入
    int n; cin >> n;
    while (n--) { int x; cin >> x; }

    // 读到文件末尾
    int x;
    while (cin >> x) { /* 处理 x */ }

    return 0;
}

📖 要点说明

⚠️ 常见错误