2023年8月23日
#include
cstdio 有两个函数 printf,scanf 用于输出和输入- int : %d
- float : %f
- double : %lf
- char : %c
- 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 不会读入空格跟回车- #include <iostream>
- using namespace std;
- int main() {
- int a, b; // ^ 定义两个变量
- cin >> a >> b; // ^ 输入(把cin里的值拿到a里面去、b里面去)
- cout << a + b << endl; // ^ 输出
- return 0;
- }
复制代码 666
666. 三角形类型 - AcWing题库
多注意题目意思,如果...否则...,而不是如果、否则两种情况都输出- #include <cstdio>
- #include <iostream>
- using namespace std;
- int main() {
- int a, b;
- scanf("%d%d", &a, &b);
- printf("%d %d\n", a + b, a * b);
- return 0;
- }
复制代码 87 ⭐
AcWing 87. 把字符串转换成整数 - AcWing- cout << 5 % 2 << endl; // 1
- cout << -5 % 2 << endl; // -1
复制代码- #include <cstdio>
- int main() {
- double r;
- scanf("%lf", &r);
- printf("A=%.4lf", 3.14159 * r * r);
- return 0;
- }
复制代码 for 循环均可修改为while减少代码量- #include <cstdio>
- int main() {
- int m;
- scanf("%d", &m);
- printf("%d\n", m);
- printf("%d nota(s) de R$ 100,00\n", m / 100);
- m %= 100;
- printf("%d nota(s) de R$ 50,00\n", m / 50);
- m %= 50;
- printf("%d nota(s) de R$ 20,00\n", m / 20);
- m %= 20;
- printf("%d nota(s) de R$ 10,00\n", m / 10);
- m %= 10;
- printf("%d nota(s) de R$ 5,00\n", m / 5);
- m %= 5;
- printf("%d nota(s) de R$ 2,00\n", m / 2);
- m %= 2;
- printf("%d nota(s) de R$ 1,00\n", m / 1);
- return 0;
- }
复制代码 35 ⭐
AcWing 35. 反转链表 - AcWing- #include <cstdio>
- int main() {
- int a;
- scanf("%d", &a);
- printf("%.0lf minutos", a / 30.0 * 60);
- return 0;
- }
复制代码 递归写法- #include <cstdio>
- int main() {
- long a, b;
- scanf("%ld%ld", &a, &b);
- printf("%.3lf", a * b / 12.0);
- return 0;
- }
复制代码 66 ⭐⭐
66. 两个链表的第一个公共结点 - AcWing题库
 - #include <cstdio>
- int main() {
- double n;
- int m;
- scanf("%lf", &n);
- m = (int)(n * 100);
- printf("NOTAS:\n");
- printf("%d nota(s) de R$ 100.00\n", m / 10000);
- m %= 10000;
- printf("%d nota(s) de R$ 50.00\n", m / 5000);
- m %= 5000;
- printf("%d nota(s) de R$ 20.00\n", m / 2000);
- m %= 2000;
- printf("%d nota(s) de R$ 10.00\n", m / 1000);
- m %= 1000;
- printf("%d nota(s) de R$ 5.00\n", m / 500);
- m %= 500;
- printf("%d nota(s) de R$ 2.00\n", m / 200);
- m %= 200;
- printf("MOEDAS:\n");
- printf("%d moeda(s) de R$ 1.00\n", m / 100);
- m %= 100;
- printf("%d moeda(s) de R$ 0.50\n", m / 50);
- m %= 50;
- printf("%d moeda(s) de R$ 0.25\n", m / 25);
- m %= 25;
- printf("%d moeda(s) de R$ 0.10\n", m / 10);
- m %= 10;
- printf("%d moeda(s) de R$ 0.05\n", m / 5);
- m %= 5;
- printf("%d moeda(s) de R$ 0.01\n", m / 1);
- return 0;
- }
复制代码 29
29. 删除链表中重复的节点 - AcWing题库- #include <iostream>
- using namespace std;
- int main() {
- string a, b, c;
- cin >> a >> b >> c;
复制代码 因为B可以由A推导出,又可以改为- #include <cstdio>
- #include <iostream>
- using namespace std;
- int main() {
- double a, b, c, tmp;
- cin >> a >> b >> c;
- if (b >= a && b >= c) {
- tmp = a;
- a = b;
- b = tmp;
- } else if (c >= a && c >= b) {
- tmp = c;
- c = a;
- a = tmp;
- }
- if (a >= b + c)
- cout << "NAO FORMA TRIANGULO" << endl;
- else {
- if (a * a == b * b + c * c) cout << "TRIANGULO RETANGULO" << endl;
- if (a * a > b * b + c * c) cout << "TRIANGULO OBTUSANGULO" << endl;
- if (a * a < b * b + c * c) cout << "TRIANGULO ACUTANGULO" << endl;
- if (a == b && a == c && b == c) cout << "TRIANGULO EQUILATERO" << endl;
- if (a == b && c != b || a == c && b != c || b == c && a != b)
- cout << "TRIANGULO ISOSCELES" << endl;
- }
- return 0;
- }
复制代码 vector
int q[100000][100000] 需要大量空间,用可变长数组可以节省很多空间。结尾插入O(1),开头插入O(n),自带比较,按字典序比- #include <cmath>
- #include <cstdio>
- int main() {
- double a, b, c;
- scanf("%lf%lf%lf", &a, &b, &c);
- if (b * b - 4 * a * c < 0 || a == 0) {
- printf("Impossivel calcular");
- } else {
- printf("R1 = %.5lf\n", (-b + sqrt(b * b - 4 * a * c)) / (2 * a));
- printf("R2 = %.5lf", (-b - sqrt(b * b - 4 * a * c)) / (2 * a));
- }
- return 0;
- }
复制代码 queue
- #include <algorithm>
- #include <cstdio>
- #include <iostream>
- using namespace std;
- int main() {
- int x, y;
- cin >> x >> y;
- if (x > y) {
- swap(x, y);
- }
- int total = 0;
- for (x++; x < y; x++) {
- if (abs(x) % 2 == 1) total += x;
- }
- cout << total;
- return 0;
- }
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |