1 条题解
-
0
作业介绍 d(`・∀・)b
- 基本模板 #include <bits/stdc++.h> using namespace std;
int main() { // 你的代码
return 0;}
- if / else 基本用法 int x; cin >> x;
if (x > 0) { cout << "positive\n"; } else if (x == 0) { cout << "zero\n"; } else { cout << "negative\n"; } 常见条件写法 if (x >= 1 && x <= 10) { } // 且 if (x == 3 || x == 7) { } // 或 if (!(x % 2 == 0)) { } // 非 2. switch 用于“等值分支”,条件通常是 int/char 之类的离散值。
int op; cin >> op;
switch (op) { case 1: cout << "one\n"; break; case 2: cout << "two\n"; break; default: cout << "other\n"; break; } 要点:break 防止“贯穿执行”。default 处理未匹配情况。
- for 循环 计数循环 for (int i = 0; i < 10; i++) { cout << i << " "; } cout << "\n"; 遍历数组/字符串(范围 for) string s = "abc"; for (char c : s) { cout << c << "\n"; }
- while 循环 条件循环 int x; cin >> x; while (x > 0) { cout << x << "\n"; x--; } 读到 EOF int a; while (cin >> a) { cout << a << "\n"; }
- 一维数组 静态数组(固定大小) int n; cin >> n; int a[100005]; // 需要足够大
for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) cout << a[i] << " "; cout << "\n"; 常用初始化 int b[5] = {1, 2, 3, 4, 5}; int c[100] = {0}; // 全部置 0 动态数组(vector) int n; cin >> n; vector a; for (int i = 0; i < n; i++) { int x; cin >> x; a.push_back(x); }
for (int i=0;i<(int)a.size()-1;i++) cout << a[i] << " "; cout << "\n";
for (int x : a) cout << a[i] << " "; cout << "\n"; 6. std::string 读入 string s; cin >> s; // 读一个“非空白串” 读整行(包含空格):
string line; getline(cin, line); // 注意:若前面刚用过 cin,可能要先 getline 读掉换行 常用操作 cout << s.size() << "\n"; // 长度 cout << s[0] << "\n"; // 下标访问(0 开始) s += "xyz"; // 拼接 遍历字符:
for (int i = 0; i < (int)s.size(); i++) { cout << s[i] << "\n"; } 子串:
string t = s.substr(2, 3); // 从下标 2 开始,取 3 个字符 7. 二维数组 静态二维数组(固定大小) int n, m; cin >> n >> m; int a[505][505]; // 需要足够大
for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> a[i][j]; } } cout << a[0][0] << "\n"; vector 二维数组 int n, m; cin >> n >> m; vector<vector> a(n, vector(m));
for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> a[i][j]; } } 8. 函数(Function) 基本定义与调用 int add(int a, int b) { return a + b; }
int main() { int x = add(2, 3); cout << x << "\n"; // 5 } 无返回值(void) void print_hello() { cout << "hello\n"; }
print_hello(); 传参方式 值传递(拷贝) void inc(int x) { x++; }
int a = 5; inc(a); cout << a << "\n"; // 5 引用传递 void inc(int &x) { x++; }
int a = 5; inc(a); cout << a << "\n"; // 6 void my_swap(int &a, int &b) { int tmp = a; a = b; b = tmp; }
int x = 3, y = 5; my_swap(x, y); const 引用(只读,避免拷贝) void print(const vector &v) { for (int x : v) cout << x << " "; } 返回多个值(pair / struct) pair<int,int> f(int x) { return {x, x * x}; }
auto [a, b] = f(3); // a=3, b=9 传入数组 void print_arr(int a[], int n) { for (int i = 0; i < n; i++) cout << a[i] << " "; }
int a[10] = {1, 2, 3, 4}; print_arr(a, 4); 传入 vector 二维数组 void print_grid(const vector<vector> &g) { for (const auto &row : g) { for (int x : row) cout << x << " "; cout << "\n"; } }
vector<vector> g = {{1, 2}, {3, 4}, {5, 6}}; print_grid(g); 9. 结构体 读入(数组) struct Person { int age; string name; };
const int MAXN = 1010; Person people[MAXN];
int n; cin >> n; for (int i = 0; i < n; i++) { cin >> people[i].age >> people[i].name; } 构造函数 struct Person { int age; string name; Person(int a, const string &n) : age(a), name(n) {} };
Person p(18, "Tom");
for (int i = 0; i < n; i++) { int age; string name; cin >> age >> name; people.push_back(Person(age, name)); } 10. 排序 对数组排序(默认升序 / greater 降序) int a[] = {3, 1, 4, 2}; int n = 4;
sort(a, a + n); // 默认升序,得到 {1, 2, 3, 4} sort(a, a + n, greater()); // 降序,得到 {4, 3, 2, 1}
int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8}; sort(a+3, a+7, greater()); // 降序排序 a[3],a[4],...,a[6](左闭右开),得到 {0, 1, 2, 6, 5, 4, 3, 7, 8} 对结构体使用 cmp 排序 struct Person { int age; string name; };
bool cmp(const Person &a, const Person &b) { return a.age < b.age; // 按年龄升序排序 }
vector people; sort(people.begin(), people.end(), cmp); 题目 状态 最后递交于 题目 没有递交 - luogu#P2142 高精度减法 没有递交 - luogu#P1009 [NOIP 1998 普及组] 阶乘之和 成绩表 我的最近递交记录 帮助 状态 正在进行… 题目 2 开始时间 2026-2-7 19:00 截止时间 2030-1-1 18:59 可延期 24 小时 状态 评测队列 服务状态 开发 开源 支持 帮助 联系我们 关于 关于 隐私 服务条款 版权申诉 Language 兼容模式 主题 © 2025 京ICP备2025145810号-1Worker 0, 11msPowered by Hydro v5.0.0-beta.17 Community
信息
- ID
- 13828
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 3
- 标签
- (无)
- 递交数
- 9
- 已通过
- 2
- 上传者