202111051401基本数据类型.mp4

大纲要求

•【1】整数型:int, long long

•【1】实数型:float, double

•【1】字符型:char

•【1】布尔型:bool


变量名称

  1. // n个数
  2. int n;
  3. // 一个数
  4. int n;
  5. int x;
  6. // 两个数
  7. int a, b;
  8. int x, y;
  9. // 求和
  10. int sum;
  11. // 计数
  12. int cnt;
  13. // 写循环, i, j, k
  14. for (int i = 0; i < n; i++)
  15. for (int j = 0; j < m; j++)
  16. for (int k = 0; k < p; k++)
  17. // 如果有更多层的循环,i1, i2, i3, i4
  18. // 开数组
  19. const int N = 110;
  20. int a[N];
  21. const int N = 1e5 + 10;
  22. int a[N];
  23. // n行,m列的矩阵
  24. int n, m;
  25. // 坐标
  26. int x, y;
  27. // 和生活中的一些事物,有关联
  28. // 钱,时间,距离
  29. int money, int tim, int dis
  30. int d;
  31. // 临时变量,存一些中间信息
  32. int temp;
  33. int t;
  34. // 有n组数据
  35. int T;
  36. while (T--){
  37. }
  38. // 奥义
  39. // 英文单词简写(缩写)

顺序程序设计 第一节 赋值语句

变量(盛饭的碗,一个有名字的碗,一个空的碗,可以盛上饭,可以盛不同的饭,就是赋值不同的数值)

  1. int a;
  2. a = 5;
  3. int b = 2;
  4. int c, d, e, f;

顺序程序设计 第二节 运算符和表达式

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int a = 1, b = 2;
  6. int c;
  7. c = a + b;
  8. c = a - b;
  9. c = a * b;
  10. c = a / b; // 整数除法,自动向下取整
  11. // %,模运算
  12. c = a % b; // 9除2,商4,余1, 模运算,就是取余运算,c等于1
  13. // 注意了
  14. int a = -5;
  15. cout << a % 2 << '\n'; // 输出 -1
  16. int a = -5;
  17. cout << a / 2 << '\n'; // 输出 -2,负数/2,向0取整
  18. int a = -5;
  19. cout << (a >> 1) << '\n'; // 输出 -3,负数右移一位,向负方向取整
  20. return 0;
  21. }
  1. // 【扩展】
  2. // 涉及负数的二进制表示,原码反码补码问题
  3. // 教给大家一个自己玩的小东西,有了这个,就可以自己去验证原码反码补码的变化过程
  4. // 进而,从根源上理解,而不是死记硬背
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7. void print(int x){
  8. for (int j = 31; j >= 0; j--)
  9. if (x >> j & 1) cout << 1;
  10. else cout << 0;
  11. puts("");
  12. }
  13. int main(){
  14. int x = -5;
  15. print(x);
  16. x >>= 1;
  17. print(x);
  18. return 0;
  19. }
  1. // ++i, i++ 的区别
  2. // ++i是,i先增加1位,赋值给别人
  3. // i++是,i先赋值给别人,自己再增加1位
  4. i++; // i从1变到了2
  5. i--; // i从2变到了1
  6. ++i;
  7. --i;
  8. a += 2; a -= 2;
  9. a += b; a -= b; // a = a + b;
  10. int a = 2;
  11. int b = a++;
  12. //此时,b = 2, a = 3;
  13. int a = 2;
  14. int b = ++a;
  15. //此时,b = 3, a = 3;
  16. if (a == b){ //判断a和b相等
  17. c = a + b;
  18. }
  19. if (a = b){
  20. cout << '=' << '\n';
  21. }
  22. else{
  23. cout << "!=" << '\n';
  24. }
  25. /*
  26. 测试数据
  27. 3 3
  28. 3 0
  29. 0 0
  30. 0 3
  31. */
  32. // 一些大小关系的示例
  33. if (a < b){...}
  34. if (a <= b){...}
  35. if (a != b){...}
  36. if (a > b && a > c){...}
  37. if (a > b || a > c){...}
  38. if (!a){...} //对a取反,当a是0的时候,成立;当a非零的时候,不成立
  39. //示例
  40. int a = 0;
  41. if (!a) cout << a << '\n';
  42. // 运算的简写,包括 += -= *= /= %=
  43. a += 5;
  44. a -= 5;
  45. // 前导零问题
  46. cout << 00123 << '\n';
  1. // 一些常用的数据函数
  2. #include <iostream>
  3. #include <cmath>
  4. using namespace std;
  5. int main()
  6. {
  7. int x = -2;
  8. cout << abs(x) << '\n'; //绝对值,整数的取绝对值
  9. double y = 1.2;
  10. cout << floor(y) << '\n'; //向下取整
  11. cout << ceil(y) << '\n'; //向上取整 (x + y - 1) / y
  12. cout << fabs(y) << '\n'; //浮点数绝对值
  13. cout << round(y) << '\n'; //四舍五入
  14. double x = -3.2;
  15. cout << round(x) << '\n';
  16. int n = 16;
  17. cout << sqrt(n) << '\n'; //开平方根,开根号 4*4=16 sqrt(16)=4
  18. cout << pow(2, 3) << '\n'; //2^3=8
  19. return 0;
  20. }

