202111051401程序基本语句.mp4

大纲要求

•【2】cin 语句,scanf 语句,cout语句,printf语句,赋值语句,复合语句

•【2】if语句,switch语句,多层条件语句

•【2】for语句,while语句,do while语句

•【3】多层循环语句


顺序结构程序设计 第五节 数据输入输出

  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); // 占10个字符宽度,靠右对齐
  9. printf("%-10d\n", x); // 占10个字符宽度,靠左对齐
  10. double y;
  11. scanf("%lf", &y);
  12. printf("%lf\n", y);
  13. printf("%.2lf\n", y); //小数点后保留2位
  14. return 0;
  15. }
  1. // 如果输出是两个int,中间有一个空格
  2. int a = 1, b = 2;
  3. printf("%d %d", a, b);
  4. // 如果输出是两个int,中间有一个加号
  5. int a = 1, b = 2;
  6. printf("%d+%d", a, b);
  7. // 如果只是输出一句话,可以这样
  8. printf("Hello world\n");
  9. // 在双引号里面,是所见即所得
  10. // %d 称之为,format specifier, 格式化符号
  11. // % 后面跟什么,是不同变量类型,跟不同的东西
  12. // 常见的,%d int,%lf double,%c 字符,%s 字符串

image.png

  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. // 学习一下字符数组、一维字符数组、二维字符数组
  2. // string类
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5. char c;
  6. string s1;
  7. char s2[110];
  8. char s3[110][110];
  9. int main(){
  10. // 以字符为例
  11. cin >> c;
  12. cout << c << '\n';
  13. // 读入string类
  14. cin >> s1;
  15. cout << s1 << '\n';
  16. // 读入字符数组
  17. scanf("%s", s2);
  18. printf("%s\n", s2);
  19. // 读入二维字符数组
  20. int n;
  21. cin >> n;
  22. for (int i = 0; i < n; i++) scanf("%s", s3[i]);
  23. for (int i = 0; i < n; i++) printf("%s\n", s3[i]);
  24. return 0;
  25. }
  1. // 占用字符宽度
  2. // int类型 占多少个字符宽度
  3. cin >> n;
  4. printf("%8d %8d\n", n, n);
  5. printf("%-8d %-8d\n", n, n);
  1. // 保留小数点的问题
  2. // double 类型保留小数点后几位
  3. printf("%.20lf\n", 1.0 / 3);
  4. // 一般就用printf解决保留小数点几位的问题,不用setprecision,主要是记不住
  5. double x = 3.1415926;
  6. cout << x << '\n';
  7. cout << setprecision(5) << x << '\n';
  8. cout << setprecision(4) << x << '\n';
  9. cout << setprecision(3) << x << '\n';
  10. cout << setprecision(2) << x << '\n'; // 3.1
  11. cout << setprecision(1) << x << '\n'; // 3
  12. // setprecision 不计算小数点
  13. // #include <iomanip>
  14. // 与 setw 字段宽度不同的是
  15. // setprecision 的精度设置将保持有效,直到更改为其他值为止
  16. // 流操作符 setw 可用于建立指定宽度的打印区域
  17. // 空格“填充”在前面,所以它被认为是右对齐的
  18. // 左对齐也能搞,麻烦
  19. // 用一回,写一回
  20. int y;
  21. cin >> y;
  22. cout << setw(8) << y << setw(8) << y << '\n';

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. //读入字符串
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. char s[10];
  5. int main(){
  6. scanf("%s", s); //这个时候,写的是数组名称,数组名本身就是一个指针
  7. return 0;
  8. }
  1. // 在读入数据的时候,充分利用格式化的优点
  2. // 输入数据1980-12-1
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5. int year, month, day;
  6. int main(){
  7. scanf("%d-%d-%d", &year, &month, &day);
  8. printf("%d %d %d\n", year, month, day);
  9. return 0;
  10. }

image.png

  1. // 上图,解释了,scanf里面有空格不影响的原因
  2. // 还有scanf里面有具体符号时候,匹配的问题
  3. // 还有具体限制读入一个字符宽度的数字
  4. #include<bits/stdc++.h>
  5. using namespace std;
  6. int main(){
  7. int a, b, c;
  8. scanf("%1d%1d%1d", &a, &b, &c);
  9. printf("%d %d %d\n", a, b, c);
  10. return 0;
  11. }
  12. /*
  13. 123
  14. */

例题,1982:【19CSPJ普及组】数字游戏

  1. // 这个就可以利用scanf的性质处理

