字典树
[cpp]
/*
*/
#include<iostream>
#include<cstdio>
#include<malloc.h>
using namespace std;
#define manx 12
struct node{
int num;
node*next[manx];
};
int n,r,stack[20];
void init(node *tree,int n){
tree->num = n;
for(int i=0;i<manx;i++)
tree->next[i]=NULL;
}
void make(node *tree,int n){
init(tree,n);
for(int i=1;i<=n-1;i++){
if(tree->next[i]==NULL){
node *p = new node();
tree->next[i]=p;
make(p,n-i);
}
}
}
void query(node *tree,int k){
if(k==r){
for(int i=1;i<=k;i++)
printf("%d",stack[i]);
printf("\n");
return ;
}
for(int i=1;i<manx;i++){
if(tree->next[i]==NULL) continue;
stack[++k] = tree->next[i]->num;
query(tree->next[i],k);
k--;
}
}
int main(){
while(cin>>n>>r){
for(int i=n;i>=1;i--){
node *tree = new node();
make(tree,i);
stack[1]=tree->num;
query(tree,1);
}
}
}
补充:软件开发 , C++ ,