例题,CSP2021-J1阅读程序第1题【拓展】

  1. // 【拓展】
  2. // 题目大意
  3. // char table[0] = 0xff;
  4. // cout << int(table[0]) << endl;
  5. // 问,输出的是不是-1
  6. #include <bits/stdc++.h>
  7. using namespace std;
  8. void print(int x){
  9. for (int j = 31; j >= 0; j--)
  10. if (x >> j & 1) cout << 1;
  11. else cout << 0;
  12. puts("");
  13. }
  14. int main(){
  15. unsigned char x = 0xff;
  16. char y = 0xff;
  17. cout << (int)x << '\n';
  18. print(x);
  19. cout << (int)y << '\n';
  20. print(y);
  21. return 0;
  22. }

顺序程序设计 第三节 常量和变量

  1. const int N = 110;
  2. const double PII = 3.14159;

顺序程序设计 第四节 标准数据类型

  1. // 以下几个单词,要记笔记
  2. int, 整型,用来存整数
  3. double, 双精度浮点型,用来存小数/实数
  4. char, 字符类型,用来存字符
  5. bool, 布尔类型,用来存true/false
  6. long long, 长整型,用来存爆int的数,1e18这个量级的
  7. string, string类,用来存字符串的
  8. // 其他不常用的:
  9. short, 短整型
  10. float, 单精度浮点数
  1. int,占4字节(Byte),取值范围 [-2^31, 2^31-1], 取值范围 [-2147483648, 2147483647]
  2. int n = (1ll << 31) - 1;
  3. cout << n << '\n';
  4. n++;
  5. cout << n << '\n';
  6. // 示例
  7. #include <bits/stdc++.h>
  8. using namespace std;
  9. typedef long long ll;
  10. int main()
  11. {
  12. int n = INT_MAX, m = INT_MIN;
  13. cout << n << '\n' << m << '\n';
  14. return 0;
  15. }
  1. long long 取值范围[-9223372036854775808, 9223372036854775807]
  2. 1e18级别的
  3. // 示例
  4. #include <bits/stdc++.h>
  5. using namespace std;
  6. typedef long long ll;
  7. int main()
  8. {
  9. ll n = LLONG_MAX, m = LLONG_MIN;
  10. cout << n << '\n' << m << '\n';
  11. return 0;
  12. }
  1. // 关于占用存储空间的大小
  2. // 各个类型的变量长度由编译器来决定(实际上与操作系统位数和编译器都有关)
  3. // 使用时可用sizeof()得到
  4. // 当前主流编译器一般是32位或64位
  5. 类型      32位  64
  6. char      1    1
  7. char* 4 8
  8. short int   2    2
  9. int       4    4
  10. unsigned int 4 4
  11. long    4    8
  12. long long 8    8
  13. float      4    4
  14. double      8    8
  15. unsigned long 4 8
  1. // 区间的符号
  2. // 区间 [1,5] 1,2,3,4,5 闭区间
  3. // 区间 (1,5) 2,3,4
  4. // 区间 (1,5] 2,3,4,5
  5. // 指数2^3 = 8
  6. // 单位换算
  7. 1 Byte 8 bit
  8. 1024 Byte 1 KB
  9. 1024 KB 1 MB
  10. 1024 MB 1 GB
  11. 1024 GB 1 TB
  12. // 无符号整型
  13. unsigned int 无符号整型 4字节 [0, 4294967295]
  14. unsigned long long 8字节
  15. cout << (1 << 32) - 1 << '\n'; // 注意warning提示
  16. cout << (1ll << 32) - 1 << '\n';
  17. unsigned int n = (1ll << 33) - 1;
  18. n++;
  19. cout << n << '\n';
  20. // 关于有效位数的区分
  21. float 单精度浮点数 4字节 有效位数 6~7
  22. double 双精度浮点数 8字节 有效位数 15~16
  23. // 换行的操作
  24. cout << '\n'; 转义字符 换行/回车
  25. cout << endl;
  26. puts("");
  1. // 输出占用存储空间的大小
  2. #include <iostream>
  3. using namespace std;
  4. int main()
  5. {
  6. cout << sizeof(int) << '\n';
  7. return 0;
  8. }
  1. // 保留小数点后多少位的写法
  2. // 有效位数的区别
  3. #include <iostream>
  4. using namespace std;
  5. int main()
  6. {
  7. double x = 1.0 / 3;
  8. printf("%.40lf\n", x);
  9. return 0;
  10. }
  11. // 0.3333333333333333148296162562473909929395
  1. // 头文件的使用
  2. 规则是需要使用什么头文件,就写什么头文件
  3. 在使用DEV C++的时候,软件会默认带上一些头文件,这对新手来说,不是什么好事情
  4. 基本的,你需要记住的是
  5. cin cout #include <iostream>
  6. scanf printf #include <cstdio>
  7. floor()这种函数 #include <cmath>
  8. sort()这个方法 #include <algorithm>
  9. // 万能头,是我们把语言部分比较熟练了之后,开始使用的,前期不应该使用,但要认识
  10. #include <bits/stdc++.h>

