g a, b, c;
cin >> a >> b >> c;
666
666. 三角形类型 - AcWing题库
多注意题目意思,如果...否则...,而不是如果、否则两种情况都输出
#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;
}
658
658. 一元二次方程公式 - AcWing题库
容易忘记最后 2 * a
的括号,导致先除后乘
#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;
}
2023年8月25日
读入个数未知
读入的个数未知时,可以用while(cin >> x)
或while(scanf("%d", &x) != -1)
或while(~scanf("%d", &x))
来输入。
如果输入的最后一个为0且该0不处理,输入语句可以用while(cin >> x && x)
或while(cin >> x, x)
,逗号表达式取最后一个值。
714
714. 连续奇数的和 1 - AcWing题库
algorithm 函数库的使用 , % 相关的概念
#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;
}
725
725. 完全数 - AcWing题库
设一个公约数 d 那么另一个公约数 x / d , d <= x/d 得 d <= 根号x;另外还要考虑1的特殊情况。
AcWing 726. 质数 - AcWing 这题同理
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
int n, x;
cin >> n;
while (cin >> x, n--) {
int sum = 0;
for (int i = 1; i * i <= x; i++) {
if (x % i == 0) {
if (i < x) sum += i;
if (x / i != i && x / i < x) sum += x / i;
}
}
if (sum == x)
cout << x << " is perfect" << endl;
else
cout << x << " is not perfect" << endl;
}
return 0;
}
2023年8月26日
reverse
在 alogorithm
库中,用于翻转数组。涉及题目(翻转整个,然后两边各自再翻转)
memset
在 cstring
库中,用于数组的初始化,速度比循环初始化要快。参数是数组指针,值(字节),长度(字节)
memset(a,-1,sizeof a)
memcpy
在 cstring
库中,用于复制数组。参数是目标数组指针,被复制数组指针,长度(字节)
753
753. 平方矩阵 I - AcWing题库
技巧,需要看各个格子距离上下左右边的最短距离。下面两道也是技巧性题目
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
int n;
while (cin >> n, n) {
for (int i = 1; i <= n; i++) {
for (int k = 1; k <= n; k++) {
cout << min(min(i, k), min(n + 1 - k, n + 1 - i)) << " ";
}
cout << endl;
}
cout << endl;
}
return 0;
}
754
754. 平方矩阵 II - AcWing题库
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
int n;
while (cin >> n, n) {
for (int i = 1; i <= n; i++) {
for (int k = 1; k <