Print all unique combination of factors of a given number

后端 未结 9 1020
挽巷
挽巷 2021-02-08 05:46

What is the most efficient algorithm to print all unique combinations of factors of a positive integer. For example if the given number is 24 then the output should be

9条回答
  •  天命终不由人
    2021-02-08 06:20

    #include
    using namespace std;
    int n;
    // prod = current product of factors in combination vector
    // curr = current factor
    void fun(int curr, int prod, vector combination )
    {
        if(prod==n)
        {
            for(int j=0; jn) break;
            if(n%i==0)
            {
                combination.push_back(i);
                fun(i, i*prod, combination);
                combination.resize(combination.size()-1);
            }
        }
    }
    
    int main()
    {
        cin>>n;
        vector combination;
        fun(2, 1, combination);
        cout<<1<<" "<

提交回复
热议问题