递归实现数字的组合
- #include <stdio.h>
- void Combin(int m,int n,int a)
- {
- if(m == n){
- if(a>0)printf("%d",a);
- while(n > 0) printf("%d",n--);
- printf("/n");
- return;
- }
- else if(0 == n){
- printf("%d ",a);
- return;
- }
- Combin(m-1,n-1,a*10+m);
- Combin(m-1,n,a);
- }
- int main()
- {
- Combin(9,5,0);
- }
代码返回:
98765 98764 98763 98762 98761
98754 98753 98752 98751
98743 98742 98741
98732 98731
98721
98654 98653 98652 98651
98643 98642 98641
98632 98631
98621
98543 98542 98541
98532 98531
98521
98432 98431
98421
98321
97654 97653 97652 97651
97643 97642 97641
97632 97631
97621
97543 97542 97541
97532 97531
97521
97432 97431
97421
97321
96543 96542 96541
96532 96531
96521
96432 96431
96421
96321
95432 95431
95421
95321
94321
87654 87653 87652 87651
87643 87642 87641
87632 87631
87621
87543 87542 87541
87532 87531
87521
87432 87431
87421
87321
86543 86542 86541
86532 86531
86521
86432 86431
86421
86321
85432 85431
85421
85321
84321
76543 76542 76541
76532 76531
76521
76432 76431
76421
76321
75432 75431
75421
75321
74321
65432 65431
65421
65321
64321
54321