大纲要求
•【1】整数型:int, long long
•【1】实数型:float, double
•【1】字符型:char
•【1】布尔型:bool
变量名称
// n个数int n;// 一个数int n;int x;// 两个数int a, b;int x, y;// 求和int sum;// 计数int cnt;// 写循环, i, j, kfor (int i = 0; i < n; i++)for (int j = 0; j < m; j++)for (int k = 0; k < p; k++)// 如果有更多层的循环,i1, i2, i3, i4// 开数组const int N = 110;int a[N];const int N = 1e5 + 10;int a[N];// n行,m列的矩阵int n, m;// 坐标int x, y;// 和生活中的一些事物,有关联// 钱,时间,距离int money, int tim, int disint d;// 临时变量,存一些中间信息int temp;int t;// 有n组数据int T;while (T--){}// 奥义// 英文单词简写(缩写)
顺序程序设计 第一节 赋值语句
变量(盛饭的碗,一个有名字的碗,一个空的碗,可以盛上饭,可以盛不同的饭,就是赋值不同的数值)
int a;a = 5;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,负数右移一位,向负方向取整return 0;}
// 【扩展】// 涉及负数的二进制表示,原码反码补码问题// 教给大家一个自己玩的小东西,有了这个,就可以自己去验证原码反码补码的变化过程// 进而,从根源上理解,而不是死记硬背#include <bits/stdc++.h>using namespace std;void print(int x){for (int j = 31; j >= 0; j--)if (x >> j & 1) cout << 1;else cout << 0;puts("");}int main(){int x = -5;print(x);x >>= 1;print(x);return 0;}
// ++i, i++ 的区别// ++i是,i先增加1位,赋值给别人// i++是,i先赋值给别人,自己再增加1位i++; // i从1变到了2i--; // i从2变到了1++i;--i;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;}if (a = b){cout << '=' << '\n';}else{cout << "!=" << '\n';}/*测试数据3 33 00 00 3*/// 一些大小关系的示例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 += 5;a -= 5;// 前导零问题cout << 00123 << '\n';
// 一些常用的数据函数#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'; //四舍五入double x = -3.2;cout << round(x) << '\n';int n = 16;cout << sqrt(n) << '\n'; //开平方根,开根号 4*4=16 sqrt(16)=4cout << pow(2, 3) << '\n'; //2^3=8return 0;}
例题,CSP2021-J1阅读程序第1题【拓展】
// 【拓展】// 题目大意// char table[0] = 0xff;// cout << int(table[0]) << endl;// 问,输出的是不是-1#include <bits/stdc++.h>using namespace std;void print(int x){for (int j = 31; j >= 0; j--)if (x >> j & 1) cout << 1;else cout << 0;puts("");}int main(){unsigned char x = 0xff;char y = 0xff;cout << (int)x << '\n';print(x);cout << (int)y << '\n';print(y);return 0;}
顺序程序设计 第三节 常量和变量
const int N = 110;const double PII = 3.14159;
顺序程序设计 第四节 标准数据类型
// 以下几个单词,要记笔记int, 整型,用来存整数double, 双精度浮点型,用来存小数/实数char, 字符类型,用来存字符bool, 布尔类型,用来存true/falselong long, 长整型,用来存爆int的数,1e18这个量级的string, string类,用来存字符串的// 其他不常用的:short, 短整型float, 单精度浮点数
int,占4字节(Byte),取值范围 [-2^31, 2^31-1], 取值范围 [-2147483648, 2147483647]int n = (1ll << 31) - 1;cout << n << '\n';n++;cout << n << '\n';// 示例#include <bits/stdc++.h>using namespace std;typedef long long ll;int main(){int n = INT_MAX, m = INT_MIN;cout << n << '\n' << m << '\n';return 0;}
long long, 取值范围[-9223372036854775808, 9223372036854775807]1e18级别的// 示例#include <bits/stdc++.h>using namespace std;typedef long long ll;int main(){ll n = LLONG_MAX, m = LLONG_MIN;cout << n << '\n' << m << '\n';return 0;}
// 关于占用存储空间的大小// 各个类型的变量长度由编译器来决定(实际上与操作系统位数和编译器都有关)// 使用时可用sizeof()得到// 当前主流编译器一般是32位或64位类型 32位 64位char 1 1char* 4 8short int 2 2int 4 4unsigned int 4 4long 4 8long long 8 8float 4 4double 8 8unsigned long 4 8
// 区间的符号// 区间 [1,5] 1,2,3,4,5 闭区间// 区间 (1,5) 2,3,4// 区间 (1,5] 2,3,4,5// 指数2^3 = 8// 单位换算1 Byte 8 bit1024 Byte 1 KB1024 KB 1 MB1024 MB 1 GB1024 GB 1 TB// 无符号整型unsigned int 无符号整型 4字节 [0, 4294967295]unsigned long long 8字节cout << (1 << 32) - 1 << '\n'; // 注意warning提示cout << (1ll << 32) - 1 << '\n';unsigned int n = (1ll << 33) - 1;n++;cout << n << '\n';// 关于有效位数的区分float 单精度浮点数 4字节 有效位数 6~7位double 双精度浮点数 8字节 有效位数 15~16位// 换行的操作cout << '\n'; 转义字符 换行/回车cout << endl;puts("");
// 输出占用存储空间的大小#include <iostream>using namespace std;int main(){cout << sizeof(int) << '\n';return 0;}
// 保留小数点后多少位的写法// 有效位数的区别#include <iostream>using namespace std;int main(){double x = 1.0 / 3;printf("%.40lf\n", x);return 0;}// 0.3333333333333333148296162562473909929395
// 头文件的使用规则是需要使用什么头文件,就写什么头文件在使用DEV C++的时候,软件会默认带上一些头文件,这对新手来说,不是什么好事情基本的,你需要记住的是cin cout #include <iostream>scanf printf #include <cstdio>floor()这种函数 #include <cmath>sort()这个方法 #include <algorithm>// 万能头,是我们把语言部分比较熟练了之后,开始使用的,前期不应该使用,但要认识#include <bits/stdc++.h>
例题,甲流疫情死亡率
// 可能会在理解题意上出现stuck#include <cstdio>using namespace std;int main(){int a, b;scanf("%d%d", &a, &b);printf("%.3lf%%\n", 1.0 * b / a * 100);return 0;}
例题, 计算多项式的值
// 多项式应该是一个超前知识点// 需要认识一下 x^3 是个啥#include <cstdio>using namespace std;int main(){double x, a, b, c, d;scanf("%lf%lf%lf%lf%lf", &x, &a, &b, &c, &d);double ans = a * x * x * x + b * x * x + c * x + d;printf("%.7lf\n", ans);return 0;}
例题,计算并联电阻的阻值
// 背景知识:物理,并联串联电阻// 适应一下这种题面,“新定义”#include <cstdio>using namespace std;int main(){double r1, r2;scanf("%lf%lf", &r1, &r2);double r = 1.0 / (1 / r1 + 1 / r2);printf("%.2lf\n", r);return 0;}
例题,整型数据类型存储空间大小
// sizeof// 输出占用存储空间大小
例题,打印ASCII码
// 百度ascii// 学会0-9 A-Z a-z// 学会大小写转化,字符和数字转化#include <bits/stdc++.h>using namespace std;int main(){char c;scanf("%c", &c);printf("%d\n", c);return 0;}
例题,计算浮点数相除的余
// 理解一下余数的定义
例题,计算球的体积
// v = 4/3 * PII * r^3// 需要注意整数除整数的问题
例题,反向输出一个三位数
#include <iostream>using namespace std;int main(){int x;cin >> x;cout << x % 10 << x / 10 % 10 << x / 100 << '\n';return 0;}// 拓展:给定一个数n,翻转得到新的数字n`,输出 n + n` 的值
例题,大象喝水查
// 这道题PII取3.14即可// 注意问题的实际背景,需要整桶整桶的水// 如果不知道圆柱体的体积,可以百度

