俊介三

一天更新一点,一天积累一点

导航

输入两个整数 n 和 m,从数列1,2,3.......n 中 随意取几个数,
使其和等于 m ,要求将其中所有的可能组合列出来.

#include <stdio.h>
#include <list>
using namespace std;

void find(int n, int m){
    static list<int> mylist;
    if(n<0 || m<0) return;
    if(m>0){
        mylist.push_front(n);
        find(n-1,m-n);

        mylist.pop_front();
        find(n-1,m);
    }else{
        list<int>::iterator it;
        for(it=mylist.begin();it!=mylist.end();it++){
            printf("%d ",*it);
        }
        puts("");
    }
}

int main(){
    find(10,15);
    return 0;
}