顺序结构程序设计

第一节 赋值语句

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

  1. int a;
  2. a = 5;
  3. a = 6;
  4. a = 7;
  5. int b = 2;
  6. 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. a++; // a从1变到了2
  21. a--; // a从2变到了1
  22. ++a;
  23. --a;
  24. a += 2; a -= 2;
  25. a += b; a -= b; // a = a + b;
  26. int a = 2;
  27. int b = a++;
  28. //此时,b = 2, a = 3;
  29. int a = 2;
  30. int b = ++a;
  31. //此时,b = 3, a = 3;
  32. if (a == b){ //判断a和b相等
  33. c = a + b;
  34. }
  35. int a = 1;
  36. if (a = -1){
  37. cout << "被执行了" << '\n';
  38. }
  39. int a = 1;
  40. if (a = 0){
  41. cout << "被执行了" << '\n';
  42. }
  43. //这条不会被执行
  44. if (a < b){
  45. }
  46. if (a <= b){
  47. }
  48. if (a != b){
  49. }
  50. if (a > b && a > c){
  51. }
  52. if (a > b || a > c){
  53. }
  54. if (!a){ //对a取反,当a是0的时候,成立;当a非零的时候,不成立
  55. }
  56. //示例
  57. int a = 0;
  58. if (!a){
  59. cout << a << '\n';
  60. }
  61. // 运算的简写,包括 += -= *= /= %=
  62. a = a + 5;
  63. a += 5;
  64. a = a - 5;
  65. a -= 5;
  66. a++;
  67. ++a;
  68. a += 1;
  69. return 0;
  70. }
  71. // ++i, i++ 的区别
  72. // ++i是,i先增加1位,赋值给别人
  73. // i++是,i先赋值给别人,自己再增加1位
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4. int main()
  5. {
  6. int x = -2;
  7. cout << abs(x) << '\n'; //绝对值,整数的取绝对值
  8. double y = 1.2;
  9. cout << floor(y) << '\n'; //向下取整
  10. cout << ceil(y) << '\n'; //向上取整 (x + y - 1) / y
  11. cout << fabs(y) << '\n'; //浮点数绝对值
  12. cout << round(y) << '\n'; //四舍五入
  13. int n = 16;
  14. cout << sqrt(n) << '\n'; //开平方根,开根号 4*4=16 sqrt(16)=4
  15. cout << pow(2, 3) << '\n'; //2^3=8
  16. return 0;
  17. }

第三节 常量和变量

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

第四节 标准数据类型

  1. short 短整型 2字节
  2. int 整型 4字节 Byte
  3. 数值范围 [-2^31, 2^31-1]
  4. /*
  5. int n = (1 << 31) - 1;
  6. n++:
  7. cout << n << '\n';
  8. */
  9. // 区间 [1,5] 1,2,3,4,5 闭区间
  10. // 区间 (1,5) 2,3,4
  11. // 区间 (1,5] 2,3,4,5
  12. // 指数2^3 = 8
  13. 1 Byte 8 bit
  14. 1024 Byte 1 KB
  15. 1024 KB 1 MB
  16. 1024 MB 1 GB
  17. 1024 G 1 TB
  18. long long 超长整型 8字节
  19. unsigned int 无符号整型 4字节
  20. [0, 4294967295]
  21. /*
  22. unsigned int n = (1ll << 33) - 1;
  23. n++;
  24. cout << n << '\n';
  25. */
  26. unsigned long long 8字节
  27. float 单精度浮点数 4字节 有效位数 6~7
  28. double 双精度浮点数 8字节 有效位数 15~16
  29. bool 布尔类型 1字节 true false
  30. char 字符类型
  31. \n 转义字符 换行
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. cout << sizeof(unsigned int) << '\n';
  6. return 0;
  7. }
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. double x = 1.0 / 3;
  6. printf("%.40lf\n", x);
  7. return 0;
  8. }
  9. // 0.3333333333333333148296162562473909929395
  10. // 计算机实验

第五节 数据输入输出

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int x;
  6. cin >> x;
  7. cout << x;
  8. return 0;
  9. }
  1. #include <cstdio>
  2. using namespace std;
  3. int main()
  4. {
  5. int x;
  6. scanf("%d", &x);
  7. printf("%d\n", x);
  8. printf("%10d\n", x);
  9. printf("%-10d\n", x);
  10. double y;
  11. scanf("%lf", &y);
  12. printf("%lf\n", y);
  13. printf("%.2lf\n", y); //小数点后保留2位
  14. return 0;
  15. }
  1. #include <iostream>
  2. #include <cstdio>
  3. using namespace std;
  4. int main()
  5. {
  6. char c;
  7. //cin >> c;
  8. //cout << c << '\n';
  9. // a b c A B C * - 1 0
  10. scanf("%c", &c);
  11. printf("%c\n", c);
  12. return 0;
  13. }
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. char c;
  4. string s1;
  5. char s2[110];
  6. char s3[110][110];
  7. int main(){
  8. // 以字符为例
  9. cin >> c;
  10. cout << c << '\n';
  11. // 读入string类
  12. cin >> s1;
  13. cout << s1 << '\n';
  14. // 读入字符数组
  15. scanf("%s", s2);
  16. printf("%s\n", s2);
  17. // 读入二维字符数组
  18. int n;
  19. cin >> n;
  20. for (int i = 0; i < n; i++) scanf("%s", s3[i]);
  21. for (int i = 0; i < n; i++) printf("%s\n", s3[i]);
  22. // int类型 占多少个字符宽度
  23. cin >> n;
  24. printf("%8d %8d\n", n, n);
  25. printf("%-8d %-8d\n", n, n);
  26. // double 类型保留小数点后几位
  27. printf("%.20lf\n", 1.0 / 3);
  28. double x = 3.1415926;
  29. cout << x << '\n';
  30. cout << setprecision(5) << x << '\n';
  31. cout << setprecision(4) << x << '\n';
  32. cout << setprecision(3) << x << '\n';
  33. cout << setprecision(2) << x << '\n'; // 3.1
  34. cout << setprecision(1) << x << '\n'; // 3
  35. // setprecision 不计算小数点
  36. // #include <iomanip>
  37. // 与 setw 字段宽度不同的是
  38. // setprecision 的精度设置将保持有效,直到更改为其他值为止
  39. // 流操作符 setw 可用于建立指定宽度的打印区域
  40. // 空格“填充”在前面,所以它被认为是右对齐的
  41. // 左对齐也能搞,麻烦
  42. // 用一回,写一回
  43. int y;
  44. cin >> y;
  45. cout << setw(8) << y << setw(8) << y << '\n';
  46. return 0;
  47. }