例题,甲流疫情死亡率

  1. // 可能会在理解题意上出现stuck
  2. #include <cstdio>
  3. using namespace std;
  4. int main()
  5. {
  6. int a, b;
  7. scanf("%d%d", &a, &b);
  8. printf("%.3lf%%\n", 1.0 * b / a * 100);
  9. return 0;
  10. }

例题, 计算多项式的值

  1. // 多项式应该是一个超前知识点
  2. // 需要认识一下 x^3 是个啥
  3. #include <cstdio>
  4. using namespace std;
  5. int main()
  6. {
  7. double x, a, b, c, d;
  8. scanf("%lf%lf%lf%lf%lf", &x, &a, &b, &c, &d);
  9. double ans = a * x * x * x + b * x * x + c * x + d;
  10. printf("%.7lf\n", ans);
  11. return 0;
  12. }

例题,计算并联电阻的阻值

  1. // 背景知识:物理,并联串联电阻
  2. // 适应一下这种题面,“新定义”
  3. #include <cstdio>
  4. using namespace std;
  5. int main()
  6. {
  7. double r1, r2;
  8. scanf("%lf%lf", &r1, &r2);
  9. double r = 1.0 / (1 / r1 + 1 / r2);
  10. printf("%.2lf\n", r);
  11. return 0;
  12. }

例题,整型数据类型存储空间大小

  1. // sizeof
  2. // 输出占用存储空间大小

例题,打印ASCII码

  1. // 百度ascii
  2. // 学会0-9 A-Z a-z
  3. // 学会大小写转化,字符和数字转化
  4. #include <bits/stdc++.h>
  5. using namespace std;
  6. int main()
  7. {
  8. char c;
  9. scanf("%c", &c);
  10. printf("%d\n", c);
  11. return 0;
  12. }

例题,计算浮点数相除的余

  1. // 理解一下余数的定义

例题,计算球的体积

  1. // v = 4/3 * PII * r^3
  2. // 需要注意整数除整数的问题

例题,反向输出一个三位数

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int x;
  6. cin >> x;
  7. cout << x % 10 << x / 10 % 10 << x / 100 << '\n';
  8. return 0;
  9. }
  10. // 拓展:给定一个数n,翻转得到新的数字n`,输出 n + n` 的值

例题,大象喝水查

  1. // 这道题PII取3.14即可
  2. // 注意问题的实际背景,需要整桶整桶的水
  3. // 如果不知道圆柱体的体积,可以百度