算法笔记 ① acwing C++基础语法 | 全课程内容

打印 上一主题 下一主题

主题 1015|帖子 1015|积分 3045

2023年8月23日

#include

cstdio 有两个函数 printf,scanf 用于输出和输入
  1. int : %d
  2. float : %f
  3. double : %lf
  4. char : %c
  5. long long : %lld
复制代码
iostream 有 cin 读入,cout 输出

using namespace std;

使用了std命名空间,cin、cout定义在该命名空间中,不引入空间会找不到导致出错

int main()

函数执行入口

基本类型


a+b

⭐所有 cout、cin 都能用 scanf、printf 替换,但反过来,由于cout、cin效率可能较低会导致超时
⭐ printf %c 会读入空格跟回车,需要手动加一个空格跟回车;cin 不会读入空格跟回车
  1. #include <iostream>
  2. using namespace std;
  3. int main() {
  4.     int a, b;  // ^ 定义两个变量
  5.     cin >> a >> b;  // ^ 输入(把cin里的值拿到a里面去、b里面去)
  6.     cout << a + b << endl;  // ^ 输出
  7.     return 0;
  8. }
复制代码
666

666. 三角形类型 - AcWing题库
多注意题目意思,如果...否则...,而不是如果、否则两种情况都输出
  1. #include <cstdio>
  2. #include <iostream>
  3. using namespace std;
  4. int main() {
  5.     int a, b;
  6.     scanf("%d%d", &a, &b);
  7.     printf("%d %d\n", a + b, a * b);
  8.     return 0;
  9. }
复制代码
87 ⭐

AcWing 87. 把字符串转换成整数 - AcWing
  1.     cout << 5 % 2 << endl; // 1
  2.     cout << -5 % 2 << endl; // -1
复制代码
  1. #include <cstdio>
  2. int main() {
  3.     double r;
  4.     scanf("%lf", &r);
  5.     printf("A=%.4lf", 3.14159 * r * r);
  6.     return 0;
  7. }
复制代码
for 循环均可修改为while减少代码量
  1. #include <cstdio>
  2. int main() {
  3.     int m;
  4.     scanf("%d", &m);
  5.     printf("%d\n", m);
  6.     printf("%d nota(s) de R$ 100,00\n", m / 100);
  7.     m %= 100;
  8.     printf("%d nota(s) de R$ 50,00\n", m / 50);
  9.     m %= 50;
  10.     printf("%d nota(s) de R$ 20,00\n", m / 20);
  11.     m %= 20;
  12.     printf("%d nota(s) de R$ 10,00\n", m / 10);
  13.     m %= 10;
  14.     printf("%d nota(s) de R$ 5,00\n", m / 5);
  15.     m %= 5;
  16.     printf("%d nota(s) de R$ 2,00\n", m / 2);
  17.     m %= 2;
  18.     printf("%d nota(s) de R$ 1,00\n", m / 1);
  19.     return 0;
  20. }
复制代码
35 ⭐

AcWing 35. 反转链表 - AcWing
  1. #include <cstdio>
  2. int main() {
  3.     int a;
  4.     scanf("%d", &a);
  5.     printf("%.0lf minutos", a / 30.0 * 60);
  6.     return 0;
  7. }
复制代码
递归写法
  1. #include <cstdio>
  2. int main() {
  3.     long a, b;
  4.     scanf("%ld%ld", &a, &b);
  5.     printf("%.3lf", a * b / 12.0);
  6.     return 0;
  7. }
复制代码
66 ⭐⭐