关于scanf

image.png
注意看,这个参数是 point to already allocated objects,指向了一个已经存在的对象

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main(){
  4. int x;
  5. scanf("%d", &x); // &取地址符号
  6. printf("%d\n", x);
  7. return 0;
  8. }
  9. /*
  10. 输入:123
  11. 输出:123
  12. */
  13. #include <bits/stdc++.h>
  14. using namespace std;
  15. int main(){
  16. int x;
  17. scanf("%d", x);
  18. printf("%d\n", x);
  19. return 0;
  20. }
  21. // 编译报错
  22. std03.cpp:7:14: warning: format specifies type 'int *' but the argument has type 'int'
  23. [-Wformat]
  24. scanf("%d", x);
  25. ~~ ^
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main(){
  4. int x;
  5. int *px = &x;
  6. scanf("%d", px);
  7. printf("%d\n", x);
  8. return 0;
  9. }
  10. /*
  11. 输入:123
  12. 输出:123
  13. */
  14. #include <bits/stdc++.h>
  15. using namespace std;
  16. int main(){
  17. int x;
  18. int *px = &x;
  19. scanf("%d", *px); //如果这样写
  20. printf("%d\n", x);
  21. return 0;
  22. }
  23. //编译报错
  24. std03.cpp:8:14: warning: format specifies type 'int *' but the argument has type 'int'
  25. [-Wformat]
  26. scanf("%d", *px);
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int a[10];
  4. int main(){
  5. for (int i = 0; i < 5; i++)
  6. scanf("%d", a + i);
  7. //scanf("%d", &a[i]); //这两句话作用是一样的
  8. return 0;
  9. }
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. char s[10]; //读入字符串的时候
  4. int main(){
  5. scanf("%s", s); //这个时候,写的是数组名称,数组名本身就是一个指针
  6. return 0;
  7. }

第六节 顺序结构实例

一本通习题

运算符和表达式

A+B问题

  1. //

计算(a+b)×c的值

  1. //

计算(a+b)×c的值

  1. //

带余除法

  1. //被除数、除数、商、余数
  2. //% 模运算

计算分数的浮点数值

  1. printf("%lf");
  2. printf("%.9lf"); //保留小数点后9位

常量和变量

甲流疫情死亡率

  1. //可能会在理解题意上出现stuck

计算多项式的值

  1. //多项式应该是一个超前知识点
  2. //需要认识一下 x^3 是个啥

温度表达转化

  1. //

与圆相关的计算

  1. //

计算并联电阻的阻值

  1. //背景知识:物理,并联串联电阻

标准数据类型

整型数据类型存储空间大小

  1. sizeof

浮点型数据类型存储空间大小

  1. //

其他数据类型存储空间大小

  1. //

浮点数向零舍入

  1. //上机实验:负的小数取整

打印ASCII码

  1. //百度ascii
  2. //学会0-9 A-Z a-z
  3. //学会大小写转化,字符和数字转化

打印字符

  1. //

整型与布尔型的转换

  1. //

Hello,World!的大小

  1. //

数据输入输出

保留3位小数的浮点数

  1. //

保留12位小数的浮点数

  1. //

空格分隔输出

  1. //

输出浮点数

  1. //

字符菱形

  1. //

顺序结构实例

计算浮点数相除的余

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

计算球的体积

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

反向输出一个三位数

  1. //拓展:给定一个数n,翻转得到新的数字n`,输出 n + n` 的值

大象喝水查

  1. //这道题PII取3.14即可
  2. //注意问题的实际背景,需要整桶整桶的水

计算线段长度

  1. //背景知识:笛卡尔坐标系

计算三角形面积

  1. //海伦公式
  2. p = (a+b+c) / 2;
  3. s = sqrt(p * (p-a) * (p-b) * (p-c));

等差数列末项计算

  1. //等差数列求和公式

A×B问题

  1. long long
  2. printf("%ld", x);
  3. 1ll * a * b

计算2的幂

  1. //幂次,方 的叫法

苹果和虫子

  1. //还是题意,剩余多少完整的苹果
  2. //也要注意苹果很少,时间很长的情况,会是什么结果