75
CSP-J 核心要点
🔒
CSP-S 提分秘籍
游客预览
当前权限
提示:你为游客,仅预览 CSP-J 前 15 项。登录后查看更多。
计算机与编程环境
6 项
1
计算机基本构成
冯·诺依曼结构:运算器+控制器(合称CPU)+存储器(内存/外存)+输入/输出设备。程序和数据都以二进制形式存储在内存中。
2
文件扩展名与存储单位
C++源文件 .cpp,可执行文件 .exe(Win),头文件 .h/.hpp。1 Byte=8 bit,1KB=1024B,1MB=1024KB,1GB=1024MB。int 占 4B,long long 占 8B,char 占 1B。int a[1e7] ≈ 40MB。
3
编译与运行基本概念
C++ 是编译型语言:源代码→预处理→编译→链接→可执行文件→运行。CE(编译错误):语法错;RE(运行时错误):除0/越界/栈溢出;TLE:超时;MLE:超内存。
4
操作系统基本概念
Linux(CSP复赛环境):路径用 / 分隔,根目录 /,家目录 ~。常用命令:ls列目录、cd切换、mkdir创建、cp复制、mv移动。Windows:路径用 \ 分隔,盘符如 C:。
5
ASCII 码与字符编码
常用:空格32,'0'=48,'A'=65,'a'=97,大小写差32。c-'0' 将字符数字转 int;s[i]-'a'+'A' 小写→大写。char 本质是 0~255 的整数。
c++
if(isdigit(c))x=c-'0'; // 字符→数字
char L=toupper('a'); // 'A'
char s=tolower('Z'); // 'z'
cout<<(int)'A'; // 656
g++ 编译命令
CSP 复赛在 Linux 终端编译:g++ -std=c++17 -O2 -Wall a.cpp -o a。-O2 开启优化(约 2-5 倍速),-g 加调试信息,-Wall 显示所有警告。提交前务必用 -O2 编译测试。
C++ 程序基础
9 项
7
#include <bits/stdc++.h>
万头文件,包含几乎所有标准库,仅限竞赛环境。工程代码不推荐。需配合 using namespace std; 省去 std:: 前缀。
c++
#include <bits/stdc++.h>
using namespace std;
int main(){return 0;}8
int 与 long long
int 范围约 ±2.1×10⁹;超过必须用 long long(约 ±9.2×10¹⁸)。凡涉及乘法、累加、计数运算——一律开 long long。
c++
#define int long long
signed main(){return 0;}9
const 与 #define
常量推荐用 const(类型安全);#define 仅文本替换。全局 const 数组大小可开很大。
c++
const int N=1e5+5; const double PI=acos(-1.0);
int a[N]; // ✅ 全局 N 是编译期常量10
全局 vs 局部变量
大数组必须定义在全局区(main外面)。局部数组在栈上分配,开 int a[1e6] 会栈溢出 RE。全局变量默认初始化为 0,局部是随机垃圾值。
c++
const int N=1e5+5; int a[N]; // ✅ 全局,自动清0
int main(){int c[100]={0};} // ✅ 局部显式初始化11
cin / cout 加速
必须加 ios::sync_with_stdio(false);cin.tie(0); 否则大数据输入 TLE。加上后不能再混用 scanf/printf。
c++
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);12
scanf / printf 格式符
%d→int,%lld→long long,%lf→double(输入),%f→double(输出),%c→char,%s→字符串。long long 必须用 %lld!
c++
scanf("%d%lld%lf%s",&a,&b,&c,s);
printf("%d %lld %.6f\n",a,b,c);13
文件输入输出(复赛必考)
CSP 复赛必须用文件读写。文件名拼错直接爆零。在 main() 最开头写 freopen。
c++
freopen("problem.in","r",stdin);
freopen("problem.out","w",stdout);
// 你的代码
fclose(stdin);fclose(stdout);14
浮点数与精度
double 精度约 15~16 位;float 约 6~7 位(竞赛基本不用)。不能用 == 比较浮点数,用 fabs(a-b)<1e-8。
c++
const double eps=1e-8;
if(fabs(a-b)<eps) cout<<"≈";
printf("%.6f\n",x);15
数学库函数(cmath)
abs(x)绝对值;ceil/floor取整;sqrt(x)平方根;pow(a,b)幂;log(x)自然对数;round(x)四舍五入;acos(-1.0)获取π。
c++
double pi=acos(-1.0);
int a=ceil(3.1); // 4
int b=floor(3.9); // 3
int c=round(3.5); // 4