跳转至

输入输出

cin, cout

C++语言的输入输出

#include <iostream>

using namespace std;

int main() {
    int a, b;
    cin >> a >> b;

    cout << a + b;

    return 0;
}

This operator (>>) applied to an input stream is known as extraction operator. (Extracts and parses characters sequentially from the stream)

This operator (<<) applied to an output stream is known as insertion operator. (inserts them into the output stream)

scanf, printf

C语言的输入输出

格式化输入输出语句

#include <cstdio>

using namespace std;

int main() {
    int a, b;
    scanf("%d%d", &a, &b);

    printf("%d", a + b);

    return 0;
}
// 占位符对应关系
int %d
double %lf
char %c
long long %lld
// 读入时间格式 12:09
int h, m;
scanf("%d:%d", &h, &m);

如果要一次输出多个变量

在双引号里面,就是要输出的格式,占位符与后面的变量一一对应

printf("%d+%d=%d", a, b, a + b);

保留小数点问题

第1种写法最常用
#include <cstdio>
printf("%.2lf", x);
第2种写法阅读程序有时会遇到
#include <iomanip>
cout << fixed << setprecision(2) << x;