顺序结构程序设计
第一节 赋值语句
变量(盛饭的碗,一个有名字的碗,一个空的碗,可以盛上饭,可以盛不同的饭,就是赋值不同的数值)
int a;a = 5;a = 6;a = 7;int b = 2;int c, d, e, f;
第二节 运算符和表达式
#include <iostream>using namespace std;int main(){int a = 1, b = 2;int c;c = a + b;c = a - b;c = a * b;c = a / b; // 整数除法,自动向下取整// %,模运算c = a % b; // 9除2,商4,余1, 模运算,就是取余运算,c等于1// 注意了int a = -5;cout << a % 2 << '\n'; // 输出 -1int a = -5;cout << a / 2 << '\n'; // 输出 -2,负数/2,向0取整int a = -5;cout << (a >> 1) << '\n'; // 输出 -3,负数右移一位,向负方向取整a++; // a从1变到了2a--; // a从2变到了1++a;--a;a += 2; a -= 2;a += b; a -= b; // a = a + b;int a = 2;int b = a++;//此时,b = 2, a = 3;int a = 2;int b = ++a;//此时,b = 3, a = 3;if (a == b){ //判断a和b相等c = a + b;}int a = 1;if (a = -1){cout << "被执行了" << '\n';}int a = 1;if (a = 0){cout << "被执行了" << '\n';}//这条不会被执行if (a < b){}if (a <= b){}if (a != b){}if (a > b && a > c){}if (a > b || a > c){}if (!a){ //对a取反,当a是0的时候,成立;当a非零的时候,不成立}//示例int a = 0;if (!a){cout << a << '\n';}// 运算的简写,包括 += -= *= /= %=a = a + 5;a += 5;a = a - 5;a -= 5;a++;++a;a += 1;return 0;}// ++i, i++ 的区别// ++i是,i先增加1位,赋值给别人// i++是,i先赋值给别人,自己再增加1位
#include <iostream>#include <cmath>using namespace std;int main(){int x = -2;cout << abs(x) << '\n'; //绝对值,整数的取绝对值double y = 1.2;cout << floor(y) << '\n'; //向下取整cout << ceil(y) << '\n'; //向上取整 (x + y - 1) / ycout << fabs(y) << '\n'; //浮点数绝对值cout << round(y) << '\n'; //四舍五入int n = 16;cout << sqrt(n) << '\n'; //开平方根,开根号 4*4=16 sqrt(16)=4cout << pow(2, 3) << '\n'; //2^3=8return 0;}
第三节 常量和变量
const int N = 110;const double PII = 3.14159;
第四节 标准数据类型
short 短整型 2字节int 整型 4字节 Byte数值范围 [-2^31, 2^31-1]/*int n = (1 << 31) - 1;n++:cout << n << '\n';*/// 区间 [1,5] 1,2,3,4,5 闭区间// 区间 (1,5) 2,3,4// 区间 (1,5] 2,3,4,5// 指数2^3 = 81 Byte 8 bit1024 Byte 1 KB1024 KB 1 MB1024 MB 1 GB1024 G 1 TBlong long 超长整型 8字节unsigned int 无符号整型 4字节[0, 4294967295]/*unsigned int n = (1ll << 33) - 1;n++;cout << n << '\n';*/unsigned long long 8字节float 单精度浮点数 4字节 有效位数 6~7位double 双精度浮点数 8字节 有效位数 15~16位bool 布尔类型 1字节 true falsechar 字符类型’\n‘ 转义字符 换行
#include <iostream>using namespace std;int main(){cout << sizeof(unsigned int) << '\n';return 0;}
#include <iostream>using namespace std;int main(){double x = 1.0 / 3;printf("%.40lf\n", x);return 0;}// 0.3333333333333333148296162562473909929395// 计算机实验
第五节 数据输入输出
#include <iostream>using namespace std;int main(){int x;cin >> x;cout << x;return 0;}
#include <cstdio>using namespace std;int main(){int x;scanf("%d", &x);printf("%d\n", x);printf("%10d\n", x);printf("%-10d\n", x);double y;scanf("%lf", &y);printf("%lf\n", y);printf("%.2lf\n", y); //小数点后保留2位return 0;}
#include <iostream>#include <cstdio>using namespace std;int main(){char c;//cin >> c;//cout << c << '\n';// a b c A B C * - 1 0scanf("%c", &c);printf("%c\n", c);return 0;}
#include <bits/stdc++.h>using namespace std;char c;string s1;char s2[110];char s3[110][110];int main(){// 以字符为例cin >> c;cout << c << '\n';// 读入string类cin >> s1;cout << s1 << '\n';// 读入字符数组scanf("%s", s2);printf("%s\n", s2);// 读入二维字符数组int n;cin >> n;for (int i = 0; i < n; i++) scanf("%s", s3[i]);for (int i = 0; i < n; i++) printf("%s\n", s3[i]);// int类型 占多少个字符宽度cin >> n;printf("%8d %8d\n", n, n);printf("%-8d %-8d\n", n, n);// double 类型保留小数点后几位printf("%.20lf\n", 1.0 / 3);double x = 3.1415926;cout << x << '\n';cout << setprecision(5) << x << '\n';cout << setprecision(4) << x << '\n';cout << setprecision(3) << x << '\n';cout << setprecision(2) << x << '\n'; // 3.1cout << setprecision(1) << x << '\n'; // 3// setprecision 不计算小数点// #include <iomanip>// 与 setw 字段宽度不同的是// setprecision 的精度设置将保持有效,直到更改为其他值为止// 流操作符 setw 可用于建立指定宽度的打印区域// 空格“填充”在前面,所以它被认为是右对齐的// 左对齐也能搞,麻烦// 用一回,写一回int y;cin >> y;cout << setw(8) << y << setw(8) << y << '\n';return 0;}
关于scanf

