- xuyanbo's blog
@鞠宪庆 (juxianqing) 转载供我自己食用 复赛知识
- 2024-10-4 9:11:59 @
@鞠宪庆 (juxianqing) 转载供我自己食用
编程语言 基本知识总结
键盘的键位
1234 5 6 7890
qwer t y uiop
asdf g h jkl;
zxcv b n m,./
[Copy](javascript:;)
左手小指 | 左手无名指 | 左手 中指 | 左手食指 1 | 左手食指 2 | 右手 食指 2 | 右手 食指 1 | 右手中指 | 右手 无名指 | 右手 小指 |
---|---|---|---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0 |
Q | W | E | R | T | Y | U | I | O | P |
A | S | D | 【F】 | G | H | 【J】 | K | L | ; |
Z | X | C | V | B | N | M | , | . | / |
编程必背单词 第1期
- include 包含
- using 使用,正在进行时
- namespace 命名空间
- std (standard), 标准
- int (integer), 整数类型
- main 主要的
- in/out 进入/出来
- i/o (input/output) 输入输出
- end 结束
- return 返回
计算机英语 第2期
- float 浮动的
- double 成双的
- fixed 修理
- set 设置
- precision 精度
- if 如果
- else 否则
- while 当 ... 时候
- do 做
- for 给、对、为
- continue 继续
- break 打断
- pow 乘方
- sqrt 开平方
- cbrt 开立方
- ceil 天花板
- floor 地板
- round 圆的(四舍五入)
常用缩写
- n : number 数字
- num : number 数字
- a : array 数组
- arr : arrary 数组
- vec : vector 向量
- c : char 字符
- ch : char 字符
- s : string 字符串
- str : string 字符串
- l : long long 超长整型
- f : float 单精度浮点数
- d : double 双精度浮点数
- std : standard 标准
- ans : answer 答案
- sum : summary 总和
- cnt : counter 计数器
- res : result 结果
- slt : solution 方案
- cur : current 当前
- vis : visit 访问
- tot : total 总共
Dev-C++常用快捷键
- Ctrl + N : New 新建
- Ctrl + S : Save 保存
- Ctrl + O : Open 打开
- F11 : Compile & Run 编译并运行
- Ctrl + A : All 全选
- Ctrl + C : Copy 复制
- Ctrl + X : Cut 剪切
- Ctrl + V : Paste 粘贴
- Ctrl + 滚轮:放大或缩小
- Tab : Indent缩进
- Shift + Tab : Unindent缩回
让计算机开口说话
#include <bits/stdc++.h>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}
[Copy](javascript:;)
#include <bits/stdc++.h> // 包含所有C++标准库的头文件
using namespace std; // 使用标准命名空间,无需每次都加 std:: 前缀
// 主函数,程序从这里开始执行
int main() {
// 打印输出 "Hello, World!" 并换行
cout << "Hello, World!" << endl;
// cout 是标准输出流对象,用于输出数据到控制台
// "Hello, World!" 是要输出的字符串
// << 是流插入运算符,将右侧的内容插入到左侧的输出流中
// endl 是换行符,表示输出一个新行并刷新输出缓冲区
return 0; // 返回0,表示程序正常结束
// main 函数的返回值类型是 int,返回 0 表示程序正常结束
// 非0的返回值通常表示程序有错误发生
}
[Copy](javascript:;)
编程四部曲
// (1) define 定义
// (2) input 输入
// (3) process 处理
// (4) output 输出
[Copy](javascript:;)
让计算机做算术题
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, sum;
cin >> a >> b;
sum = a + b;
cout << sum << endl;
return 0;
}
[Copy](javascript:;)
#include <bits/stdc++.h>
using namespace std;
int main() {
// (1) define 定义
int a, b, sum;
// (2) input 输入
cin >> a >> b;
// (3) process 处理
sum = a + b;
// (4) output 输出
cout << sum << endl;
return 0;
}
[Copy](javascript:;)
编程要素 知识总结
#include <bits/stdc++.h>
using namespace std;
int main() {
// 1. 基本数据类型
// (1) 整型数据类型
// int 整型 (4个字节): -21亿 ~ 21亿 (10位数字)
// long long 超长整型 (8个字节): -922亿亿 ~ 922亿亿 (19位数字)
long long a, b;
cin >> a >> b;
cout << a + b << endl;
// char 字符型 (1个字节) :-128 ~ 127
char c = 'A' + 15;
cout << c << endl;
cout << int(c) << endl;
// bool 逻辑型 (1个字节) :-128 ~ 127
int n;
cin >> n;
bool flag = (n == 100); // true : 1, false : 0
cout << flag << endl;
// (2) 浮点数据类型
// 123.456 : 0.123456 x 10的3次方
// float 单精度 (4个字节): 7位有效数字,正负10的38次方
// double 双精度 (8个字节): 17位有效数字,正负10的109次方
double db = 1.0 * 10 / 3;
cout << db << endl;
cout << fixed << setprecision(14) << db << endl;
// 2. 变量、常量
// (1) 变量
int n;
char c;
// (2) 常量
const double pi = 3.1415926535897932;
// pi = 3.14;
cout << pi << endl;
// 3. 运算符
// (1) 算术运算符: + - * / %
double R = 1.0 / (1.0 / r1 + 1.0 / r2)
cout << pow(2, 3) << endl; // 乘方
cout << sqrt(16) << endl; // 算术平方根
cout << cbrt(8) << endl; // 算术立方根
cout << abs(-10) << endl; // 整数绝对值
cout << fabs(-8.3) << endl; // 浮点数绝对值
cout << ceil(1.32) << endl; // 向上取整
cout << floor(1.82) << endl; // 向下取整
cout << int(1.82) << endl; // 向下取整
cout << round(3.45) << endl; // 四舍五入取整
cout << ceil(-1.32) << endl; // 向上取整
cout << floor(-1.82) << endl; // 向下取整
cout << int(-1.82) << endl; // 向下取整
cout << round(-3.45) << endl; // 四舍五入取整
cout << round(3.433 * 100) / 100 << endl;
cout << fixed << setprecision(2) << 3.433 << endl;
// 2. 关系运算符 > , >= , <, <=, ==, !=
int n;
cin >> n;
if (!(n > 0)) {
cout << "真 true 成立" << endl;
} else {
cout << "假 false 不成立" << endl;
}
// 3. 逻辑运算符: 逻辑与 &&, 逻辑或 ||, 逻辑非 !
// n > 0 && n < 100 (相当于 0 < n < 100)
// n < 0 || n > 100 (相当于 n < 0, 或 n > 100)
// !(n > 0) (相当于 非正数)
return 0;
}
[Copy](javascript:;)
流程控制结构 - 顺序结构、分支结构 知识总结
#include <bits/stdc++.h>
using namespace std;
int main() {
// 1、顺序结构
int a, b, c;
cin >> a >> b;
// c = a; // c <- a
// a = b; // a <- b
// b = c; // b <- c
swap(a, b);
cout << a << " " << b << endl;
// 2、分支(选择)结构
// (1) 单分支结构:求整数的绝对值
int n;
cin >> n;
if (n < 0) { // true 成立 真
n = -n;
}
cout << n << endl;
// (2) 双分支结构:判断整数的奇偶性
int n;
cin >> n;
if (n % 2 == 0) {
cout << "even" << endl;
} else {
cout << "odd" << endl;
}
if (n % 2) { // 0 : false 不成立, 非0 : true 成立
cout << "odd" << endl;
} else {
cout << "even" << endl;
}
// (3) 多分支 :判断整数的正负性
int n;
cin >> n;
if (n > 0) {
cout << "positive" << endl;
} else if (n < 0) {
cout << "negative" << endl;
} else {
cout << "zero" << endl;
}
// 成绩等级判断
// A : 90 ~ 100
// B : 80 ~ 89
// C : 70 ~ 79
// D : 60 ~ 69
// E : 0 ~ 59
int n;
cin >> n;
if (n >= 90) {
cout << "A" << endl;
} else if (n >= 80) {
cout << "B" << endl;
} else if (n >= 70) {
cout << "C" << endl;
} else if (n >= 60) {
cout << "D" << endl;
} else {
cout << "E" << endl;
}
// (4) 分支结构嵌套 :三个整数比大小
int a, b, c;
cin >> a >> b >> c;
if (a > b) {
if (a > c) {
cout << a << endl;
} else {
cout << c << endl;
}
} else {
if (b > c) {
cout << b << endl;
} else {
cout << c << endl;
}
}
// 使用 max 函数
cout << max(max(a, b), c) << endl;
// (5) switch 开关分支结构: 星期判断
int n;
cin >> n;
switch (n) {
case 1:
cout << "Monday" << endl;
break;
case 2:
cout << "Tuesday" << endl;
break;
case 3:
cout << "Wednesday" << endl;
break;
case 4:
cout << "Thursday" << endl;
break;
case 5:
cout << "Friday" << endl;
break;
case 6:
cout << "Saturday" << endl;
break;
case 7:
cout << "Sunday" << endl;
break;
default:
cout << "Error" << endl;
}
// (6) 三目运算符 ? :
bool flag; // 1 : true, 0 : false
cin >> flag;
if (flag) {
cout << "true" << endl;
} else {
cout << "false" << endl;
}
// 三目运算符 (简洁、省事)
cout << (flag ? "true" : "false") << endl;
return 0;
}
[Copy](javascript:;)
流程控制结构 - 循环结构 知识总结
#include <bits/stdc++.h>
using namespace std;
int main() {
// 3、循环结构
// (1) 前置条件循环 while () { } 对比 if () {}
int i = 1;
while (i <= 9) {
cout << "我要上天 " << i << " 重" << endl;
i++; // 相当于 i = i + 1;
}
int j = 0;
while (j < 18) {
cout << "他要入地 " << j << " 层" << endl;
j++;
}
// 高斯求和: 1 + 2 + 3 + ... + 18
// 1 = 0 + 1;
// 3 = 1 + 2;
// 6 = 3 + 3;
// 10 = 6 + 4;
int i = 1; // 循环变量 初始化
int sum = 0;
while (i <= 100) { // 循环条件
sum = sum + i;
i++; // 循环变量 渐变
}
cout << sum << endl;
// (2) 后置条件循环 do{ } while();
int i = 1;
do {
cout << "我要上天 " << i << " 重" << endl;
i++;
} while (i <= 9);
int j = 0;
do {
cout << "他要入地 " << j << " 层" << endl;
j++;
} while (j < 18);
int i = 1; // (1) 循环变量 初始化
int sum = 0;
do { // (2) 循环条件
sum = sum + i;
i++; // (3) 循环变量 渐变
} while (i <= 100);
cout << sum << endl;
// (3) 计数循环 for (循环变量初始化; 循环条件; 循环变量 渐变) { 代码块 }
for (int i = 1; i <= 9; i++) {
cout << "我要上天 " << i << " 重" << endl;
}
for (int j = 0; j < 18; j++) {
cout << "他要入地 " << j << " 层" << endl;
}
int sum = 0;
for (int i = 1; i <= 100; i++) {
sum = sum + i;
}
cout << sum << endl;
// (4) 无限循环 : while(true) {} 或 for (;;) {}
int num;
int sum = 0;
while (true) {
cin >> num;
if (num == 0) {
break;
}
sum += num; // 相当于 sum = sum + num;
}
cout << sum << endl;
// 无限循环
for (;;) {
}
// (5) 循环语句 :
// continue 跳过当前循环
// break 停止后续所有循环
for (int i = 1; i <= 18; i++) {
if (i == 4)
continue;
if (i == 14)
continue;
cout << "他要入地 " << i << " 层" << endl;
}
// (6) 遍历循环 :遍历输出容器内的所有元素
int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for (auto num : arr) {
cout << num << " ";
}
cout << endl;
string s = "Hello,World!";
for (auto c : s) {
cout << c << " ";
}
cout << endl;
return 0;
}
[Copy](javascript:;)
流程控制结构 - 循环结构嵌套 知识总结
#include <bits/stdc++.h>
using namespace std;
int main() {
// 4、循环结构嵌套
//(1) 矩形星阵
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cout << "*";
}
cout << endl;
}
// (2) 直角三角形星阵
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
cout << "*";
}
cout << endl;
}
// (3) 等腰三角形星阵
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
for (int k = 1; k <= n - i; k++) {
cout << " ";
}
for (int j = 1; j <= 2 * i - 1; j++) {
cout << "*";
}
cout << endl;
}
// (4) 倒置等腰三角形星阵
int n;
cin >> n;
for (int i = n; i >= 1; i--) {
for (int k = 1; k <= n - i; k++) {
cout << " ";
}
for (int j = 1; j <= 2 * i - 1; j++) {
cout << "*";
}
cout << endl;
}
// (5) 九九乘法表
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
cout << j << "*" << i << "=" << i * j << " ";
}
cout << endl;
}
return 0;
}
[Copy](javascript:;)
鸡兔同笼 & 百钱买百鸡 知识总结
#include <bits/stdc++.h>
using namespace std;
int main() {
// 鸡兔同笼是大约在1500年前,《孙子算经》中记载的有趣的问题。
// 今有雉兔同笼,上有三十五头,下有九十四足,问雉兔各几何?
// 鸡兔同笼
for (int i = 1; i <= 34; i++) {
for (int j = 1; j <= 34; j++) {
if ((i + j == 35) && (2 * i + 4 * j == 94)) {
cout << i << " " << j << endl;
}
}
}
// 百鸡百钱是我国古代数学家张丘建在《算经》一书中提出的数学问题:
// “鸡翁一值钱五,鸡母一值钱三,鸡雏三值钱一。
// 百钱买百鸡,问鸡翁、鸡母、鸡雏各几何?”
// 百钱买百鸡 : 100 * 100 * 100 = 1000000
for (int i = 1; i <= 100; i++) {
for (int j = 1; j <= 100; j++) {
for (int k = 1; k <= 100; k++) {
if (i + j + k == 100 && 5 * i + 3 * j + k / 3.0 == 100) {
cout << i << " " << j << " " << k << endl;
}
}
}
}
// 百钱买百鸡 : 20 * 33 * 100 = 66000
for (int i = 1; i <= 100 / 5; i++) {
for (int j = 1; j <= 100 / 3; j++) {
for (int k = 1; k <= 100; k++) {
if (i + j + k == 100 && 5 * i + 3 * j + k / 3.0 == 100) {
cout << i << " " << j << " " << k << endl;
}
}
}
}
// 百钱买百鸡 : 20 * 33 = 660
for (int i = 1; i <= 100 / 5; i++) {
for (int j = 1; j <= 100 / 3; j++) {
if (5 * i + 3 * j + (100 - i - j) / 3.0 == 100) {
cout << i << " " << j << " " << 100 - i - j << endl;
}
}
}
return 0;
}
[Copy](javascript:;)
#include <bits/stdc++.h>
using namespace std;
int main() {
// 鸡兔同笼 : 穷举
for (int i = 1; i <= 35; i++) {
for (int j = 1; j <= 35; j++) {
if (i + j == 35 && 2 * i + 4 * j == 94) {
cout << i << " " << j << endl;
}
}
}
// 鸡兔同笼 : 穷举 优化
for (int i = 1; i <= 35; i++) {
// j = (35 - i)
if (i + (35 - i) == 35 && 2 * i + 4 * (35 - i) == 94) {
cout << i << " " << (35 - i) << endl;
}
}
// 百钱买百鸡 : 100 * 100 * 100 = 100 0000
for (int i = 1; i <= 100; i++) {
for (int j = 1; j <= 100; j++) {
for (int k = 1; k <= 100; k++) {
if (i + j + k == 100 && 5 * i + 3 * j + 1.0 / 3 * k == 100) {
cout << i << " " << j << " " << k << endl;
}
}
}
}
// 百钱买百鸡 (剪枝优化): 20 * 33 * 100 = 66 000
for (int i = 1; i <= 100 / 5; i++) {
for (int j = 1; j <= 100 / 3; j++) {
for (int k = 1; k <= 100; k++) {
if (i + j + k == 100 && 5 * i + 3 * j + 1.0 / 3 * k == 100) {
cout << i << " " << j << " " << k << endl;
}
}
}
}
// 百钱买百鸡 (剪枝优化): 20 * 33 = 660
for (int i = 1; i <= 100 / 5; i++) {
for (int j = 1; j <= 100 / 3; j++) {
// k = (100 - i - j)
if (i + j + (100 - i - j) == 100 &&
5 * i + 3 * j + 1.0 / 3 * (100 - i - j) == 100) {
cout << i << " " << j << " " << (100 - i - j) << endl;
}
}
}
return 0;
}
[Copy](javascript:;)
数学专题 知识总结
#include <bits/stdc++.h>
using namespace std;
bool prime(int n) {
if (n < 2) {
return false;
} else {
int nn = sqrt(n);
for (int i = 2; i <= nn; i++) {
if (n % i == 0) {
return false;
}
}
}
return true;
}
int main() {
// 数学专题
// 穷举法 求所有公因数
int a, b;
cin >> a >> b;
if (a > b)
swap(a, b);
for (int i = 1; i <= a; i++) {
if (a % i == 0 && b % i == 0) {
cout << i << endl;
}
}
// 穷举法 求最大公因数
for (int i = a; i >= 1; i--) {
if (a % i == 0 && b % i == 0) {
cout << i << endl;
break;
}
}
// 辗转相除法 求最大公因数
int a, b, c;
cin >> a >> b;
if (a < b)
swap(a, b);
c = a % b;
while (c > 0) {
a = b;
b = c;
c = a % b;
}
cout << b << endl;
// 穷举法 求最小公倍数
int a, b;
cin >> a >> b;
if (a > b)
swap(a, b);
for (int i = b; i <= a * b; i++) {
if (i % a == 0 && i % b == 0) {
cout << i << endl;
break;
}
}
// 穷举法优化 求最小公倍数
int a, b;
cin >> a >> b;
if (a > b)
swap(a, b);
for (int i = b; i <= a * b; i = i + b) {
if (i % a == 0 && i % b == 0) {
cout << i << endl;
break;
}
}
// 借助最大公约数的终极优化
lcm(a, b) = (a * b) / gcd(a, b);
// 判断质数(素数) (穷举大法)
int n;
cin >> n;
bool isPrime = true;
if (n < 2) {
isPrime = false;
} else {
for (int i = 2; i < n; i++) {
if (n % i == 0) {
isPrime = false;
break;
}
}
}
if (isPrime) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
// 判断质数(素数) (优化)
int n;
cin >> n;
bool isPrime = true;
if (n < 2) {
isPrime = false;
} else {
int nn = sqrt(n);
for (int i = 2; i <= nn; i++) {
if (n % i == 0) {
isPrime = false;
break;
}
}
}
if (isPrime) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
// 哥德巴赫猜想:任一大于2的偶数都可写成两个质数之和
int cnt = 0;
for (int n = 4; n <= 1000; n += 2) {
for (int i = 2; i < n - 1; i++) {
if (prime(i) && prime(n - i)) {
cout << n << "=" << i << "+" << n - i << endl;
cnt++;
break;
}
}
}
cout << "cnt = " << cnt << endl;
return 0;
}
[Copy](javascript:;)
函数专题 知识总结
#include <bits/stdc++.h>
using namespace std;
int MAX(int a, int b); // 函数的声明
int main() {
int a, b;
cin >> a >> b;
cout << MAX(a, b) << endl; // 函数的调用
return 0;
}
int MAX(int a, int b) { // 函数的定义
if (a > b) {
return a;
} else {
return b;
}
}
[Copy](javascript:;)
一维数组的基本操作
#include <bits/stdc++.h>
using namespace std;
int main() {
// (1) 一维数组的基本操作
const int MAXSIZE = 1000; // 定义常量
int arr[MAXSIZE + 1]; // 定义数组
// int arr[1000 + 1]; //
//也可以 vector<int> vec; //
//向量(变长数组)
memset(arr, 0, sizeof(arr)); // 数组置零
// 数组的引用
arr[0] = 102;
arr[1] = 105;
arr[2] = 104;
cout << arr[0] + arr[1] + arr[2] << endl;
int n;
cin >> n;
// 从 1 号 下标 (索引 index) 使用数组
// 批量读入
for (int i = 1; i <= n; i++) {
cin >> arr[i];
}
// 批量处理(排序)
sort(arr + 1, arr + 1 + n); // 对 1 ~ n 号 元素 进行排序
// 批量输出
for (int i = 1; i <= n; i++) {
cout << arr[i] << " ";
}
cout << endl;
// 从 0 号 下标 (索引 index) 使用数组
// 批量读入
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
// 批量处理(排序)
sort(arr, arr + n); // 对 0 ~ n - 1 号 元素 进行排序
// 批量输出
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
[Copy](javascript:;)
一维数组的查找
#include <bits/stdc++.h>
using namespace std;
int main() {
// 2、一维数组的查找
int n;
cin >> n;
int key = 108;
int arr[100 + 1];
memset(arr, 0, sizeof(arr));
// 数组的批量读入
for (int i = 1; i <= n; i++) {
cin >> arr[i];
}
// (1) 顺序查找
bool found = false;
for (int i = 1; i <= n; i++) {
if (arr[i] == key) {
found = true;
}
}
if (found) {
cout << "found" << endl;
} else {
cout << "not found" << endl;
}
// (2) 二分法查找
bool found = false;
int left = 1;
int right = n;
int mid;
while (left <= right) {
mid = (left + right) / 2;
if (key == arr[mid]) {
found = true;
break;
} else if (key < arr[mid]) {
right = mid - 1;
} else {
left = mid + 1;
}
}
if (found) {
cout << "found" << endl;
} else {
cout << "not found" << endl;
}
return 0;
}
[Copy](javascript:;)
一维数组的排序
#include <bits/stdc++.h>
using namespace std;
int main() {
// 3、一维数组的排序
int n;
cin >> n;
const int MAXSIZE = 100 + 1;
int arr[MAXSIZE];
memset(arr, 0, sizeof(arr));
for (int i = 1; i <= n; i++) {
cin >> arr[i];
}
// (1) 冒泡排序
for (int i = 1; i <= n; i++) {
for (int j = n; j > i; j--) {
if (arr[j] < arr[j - 1]) { // < 升序,> 降序
swap(arr[j], arr[j - 1]);
}
}
}
// (2) 冒泡排序(优化,用于接近完成的序列)
bool exchange;
for (int i = 1; i <= n; i++) {
exchange = false;
for (int j = n; j > i; j--) {
if (arr[j] < arr[j - 1]) { // < 升序,> 降序
swap(arr[j], arr[j - 1]);
cout << "swap : " << arr[j] << " <-> " << arr[j - 1] << endl;
exchange = true;
}
}
if (!exchange)
break;
}
// (3) 选择排序
for (int i = 1; i < n; i++) {
for (int j = i; j <= n; j++) {
if (arr[i] > arr[j]) { // 确定擂主
swap(arr[i], arr[j]);
}
}
}
// (3) 插入排序
for (int i = 1; i <= n; i++) {
for (int j = i; j > 1; j--) {
if (arr[j] < arr[j - 1]) {
swap(arr[j], arr[j - 1]);
}
}
}
for (int i = 1; i <= n; i++) {
cout << arr[i] << " ";
}
cout << endl;
// (4) 计数排序
int n;
cin >> n;
const int MAXSIZE = 100 + 1;
int arr[MAXSIZE];
memset(arr, 0, sizeof(arr));
int num;
for (int i = 1; i <= n; i++) {
cin >> num;
arr[num]++;
}
// 去重输出
for (int i = 1; i <= MAXSIZE; i++) {
if (arr[i] > 0) {
cout << i << " ";
}
}
cout << endl;
// 重复元素输出
for (int i = 1; i <= MAXSIZE; i++) {
while (arr[i] > 0) {
cout << i << " ";
arr[i]--;
}
}
cout << endl;
// (5) sort 排序
int n;
cin >> n;
const int MAXSIZE = 100 + 1;
int arr[MAXSIZE];
memset(arr, 0, sizeof(arr));
for (int i = 1; i <= n; i++) {
cin >> arr[i];
}
sort(arr + 1, arr + 1 + n); // 升序排序
sort(arr + 1, arr + 1 + n, greater<int>()); // 降序排序
for (int i = 1; i <= n; i++) {
cout << arr[i] << " ";
}
cout << endl;
int cnt =
unique(arr + 1, arr + 1 + n) - (arr + 1); // 去重 , 并返回去重后的数量
for (int i = 1; i <= cnt; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
[Copy](javascript:;)
YBT2037.约瑟夫问题
#include <bits/stdc++.h>
using namespace std;
int main() {
int N, M; // 总人数,报数数
int cnt = 0, check = 0, remain; // 计数器,检查器,剩余人数
bool arr[1000 + 1]; // 用布尔型数组模拟人的编号,标记是否出局
memset(arr, -1, sizeof(arr)); // 数组元素 设置为 true;
cin >> N >> M;
remain = N; // 初始剩余人数为 总人数
while (remain > 0) { // 循环条件为 剩余人数不为零
cnt++; // 计数器始终进行
if (arr[cnt]) { // 判断当前计数器所在数组元素是否已出局
check++; // 若未出局,则检查器加一
}
if (check == M) { // 当检查器数到报数数时
arr[cnt] = false; // 设置当前编号(数组元素)出局
cout << cnt << " "; // 输出当前数组元素的索引
check = 0; // 检查器置零
remain--; // 剩余人数减一
}
if (cnt == N) { // 当计数器运行到末尾时
cnt = 0; // 计数器置零,实现转圈效果
}
}
return 0;
}
[Copy](javascript:;)
二维数组的基本操作
#include <bits/stdc++.h>
using namespace std;
int arr[3 + 1][4 + 1];
int main() {
// 二维数组的基本操作
// define
int n, m;
// input : 批量读入
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> arr[i][j];
}
}
// process : 批量处理
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
arr[i][j] += 100;
}
}
// output : 批量输出
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
return 0;
}
[Copy](javascript:;)
字符数组的基本操作
#include <bits/stdc++.h>
using namespace std;
char arr[1000];
char arr1[1000];
char arr2[1000];
int main() {
// 1、字符数组的基本操作
// memset(arr, 0, sizeof(0)); // 定义局部变量数组,务必置零
// cin >> arr; // cin 输入字符数组,遇空格或回车停止
cin.getline(arr, sizeof(arr)); // cin.getline 整行输入字符数组,遇回车停止
// arr[5] = '\0'; // 字符串 遇 '\0' 结束
cout << arr << endl; // 输出 字符数组
cout << "strlen = " << strlen(arr) << endl; // 字符数组的有效长度
cout << "sizeof = " << sizeof(arr) << endl; // 字符数组的定义长度
// 逐个字符 批量数组字符数组
for (int i = 0; i < strlen(arr); i++) {
cout << arr[i] << " ";
}
cout << endl;
// 2、字符 常用处理函数
char c;
cin >> c;
cout << "isdigit : " << isdigit(c) << endl; // 判断数字字符
cout << "isalpha : " << isalpha(c)
<< endl; // 1 大写字母, 2 小写字母, 0 不是字母
cout << "isalnum : " << isalnum(c) << endl; // 判断 字母 或 数字
cout << "islower : " << islower(c) << endl; // 判断 小写字母
cout << "isupper : " << isupper(c) << endl; // 判断 大写字母
cout << "tolower : " << char(tolower(c)) << endl; // 转换 小写字母
cout << "toupper : " << char(toupper(c)) << endl; // 转换 大写字母
/*
测试输出:
E
isdigit : 0
isalpha : 1
isalnum : 1
islower : 0
isupper : 1
tolower : e
toupper : E
*/
// 3、字符数组 常用处理函数
// (1) strcat 字符数组 拼接
cin >> arr1 >> arr2;
strcat(arr1, arr2); // 后面数组 拼接到 前面数组
cout << arr1 << endl;
cout << arr2 << endl;
// 拼接到 第三方字符数组
strcat(arr, arr1);
strcat(arr, arr2);
cout << arr << endl;
// (2) strcpy 字符串复制
cin >> arr1 >> arr2;
strcpy(arr1, arr2); // 后面数组 复制到 前面数组
cout << arr1 << endl;
cout << arr2 << endl;
// (3) strcmp 字符串比较 :1 大于 , 0 等于 , -1 小于
cin >> arr1 >> arr2;
cout << strcmp(arr1, arr2)
<< endl; // 逐位比较 ASCII : 1 大于 , 0 等于 , -1 小于
return 0;
}
[Copy](javascript:;)
string 类的基本用法
#include <bits/stdc++.h>
using namespace std;
int main() {
string s; // vector<char> s
cin >> s;
getline(cin, s);
cout << s << endl;
for (char c : s) {
cout << c;
}
cout << endl;
cout << s.size() << endl;
s.push_back('S');
cout << s << endl;
s.pop_back();
cout << s << endl;
cout << s.front() << endl;
cout << s.back() << endl;
s += "OK";
cout << s << endl;
sort(s.begin(), s.end());
cout << s << endl;
reverse(s.begin(), s.end());
cout << s << endl;
s.insert(s.begin() + 2, 'K'); // 插入字符,注意用迭代器定位
cout << s << endl;
s.erase(s.begin() + 3);
cout << s << endl;
s.clear(); // 清空
cout << "size = " << s.size() << endl;
cout << "empty = " << s.empty() << endl; // 判断是否为空
cout << s.substr(6, 5) << endl; // 返回子串,注意用下标定位
int n = 12345;
string s = to_string(n); // 整型转字符串
cout << s << endl;
double db = 12.345;
s = to_string(db); // 浮点型转字符串
cout << s << endl;
s.pop_back();
s.pop_back();
s.pop_back();
s.erase(s.begin() + 3, s.begin() + 3 + 3); // 删除
cout << s << endl;
string s = "12";
int n = stoi(s); // 字符串转整型,注意 atoi 已失效
cout << n * n << endl;
string s = "12.345";
double db = stod(s); // 字符串转浮点型
cout << db << endl;
string s = "hello world"; // llo -> 110
for (int i = 0; i < s.size(); i++) {
if (s[i] == 'w') {
cout << "find" << endl;
break;
}
}
// Box::get()
if (s.find("werewrt") == string::npos) { // 查找
cout << "not find" << endl;
} else {
cout << "find" << endl;
}
s.erase(s.begin() + 2, s.begin() + 2 + 3); // 删除
cout << s << endl;
s.insert(2, "110"); // 插入字符串,注意用下标索引
cout << s << endl;
s.replace(2, 3, "110110"); // 替换
cout << s << endl;
// 字符数组转换 string类
char arr[6] = {'h', 'e', 'l', 'l', 'o', '\0'};
string s(arr);
string s = arr;
cout << s << endl;
// string 类转换 字符数组
string s = "hello world";
char arr[100];
memset(arr, 0, sizeof(arr));
strcpy(arr, s.c_str()); // 返回以'\0'结尾的字符数组,优先使用
strcpy(arr, s.data()); // 返回 字符数组,不添加 '\0'
cout << arr << endl;
return 0;
}
[Copy](javascript:;)
vector 向量的基本用法
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> vec(n);
for (int i = 0; i < n; i++) {
cin >> vec[i];
}
sort(vec.begin(), vec.end());
vec.insert(vec.begin() + 2, 1000);
vector<int> vec1 = {10, 20, 30, 40, 50};
vector<int> vec2 = {100, 200, 300};
for (auto num : vec1) {
cout << num << " ";
}
cout << endl;
cout << vec1.size() << endl;
for (auto num : vec2) {
cout << num << " ";
}
cout << endl;
cout << vec2.size() << endl;
vec1.clear();
vec.erase(vec.begin() + 3); // 删除,迭代器定位
vec.erase(find(vec.begin(), vec.end(), 30)); // 查找位置并删除
for (int i = 0; i < n; i++) {
cout << vec[i] << " ";
}
// 数组转换成向量
int arr[] = {10, 20, 50, 40, 30};
vector<int> vec(arr, arr + 5);
for (auto num : vec) {
cout << num << " ";
}
cout << endl;
vec.push_back(100); // 添加末尾元素
for (auto num : vec) {
cout << num << " ";
}
cout << endl;
vec.pop_back(); // 删除末尾元素
sort(vec.begin(), vec.end()); // 排序
for (auto num : vec) {
cout << num << " ";
}
cout << endl;
reverse(vec.begin(), vec.end()); // 反转
for (auto num : vec) {
cout << num << " ";
}
cout << endl;
cout << vec.front() << endl; // 起始元素
cout << vec.back() << endl; // 末尾元素
vec.clear(); // 清空
cout << "size = " << vec.size() << endl; // 返回个数
cout << "empty = " << vec.empty() << endl; // 判断为空
// 向量转换为数组
vector<int> vec = {10, 20, 30, 40, 50};
int arr[10];
memcpy(arr, &vec[0], vec.size() * sizeof(int));
for (int i = 0; i < vec.size(); i++) {
cout << arr[i] << " ";
}
// TDP1104 读取方式
int num;
vector<int> nums;
while (cin >> num)
nums.push_back(num);
// TDP1109 读取方式
int num, target;
vector<int> nums;
while (cin >> num) {
nums.push_back(num);
char ch = getchar();
if (ch == '\n')
break;
}
cin >> target;
// 二维向量用法
vector<vector<int>> vecvec;
vector<int> vec = {10, 20, 30};
vecvec.push_back(vec);
vecvec.push_back(vec);
vecvec.push_back(vec);
for (auto vec : vecvec) {
for (auto num : vec) {
cout << num << " ";
}
cout << endl;
}
return 0;
}
[Copy](javascript:;)
- 85 次查看
- 取消收藏
- 举报
2 条评论
-
-
鞠宪庆 (juxianqing) LV 6 @ 3 个月前
[ ](javascript:;)[](javascript:;)
#include <bits/stdc++.h> using namespace std; int main() { // 3、循环结构 // (1) 前置条件循环 while (n > 100) {...} if () {...} else {...} // (2) 后置条件循环 do {...} while (); // (3) 计数循环 for (;;) {...} // (4) 循环嵌套 for (;;) { for (;;) {} } // (1) 前置条件循环 while (n > 100) {...} if () {...} else {...} // 我要上 9 重天 int i = 1; // (1) 循环变量初始化 while (i <= 9) { // (2) 循环条件 cout << "我要上 " << i << " 重天" << endl; // (3) 循环体 i++; // i = i + 1 // (4) 循环变量渐变 } // 你要入地 18 ,17, 1 int j = 18; while (j >= 1) { cout << "我要入 " << j << " 地" << endl; j = j - 1; //j--; } // 1 + 2 + 3 + ... + 100 = ?; int k = 1; int sum = 0; while (k <= 100) { sum = sum + k; k++; } cout << sum << endl; // (3) 计数循环 for (;;) {...} for (int i = 1; i <= 9; i++) { cout << "我要上 " << i << " 重天" << endl; } for (int j = 18; j >= 1; j--) { cout << "我要入 " << j << " 层地" << endl; } int n = 100; int sum = 0; for (int k = 1; k <= n; k++) { sum = sum + k; } cout << sum << endl; if (Chinese >= 60 && math < 60 || Chinese < 60 && math >= 60 ) return 0; }
[Copy](javascript:;)
-
鞠宪庆 (juxianqing) LV 6 @ 3 个月前
[ ](javascript:;)[](javascript:;)
int a = 10; int b = 3; double c = double(a) / b; // 整数转换浮点数 double c = 1.0 * a / b; // 整数转换浮点数 double c = a / b * 1.0; // 无效用法 cout << c << endl; cout << fixed << setprecision(3) << c << endl; int i = 1; i = i + 1; cout << i << endl; i += 1; cout << i << endl; i++; cout << i << endl; ++i; cout << i << endl;