【牛客】[编程题] 幸运的袋子C++
发布日期:2021-05-10 06:32:27 浏览次数:13 分类:精选文章

本文共 3137 字,大约阅读时间需要 10 分钟。

������������

������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

������

  • ������������������������������������������������������������������������������������������������������������������������������������������������������������

  • ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

  • ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

  • ���������������������������������������������������������������������������������������������������������������������������������������������

  • ������������

    #include 
    #include
    #include
    using namespace std;
    int* x = NULL;
    int n = 0;
    long long add = 0;
    long long mul = 1;
    void Count(int pos) {
    // ���������������pos������������
    for (int i = pos; i < n; ++i) {
    // ���������������������������
    if (x[i] == 0) continue;
    int temp_add = add + x[i];
    long long temp_mul = mul * x[i];
    if (temp_add > temp_mul) {
    // ������������������������������
    Count(i + 1);
    add = temp_add;
    mul = temp_mul;
    } else if (x[i] == 1) {
    // ������������1���������������������
    Count(i + 1);
    add -= x[i];
    mul /= x[i];
    // ������1���������������
    while (i < n - 1 && x[i] == x[i + 1] && x[i + 1] == 1) {
    ++i;
    }
    // ������������������������
    add += x[i];
    mul *= x[i];
    } else {
    // ������������������������
    add -= x[i];
    mul /= x[i];
    // ������������������������
    while (i < n - 1 && x[i] == x[i + 1]) {
    ++i;
    }
    // ������������������������
    add += x[i];
    mul *= x[i];
    break;
    }
    }
    }
    int main() {
    // ������������
    cin >> n;
    x = new int[n];
    for (int i = 0; i < n; ++i) {
    cin >> x[i];
    }
    // ������
    sort(x, x + n, greater
    ());
    // ������������������
    Count(0);
    delete[]x;
    return 0;
    }

    ������������

  • ���������������������������������������������������������������������������������������

  • ������������ Count������������������������������������������������������������������������������������������������������������������

  • ������������������������������������������������������������������������������������������������������������

  • ������������������������������������������������������������������������������������������������������������

  • ���������������������������������������������������������������������������������������������������������������������������������������������������

    上一篇:【牛客】[编程题]查找输入整数二进制中1的个数 C++
    下一篇:【牛客】[编程题]计算日期到天数转换C++

    发表评论

    最新留言

    能坚持,总会有不一样的收获!
    [***.219.124.196]2025年04月02日 08时53分52秒