注意看,这个参数是 point to already allocated objects,指向了一个已经存在的对象
#include <bits/stdc++.h>using namespace std;int main(){int x;scanf("%d", &x); // &取地址符号printf("%d\n", x);return 0;}/*输入:123输出:123*/#include <bits/stdc++.h>using namespace std;int main(){int x;scanf("%d", x);printf("%d\n", x);return 0;}// 编译报错std03.cpp:7:14: warning: format specifies type 'int *' but the argument has type 'int'[-Wformat]scanf("%d", x);~~ ^
#include <bits/stdc++.h>using namespace std;int main(){int x;int *px = &x;scanf("%d", px);printf("%d\n", x);return 0;}/*输入:123输出:123*/#include <bits/stdc++.h>using namespace std;int main(){int x;int *px = &x;scanf("%d", *px); //如果这样写printf("%d\n", x);return 0;}//编译报错std03.cpp:8:14: warning: format specifies type 'int *' but the argument has type 'int'[-Wformat]scanf("%d", *px);
#include <bits/stdc++.h>using namespace std;int a[10];int main(){for (int i = 0; i < 5; i++)scanf("%d", a + i);//scanf("%d", &a[i]); //这两句话作用是一样的return 0;}
#include <bits/stdc++.h>using namespace std;char s[10]; //读入字符串的时候int main(){scanf("%s", s); //这个时候,写的是数组名称,数组名本身就是一个指针return 0;}
第六节 顺序结构实例
一本通习题
运算符和表达式
//
//
//
//被除数、除数、商、余数//% 模运算
printf("%lf");printf("%.9lf"); //保留小数点后9位
常量和变量
//可能会在理解题意上出现stuck
//多项式应该是一个超前知识点//需要认识一下 x^3 是个啥
//
//
//背景知识:物理,并联串联电阻
标准数据类型
sizeof
//
//
//上机实验:负的小数取整
//百度ascii//学会0-9 A-Z a-z//学会大小写转化,字符和数字转化
//
//
//
数据输入输出
//
//
//
//
//
顺序结构实例
//理解一下余数的定义
//v = 4/3 * PII * r^3//需要注意整数除整数的问题
//拓展:给定一个数n,翻转得到新的数字n`,输出 n + n` 的值
//这道题PII取3.14即可//注意问题的实际背景,需要整桶整桶的水
//背景知识:笛卡尔坐标系
//海伦公式p = (a+b+c) / 2;s = sqrt(p * (p-a) * (p-b) * (p-c));
//等差数列求和公式
long longprintf("%ld", x);1ll * a * b
//幂次,方 的叫法
//还是题意,剩余多少完整的苹果//也要注意苹果很少,时间很长的情况,会是什么结果
