diff --git a/20240827/preComp/2021 CSP-S 初赛试题(2).pdf b/20240827/preComp/2021 CSP-S 初赛试题(2).pdf new file mode 100644 index 0000000..1ffbdfc Binary files /dev/null and b/20240827/preComp/2021 CSP-S 初赛试题(2).pdf differ diff --git a/20240827/preComp/ans.txt b/20240827/preComp/ans.txt new file mode 100644 index 0000000..d3fe87b --- /dev/null +++ b/20240827/preComp/ans.txt @@ -0,0 +1,6 @@ +1.A 2.B 3.A 4.C 5.A 6.C 7.B 8.B 9.D 10.A 11.?A 12.C 13.D 14.?B 15.B +16.T 17.T 18.F 19.F 20.?D 21.D +21.?F 22.?T 23.?F 24.?T 25.?A 26.?A 27.?A +28.T 29.T 30.T 31.B 32.?B 33.?B +34.?B 35.B 36.B 37.C +38.?C 39.?A 40.D 41.?B 42.?C 43.?D \ No newline at end of file diff --git a/20240827/preComp/learn.md b/20240827/preComp/learn.md new file mode 100644 index 0000000..9fd9892 --- /dev/null +++ b/20240827/preComp/learn.md @@ -0,0 +1,87 @@ +# 阅读程序 + +## 1 + +在C++中,`cout.flags(ios::fixed)` 和 `cout.precision()` 是用于控制浮点数输出格式的操作。下面分别介绍它们的作用: + +### 1. `cout.flags(ios::fixed)` + +`cout.flags(ios::fixed)` 用于设置输出流的格式标志,指定以固定小数点记法(即定点格式)输出浮点数。 + +默认情况下,C++ 会以科学计数法或定点格式输出浮点数,具体取决于浮点数的大小。例如,较大的数字可能会以科学计数法的形式输出,而较小的数字会以定点格式输出。 + +使用 `ios::fixed` 作为标志后,所有浮点数都将使用定点格式输出,而不会切换到科学计数法。这意味着浮点数总是以小数点后固定的位数来表示。 + +示例: + +```cpp +#include +using namespace std; + +int main() { + double num = 123.456789; + + // 默认输出 + cout << "默认输出: " << num << endl; + + // 设置为定点格式 + cout.flags(ios::fixed); + cout << "定点格式输出: " << num << endl; + + return 0; +} +``` + +输出: + +``` +默认输出: 123.457 +定点格式输出: 123.456789 +``` + +### 2. `cout.precision()` + +`cout.precision(int n)` 用于设置浮点数输出时小数点后的精度(即显示多少位小数)。该函数有两种形式: + +- 无参数形式:返回当前的精度设置。 +- 有参数形式:设置新的精度并返回之前的精度。 + +在使用 `ios::fixed` 标志之后,`precision()` 函数控制小数点后输出的位数。例如,如果设置了 `cout.precision(2)`,则浮点数将以小数点后两位输出。 + +示例: + +```cpp +#include +using namespace std; + +int main() { + double num = 123.456789; + + // 设置为定点格式 + cout.flags(ios::fixed); + + // 设置精度为2 + cout.precision(2); + cout << "精度为2的定点格式输出: " << num << endl; + + // 设置精度为4 + cout.precision(4); + cout << "精度为4的定点格式输出: " << num << endl; + + return 0; +} +``` + +输出: + +``` +精度为2的定点格式输出: 123.46 +精度为4的定点格式输出: 123.4568 +``` + +### 总结 + +- `cout.flags(ios::fixed)`:设置输出浮点数为定点格式(固定小数点)。 +- `cout.precision(n)`:设置浮点数输出的精度,即小数点后显示的位数。 + +这两个函数常一起使用,控制浮点数的输出格式和精度。