全部评论 2

  • #include <iostream>
    #include <queue>
    using namespace std;
    
    int cnt;
    int n,k;
    
    void DFS2(int si,int sum,int step)
    {
        if(step == k) //基线条件
        {
            if(sum == n)
                cnt++;
            else
                return;
        }
        else
        {
            //优化循环,减少不必要的迭代
            for(int j=si; j<=(n-sum+1)/(k-step); j++)
            {
                if(sum + j <= n && si <= j)
                {
                    DFS2(j, sum + j, step + 1);
                }
            }
        }
        return;
    }
    
    int main()
    {
        cin >> n >> k;
        DFS2(1, 0, 0); //第一个数从1开始搜索
        cout << cnt;
        return 0;
    }带缩进代码
    

    2天前 来自 北京

    1
  • if(stepk) //基线
    {
        if(sumn)
    

    这段代码不对,因该是

    if(step == k) //基线
        {
            if(sum == n)
    

    2天前 来自 北京

    1
首页