为什么使用数组

  1. int a, b, c, d;
  2. int a1, a2, a3, a4, a5, a6, a7....;
  3. int a[100]; //定义100个int类型
  4. a[0], a[1], a[2], a[3], a[4]...
  5. [0][1][2][3][4][5][6][7]...
  6. //连续地址空间存储的
  7. //静态数组,大小是静态固定的,上来就要告诉计算机,我需要100个连续的int空间
  8. //然后,我想用几个就用几个。但是,千万不能要超。
  9. //a[0] ... a[99],这些是100个,但不能用a[100],这是第101个
  10. //因为,a[101]这个位置,计算机可能存储了其他的东西,有可能是危险的东西(越界访问)

一维数组

  1. #include <iostream>
  2. using namespace std;
  3. int a[10]; //全局变量,初始值默认是0
  4. int n;
  5. int main()
  6. {
  7. int b[10]; //局部变量,初始值是计算机随机给的
  8. cin >> n;
  9. for (int i = 0; i < n; i++) cin >> a[i];
  10. for (int i = 0; i < n; i++) cout << a[i] << ' ';
  11. cout << '\n';
  12. return 0;
  13. }
  1. #include <iostream>
  2. #include <cstdio>
  3. using namespace std;
  4. const int N = 1e5 + 10;
  5. int a[N], b[N];
  6. int n;
  7. int main()
  8. {
  9. cin >> n;
  10. for (int i = 0; i < n; i++) cin >> a[i];
  11. for (int i = 0; i < n; i++) cout << a[i] << ' ';
  12. puts("");
  13. return 0;
  14. }
  1. int a[5] = {1, 2, 3, 4, 5};
  2. a[-1]; //这是不对的,下标不能是负数
  3. memset(a, 0, sizeof a); //把a[i]全部置成0

二维数组

  1. #include <iostream>
  2. using namespace std;
  3. const int N = 110;
  4. int a[N][N];
  5. int n, m; //n行m列
  6. int main()
  7. {
  8. cin >> n >> m;
  9. for (int i = 0; i < n; i++)
  10. for (int j = 0; j < n; j++)
  11. cin >> a[i][j];
  12. //逐行逐列扫描
  13. for (int i = 0; i < n; i++){
  14. for (int j = 0; j < n; j++) cout << a[i][j] << ' ';
  15. puts("");
  16. }
  17. return 0;
  18. }

字符类型和字符数组

  1. // 字符类型
  2. char c;
  3. int a[N];
  4. double a[N];
  5. bool a[N];
  6. // 字符数组
  7. char s[N];
  8. char s[N][N];
  9. /*
  10. .#...#
  11. ...#..
  12. #.....
  13. */
  14. // 界定符
  15. 'a' 字符 ' '
  16. "a" 字符串 "abc" "" " "
  17. a[0] = '\n';
  18. cout << a[0];
  19. #include <bits/stdc++.h>
  20. using namespace std;
  21. int main()
  22. {
  23. cout << sizeof ('a') << '\n';
  24. cout << sizeof ("a") << '\n';
  25. return 0;
  26. }
  27. /*
  28. 1
  29. 2
  30. */
  31. 字符占一个字节,“a”会增加一个字节,用来存放字符串结束符'\0',所以占2字节
  1. // 字符串读入
  2. #include <cstdio>
  3. using namespace std;
  4. char s[110];
  5. int main()
  6. {
  7. scanf("%s", s);
  8. printf("%s\n", s);
  9. return 0;
  10. }
  1. // string类型
  2. #include <iostream>
  3. using namespace std;
  4. int main()
  5. {
  6. string s;
  7. cin >> s;
  8. cout << s << '\n';
  9. return 0;
  10. }
  1. // 读入一整行
  2. // ab cd ed
  3. #include <iostream>
  4. using namespace std;
  5. int main()
  6. {
  7. string s;
  8. getline(cin, s);
  9. cout << s << '\n';
  10. return 0;
  11. }
  1. // 字符串处理函数
  2. #include <iostream>
  3. #include <cstring>
  4. using namespace std;
  5. int main()
  6. {
  7. string s;
  8. s = "abcdef";
  9. cout << s << '\n';
  10. cout << s.size() << '\n'; //输出字符串大小
  11. cout << s.substr(2) << '\n'; //截取子串,是位置2开始往后所有
  12. cout << s.substr(2, 3) << '\n'; //从位置2开始,截取三位
  13. return 0;
  14. }
  1. /*
  2. 二维字符数组
  3. .....#
  4. ####.#
  5. ###..#
  6. ##.#..
  7. */
  8. #include <iostream>
  9. #include <cstdio>
  10. using namespace std;
  11. const int N = 110;
  12. char s[N][N];
  13. int n, m;
  14. int main()
  15. {
  16. cin >> n >> m;
  17. for (int i = 0; i < n; i++) scanf("%s", s[i]);
  18. for (int i = 0; i < n; i++) printf("%s\n", s[i]);
  19. return 0;
  20. }

image.png
image.png
image.png

空间复杂度

image.png
image.png

一本通习题

一维数组

与指定数字相同的数的个数

  1. //

陶陶摘苹果

  1. //

计算书费

  1. //

数组逆序重存放

  1. //

年龄与疾病

  1. //

校门外的树

  1. //原题

向量点积计算

  1. //

开关灯

  1. //

查找特定的值

  1. //

不高兴的津津

  1. //原题

最大值和最小值的差

  1. //打擂台

不与最大数相同的数字之和

  1. //

白细胞计数

  1. //

直方图

  1. //

最长平台

  1. //

整数去重

  1. //

铺地毯

  1. //

二维数组

矩阵交换行

  1. //

同行列对角线的格

  1. //

计算矩阵边缘元素之和

  1. //

计算鞍点

  1. //

图像相似度

  1. //

矩阵加法

  1. //

矩阵乘法

  1. //

矩阵转置

  1. //

图像旋转

  1. //

图像模糊处理

  1. //

字符数组

统计数字字符个数

  1. //

找第一个只出现一次的字符

  1. //

基因相关性

  1. //

石头剪子布

  1. //

输出亲朋字符串

  1. //

合法C标识符查

  1. //

配对碱基链

  1. //

密码翻译

  1. //

加密的病历单

  1. //

将字符串中的小写字母转换成大写字母

  1. //

整理药名

  1. //

验证子串

  1. //string s;
  2. //s.find()

删除单词后缀

  1. //string s;
  2. //s.erase(len - 2)
  3. //使用char s[40];
  4. //匹配后缀成功后,利用s[len-2] = '\0'进行截断

单词的长度

  1. //getline(cin, s);
  2. //然后遍历,碰到空格算一个单词
  3. //要注意输出的格式
  4. //使用while (cin >> s),更好编写
  5. //不过要注意一下格式控制的小技巧
  6. //本地调试要使用freopen
  7. //输出格式如下
  8. 3,3,4,2,10,3,4,7,5

最长最短单词

  1. //

单词翻转

  1. //getline(cin, s);
  2. //比较麻烦,构造一个一个单词出来,然后翻转
  3. //使用while (cin >> s)
  4. //会两个点,格式错误,调不出来

字符串p型编码

  1. //

判断字符串是否为回文

  1. //

最高分数的学生姓名

  1. //

连续出现的字符

  1. //

最长单词2

  1. //