66. 两个链表的第一个公共结点 - AcWing题库
  1. #include <cstdio>
  2. int main() {
  3.     double n;
  4.     int m;
  5.     scanf("%lf", &n);
  6.     m = (int)(n * 100);
  7.     printf("NOTAS:\n");
  8.     printf("%d nota(s) de R$ 100.00\n", m / 10000);
  9.     m %= 10000;
  10.     printf("%d nota(s) de R$ 50.00\n", m / 5000);
  11.     m %= 5000;
  12.     printf("%d nota(s) de R$ 20.00\n", m / 2000);
  13.     m %= 2000;
  14.     printf("%d nota(s) de R$ 10.00\n", m / 1000);
  15.     m %= 1000;
  16.     printf("%d nota(s) de R$ 5.00\n", m / 500);
  17.     m %= 500;
  18.     printf("%d nota(s) de R$ 2.00\n", m / 200);
  19.     m %= 200;
  20.     printf("MOEDAS:\n");
  21.     printf("%d moeda(s) de R$ 1.00\n", m / 100);
  22.     m %= 100;
  23.     printf("%d moeda(s) de R$ 0.50\n", m / 50);
  24.     m %= 50;
  25.     printf("%d moeda(s) de R$ 0.25\n", m / 25);
  26.     m %= 25;
  27.     printf("%d moeda(s) de R$ 0.10\n", m / 10);
  28.     m %= 10;
  29.     printf("%d moeda(s) de R$ 0.05\n", m / 5);
  30.     m %= 5;
  31.     printf("%d moeda(s) de R$ 0.01\n", m / 1);
  32.     return 0;
  33. }
复制代码
29

29. 删除链表中重复的节点 - AcWing题库
  1. #include <iostream>
  2. using namespace std;
  3. int main() {
  4.     string a, b, c;
  5.     cin >> a >> b >> c;
复制代码
因为B可以由A推导出,又可以改为
  1. #include <cstdio>
  2. #include <iostream>
  3. using namespace std;
  4. int main() {
  5.     double a, b, c, tmp;
  6.     cin >> a >> b >> c;
  7.     if (b >= a && b >= c) {
  8.         tmp = a;
  9.         a = b;
  10.         b = tmp;
  11.     } else if (c >= a && c >= b) {
  12.         tmp = c;
  13.         c = a;
  14.         a = tmp;
  15.     }
  16.     if (a >= b + c)
  17.         cout << "NAO FORMA TRIANGULO" << endl;
  18.     else {
  19.         if (a * a == b * b + c * c) cout << "TRIANGULO RETANGULO" << endl;
  20.         if (a * a > b * b + c * c) cout << "TRIANGULO OBTUSANGULO" << endl;
  21.         if (a * a < b * b + c * c) cout << "TRIANGULO ACUTANGULO" << endl;
  22.         if (a == b && a == c && b == c) cout << "TRIANGULO EQUILATERO" << endl;
  23.         if (a == b && c != b || a == c && b != c || b == c && a != b)
  24.             cout << "TRIANGULO ISOSCELES" << endl;
  25.     }
  26.     return 0;
  27. }
复制代码
vector

int q[100000][100000] 需要大量空间,用可变长数组可以节省很多空间。结尾插入O(1),开头插入O(n),自带比较,按字典序比
  1. #include <cmath>
  2. #include <cstdio>
  3. int main() {
  4.     double a, b, c;
  5.     scanf("%lf%lf%lf", &a, &b, &c);
  6.     if (b * b - 4 * a * c < 0 || a == 0) {
  7.         printf("Impossivel calcular");
  8.     } else {
  9.         printf("R1 = %.5lf\n", (-b + sqrt(b * b - 4 * a * c)) / (2 * a));
  10.         printf("R2 = %.5lf", (-b - sqrt(b * b - 4 * a * c)) / (2 * a));
  11.     }
  12.     return 0;
  13. }
复制代码
queue
  1. #include <algorithm>
  2. #include <cstdio>
  3. #include <iostream>
  4. using namespace std;
  5. int main() {
  6.     int x, y;
  7.     cin >> x >> y;
  8.     if (x > y) {
  9.         swap(x, y);
  10.     }
  11.     int total = 0;
  12.     for (x++; x < y; x++) {
  13.         if (abs(x) % 2 == 1) total += x;
  14.     }
  15.     cout << total;
  16.     return 0;
  17. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

飞不高

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表