阶乘分解 kkmd66

#include 
#include 
#include "vector"

using namespace std;

/**
 * kkmd66 二刷
 * @return
 */

int main() {
    int n;
    while (cin >> n) {
        //存储素数
        vector<int> number;
        for (int i = 2; i <= n; ++i) {
            //判断是否为素数
            bool flag = true;
            for (int j = 2; j <= sqrt(i); ++j) {
                if (i % j == 0) {
                    flag = false;
                    break;
                }
            }
            //添加
            if (flag)
                number.push_back(i);
        }

        //存放阶乘
        vector<int> number_all(n);
        for (int i = 0; i < n; ++i) {
            number_all[i] = i + 1;
        }

        //依次相除,并存储
        vector<int> result;
        for (int i = 0; i < number.size(); ++i) {
            //计次
            int count = 0;
            for (int j = 0; j < number_all.size(); ++j) {
                while (number_all[j] % number[i] == 0) {
                    number_all[j] /= number[i];
                    count++;
                }
            }
            result.push_back(count);
        }

        //输出
        for (int i = 0; i < result.size(); ++i) {
            if (i != result.size() - 1)
                cout << result[i] << " ";
            else
                cout << result[i];
        }
    }
    return 0;
}

你可能感兴趣的:(OJ,c++,算法,动态规划)