cin cout

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int x;
  6. cin >> x;
  7. cout << x;
  8. return 0;
  9. }

printf

  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. // 竞赛当中,很少使用不用setprecision
  6. double x = 3.1415926;
  7. cout << x << '\n';
  8. cout << setprecision(5) << x << '\n';
  9. cout << setprecision(4) << x << '\n';
  10. cout << setprecision(3) << x << '\n';
  11. cout << setprecision(2) << x << '\n'; // 3.1
  12. cout << setprecision(1) << x << '\n'; // 3
  13. // setprecision 不计算小数点
  14. // #include <iomanip>
  15. // 与 setw 字段宽度不同的是
  16. // setprecision 的精度设置将保持有效,直到更改为其他值为止
  17. // 流操作符 setw 可用于建立指定宽度的打印区域
  18. // 空格“填充”在前面,所以它被认为是右对齐的
  19. // 左对齐也能搞,麻烦
  20. // 用一回,写一回
  21. int y;
  22. cin >> y;
  23. cout << setw(8) << y << setw(8) << y << '\n';

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. //读入字符串
  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. //也要注意苹果很少,时间很长的情况,会是什么结果【*】

整数除法

  1. int x = 1, y = 2;
  2. cout << x / y << '\n';
  3. // 会输出 0
  4. // 整数除以整数,会自动向下取整
  5. cout << (x + y - 1) / y << '\n';
  6. // 这样做会得到一个向上取整的结果
  7. double z = 1.0 * x / y;
  8. // 这样能得到一个小数结果