zoukankan      html  css  js  c++  java
  • 洛谷:P1025 数的划分

    #include<iostream>
    #include<cstring>
    using namespace std;
    int num[200],N,k,totalnum=0;//num保存每个数的数量
    int show(){//显示目前的分法
        for(int i=1;i<N;i++){
            if(num[i]>0)cout<<i<<"="<<num[i]<<" ";
        }
        return 0;
    }
    int f(int indis,int lastnum,int totalnow){
        //首先我发现所有的分法里,分的后一个数不应该小于上一个数
        if(indis<k-1){//如果选的数非最后一个数
            for(int i=0;i<N-totalnow-lastnum;i++){//如果没有可选数(前面的数加起来就>=总和了)就无法继续
                num[lastnum+i]+=1;//从0开始,比上个数依次加到可选的最大的
                f(indis+1,lastnum+i,totalnow+lastnum+i);
                num[lastnum+i]-=1;//撤销,进行下一次
            }
        }else if(indis==k-1){//如果选的是最后一个数
            int temp=N-totalnow;
            if(temp>=lastnum){//如果必须选的数比上个数大
                num[temp]+=1;
                totalnum+=1;
                //show();cout<<endl;///////调试输出
                num[temp]-=1;//该不会不需要吧
            }
        }
        return 0;
    }
    int main(){
        //memset(num,0,4*200);
        cin>>N>>k;
        f(0,1,0);
        cout<<totalnum<<endl;;
        return 0;
    }
    //地址:https://www.luogu.com.cn/problem/P1025
  • 相关阅读:
    [LeetCode 题解]: Remove Duplicates from Sorted List
    [LeetCode 题解]: Merge k Sorted Lists
    [LeetCode 题解]: Insertion Sort List
    [LeetCode 题解]:Candy
    求任意多边形面积 python实现
    C++飞机大战
    version robot
    python一段代码 感受一下
    微机原理上机第四次实验内容
    初步的百度爬虫
  • 原文地址:https://www.cnblogs.com/forwhat00/p/13214205.html
Copyright © 2011-2022 走看看