5

把 100 拆分成 4 个正整数之和,有多少种拆分方法(无序)?

 3 years ago
source link: https://www.zhihu.com/question/288336997/answer/1726745631
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

把 100 拆分成 4 个正整数之和,有多少种拆分方法(无序)?

47
38,753
登录一下,更多精彩内容等你发现
贡献精彩回答,参与评论互动
C++程序猿, 公众号:高级开发者

使用C++20的ranges来做。

#include <iostream>
#include <range/v3/all.hpp>

using namespace ranges;
int main(int argc, char** argv) {
    auto ints = views::iota(1, 101);
    auto result =
        count_if(
            views::cartesian_product(ints, ints, ints, ints),
            [](auto p) {
                auto [x, y, z, t] = p;
                return (x <= y && y <= z && z <= t
                    && x + y + z + t == 100);
            });
    std::cout << result << std::endl; // 7153

    return 0;
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK