设为首页 加入收藏

TOP

C++算法之旅、03 语法篇 | 全内容(二)
2023-09-09 10:25:48 】 浏览:284
Tags:全内容
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 <
首页 上一页 1 2 3 4 5 6 7 下一页 尾页 2/9/9
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇AC 自动机学习笔记 下一篇1.12 进程注入ShellCode套接字

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目