例题,计算三角形面积

  1. //海伦公式
  2. p = (a+b+c) / 2;
  3. s = sqrt(p * (p-a) * (p-b) * (p-c));
  4. #include<iostream>
  5. #include<cstdio>
  6. #include<cmath>
  7. using namespace std;
  8. int main()
  9. {
  10. double x1, y1, x2, y2, x3, y3, a, b, c, p, s;
  11. scanf("%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3);
  12. a = sqrt((y2-y1)*(y2-y1)+(x2-x1)*(x2-x1));
  13. b = sqrt((y3-y1)*(y3-y1)+(x3-x1)*(x3-x1));
  14. c = sqrt((y2-y3)*(y2-y3)+(x2-x3)*(x2-x3));
  15. p = (a+b+c)/2;
  16. s = sqrt(p*(p-a)*(p-b)*(p-c));
  17. printf("%.2lf",s);
  18. return 0;
  19. }

例题,等差数列末项计算

  1. //等差数列求和公式
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. int main()
  5. {
  6. int a1, a2, n;
  7. cin >> a1 >> a2 >> n;
  8. int d = a2 - a1;
  9. int an = a1 + (n - 1) * d;
  10. cout << an << endl;
  11. return 0;
  12. }

例题,计算2的幂

  1. //幂次,方 的叫法
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. int main()
  5. {
  6. int n;
  7. cin >> n;
  8. int res = 1;
  9. while (n--)
  10. {
  11. res *= 2;
  12. }
  13. cout << res << endl;
  14. return 0;
  15. }

例题,苹果和虫子

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

控制结构程序设计 if选择结构

  1. // 分类讨论,不重不漏【*】
  2. #include <iostream>
  3. using namespace std;
  4. int main()
  5. {
  6. int x;
  7. cin >> x;
  8. if (x > 0) cout << '+' << '\n';
  9. if (x > 0) cout << '+' << '\n';
  10. else cout << "-" << '\n';
  11. if (x > 0) cout << '+' << '\n';
  12. else if (x == 0) cout << '0' << '\n';
  13. else cout << '-' << '\n';
  14. if (x > 0) cout << '+';
  15. else{
  16. if (x == 0) cout << '0';
  17. else cout << '-';
  18. }
  19. //if嵌套,套一层而已
  20. if (x >= 0){
  21. if (x == 0) cout << '0' << '\n';
  22. else cout << '+' << '\n';
  23. }
  24. return 0;
  25. }

例题,收集瓶盖赢大奖

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5. int a, b;
  6. cin >> a >> b;
  7. if (a >= 10 || b >= 20) cout << 1 << endl;
  8. else cout << 0 << endl;
  9. return 0;
  10. }

例题,判断一个数能否同时被3和5整除

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5. int n;
  6. cin >> n;
  7. if (n % 3 == 0 && n % 5 == 0) puts("YES");
  8. else puts("NO");
  9. return 0;
  10. }

例题,判断能否被3,5,7整除

  1. //

例题,判断闰年

  1. // 百度百科,闰年
  2. // 普通闰年:公历年份是4的倍数,且不是100的倍数的,为闰年(如2004年、2020年等就是闰年)。
  3. // 世纪闰年:公历年份是整百数的,必须是400的倍数才是闰年(如1900年不是闰年,2000年是闰年)。
  4. // 能被4整除,不能被100整除的,是闰年
  5. // 能被400整除的,是闰年
  6. #include <bits/stdc++.h>
  7. using namespace std;
  8. int main()
  9. {
  10. int n;
  11. cin >> n;
  12. if ((n % 4 == 0 && n % 100) || (n % 400 == 0)) puts("Y");
  13. else puts("N");
  14. return 0;
  15. }

例题,求一元二次方程

  1. // 本题对小学阶段有点麻烦,需要学习几下基本知识
  2. // 重点是阅读一下,我是如何用代码表达意思的
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5. int main(){
  6. double a, b, c;
  7. cin >> a >> b >> c;
  8. double delta = b * b - 4 * a * c;
  9. if (delta < 0) cout << "No answer!" << '\n';
  10. else if (delta == 0){
  11. printf("x1=x2=%.5lf\n", -b / (2 * a));
  12. }
  13. else{
  14. double x1 = (-b - sqrt(delta)) / (2 * a);
  15. double x2 = (-b + sqrt(delta)) / (2 * a);
  16. printf("x1=%.5lf;x2=%.5lf\n", min(x1, x2), max(x1, x2));
  17. }
  18. return 0;
  19. }

控制结构程序设计 switch语句

我从来不用switch..,你们用不用随意吧

  1. // 还是认真的给一个示例
  2. #include <iostream>
  3. using namespace std;
  4. int main()
  5. {
  6. int n;
  7. cin >> n;
  8. switch(n){
  9. case 1: cout << 1 << '\n'; break;
  10. case 2: cout << 2 << '\n'; break;
  11. default: cout << "不是1,也不是2" << '\n';
  12. }
  13. return 0;
  14. }

循环结构程序设计 for语句

  1. // i从0变化到n-1,一共循环n次
  2. for (int i = 0; i < n; i++){
  3. }
  4. for (int i = 1; i <= n; i++){
  5. //...
  6. }
  7. // 格式
  8. for (控制变量初始化表达式;条件表达式;增量表达式){
  9. }
  10. for (int i = 2; i <= n; i += 2){
  11. }
  12. // 循环嵌套,表达清楚循环的过程
  13. for (int i = 0; i < n; i++)
  14. for (int j = 0; j < n; j++){
  15. //...
  16. }

例题,求整数的和与均值

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int sum;
  4. double ave;
  5. int main()
  6. {
  7. int n;
  8. cin >> n;
  9. for (int i = 0; i < n; i++)
  10. {
  11. int x;
  12. cin >> x;
  13. sum += x;
  14. }
  15. ave = sum * 1.0 / n;
  16. printf("%d %.5lf\n", sum, ave);
  17. return 0;
  18. }

例题,最高的分数【打擂台】

  1. //介绍”打擂台“
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. int ans = -1;
  5. int main()
  6. {
  7. int n;
  8. cin >> n;
  9. while (n--)
  10. {
  11. int x;
  12. cin >> x;
  13. ans = max(ans, x);
  14. }
  15. cout << ans << endl;
  16. return 0;
  17. }

例题,奇数求和

  1. //判断奇数
  2. //模2不为0
  3. for (int i = m; i <= n; i++)
  4. if (i % 2) sum += i;

例题,乘方计算

  1. //这个幂次的数学概念,如果不清楚,视频问我
  2. long long res = 1ll;
  3. while (n--)
  4. {
  5. res *= a;
  6. }
  7. //1ll,1后面有两个l(小写L),表示的是long long类型
  8. #include <bits/stdc++.h>
  9. using namespace std;
  10. int main()
  11. {
  12. int a, n;
  13. cin >> a >> n;
  14. long long res = 1ll;
  15. while (n--)
  16. {
  17. res *= a;
  18. }
  19. printf("%lld\n", res);
  20. return 0;
  21. }

例题,求小数的某一位

  1. // 对小数的理解
  2. // 不能使用double
  3. // 为什么呢?因为,题目求小数点后10000位。double才15位
  4. // 这个时候,要模拟一下竖式除法

例题, 幂的末尾

  1. // a 的 b 次幂,这肯定是一个很大数,爆int,爆longlong
  2. // 好在题目只要求保留末三位
  3. // 最后在输出的时候,如果不足三位,要在前面补零
  4. // 这个需要考虑一下如何编程实现

循环结构程序设计 while语句

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int n;
  6. cin >> n;
  7. // 循环n次,这个写法,非常实用
  8. while (n--){
  9. //...
  10. }
  11. return 0;
  12. }
  1. while (true){
  2. if (x == 100) break; //跳出循环
  3. if (x % 2 == 1) continue; //本次循环到此为止,执行下一次循环
  4. x += 1;
  5. }
  6. while (1){
  7. x++;
  8. }
  1. for (int i = 0; i < n; i++){
  2. int j = i;
  3. while (j < n) j++;
  4. // ...
  5. }
  1. /*
  2. do while很少用
  3. do{
  4. // ...
  5. }while (true);
  6. */
  7. #include <iostream>
  8. #include <cstdio>
  9. using namespace std;
  10. int main()
  11. {
  12. int x = 100;
  13. while (x < 100){
  14. x++;
  15. }
  16. cout << x << '\n';
  17. int y = 100;
  18. do{
  19. y++;
  20. }while (y < 100);
  21. cout << y << '\n';
  22. return 0;
  23. }

例题,球弹跳高度的计算

  1. // 这个画图理解一下过程
  2. // 务必画图,建议录制讲解视频

例题,角谷猜想

  1. // 花点时间理解题意
  2. // 建议录制讲解视频

循环结构程序设计 循环嵌套

例题,求阶乘的和

  1. // 有两重循环,或者一重循环的写法

例题,金币

  1. // 这个在循环上,有点小困难
  2. // 困难的点,在如何控制每天取的币数,去多少天,一共取了多少天,怎么结束循环

例题,第n小的质数【判断质数】

  1. // 介绍试除法判断质数模板
  2. // 函数 模块化编程
  3. // 判断质数 数论,三个中的其中之一
  4. #include<bits/stdc++.h>
  5. using namespace std;
  6. bool is_prime(int x){
  7. if (x < 2) return false;
  8. for (int i = 2; i <= x / i; i++)
  9. if (x % i == 0) return false;
  10. return true;
  11. }
  12. int main()
  13. {
  14. int n;
  15. cin >> n;
  16. int cnt = 0;
  17. for (int i = 2; ; i++){
  18. if (is_prime(i)) cnt++;
  19. if (cnt == n){
  20. cout << i << '\n';
  21. break;
  22. }
  23. }
  24. return 0;
  25. }