排列与组合

组合:

public class Solution {
    static int  count = 0;
    static void findSo(String soFar,String rest) {
        if(soFar.length() == 3){
            count++;
            System.out.println(soFar);
        }else{
            for(int i=0; i<rest.length(); i++){
                String now;
                if(soFar.length()<3){
                  now = soFar+rest.charAt(i);
                }else{
                  now = soFar;
                }
                String remain = rest.substring(i+1);
                findSo(now,remain);
            }
            
        }
    }

    public static void main(String[] args) {
        String str  ="1234567";
        findSo("",str);
        System.out.println("count:"+count);
    }
}

结果:

123
124
125
126
127
134
135
136
137
145
146
147
156
157
167
234
235
236
237
245
246
247
256
257
267
345
346
347
356
357
367
456
457
467
567
count:35

C++版本

#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
int count;
void findSo(string soFar, string rest) {
        if (soFar.length() == 3) {
            count++;
            cout<<soFar<<endl;
        } else {
            for (int i = 0; i < rest.length(); i++) {
                string now;
                if(soFar.length()<3){
                    now = soFar + rest.at(i);
                }else{
                    now  = soFar;
                }
                string remain = rest.substr(i + 1);
                findSo(now, remain);
            }

        }
}
int main()
{
    string str = "1234567";
    findSo("", str);
    cout<<"count:"<<count<<endl;
}

 

排列:

public class Solution {
    static int count = 0;

    static void findSo(String soFar, String rest) {
        if (soFar.length() == 3) {
            count++;
            System.out.println(soFar);
        } else {
            for (int i = 0; i < rest.length(); i++) {
                String now = soFar + rest.charAt(i);
                String remain = rest.substring(0,i) + rest.substring(i + 1);
                findSo(now, remain);
            }

        }
    }

    public static void main(String[] args) {
        String str = "1234567";
        findSo("", str);
        System.out.println("count:" + count);
    }
}

for循环里面的代码也可以和组合一样

       for (int i = 0; i < rest.length(); i++) {
                String now;
                if (soFar.length() < 3) {
                    now = soFar + rest.charAt(i);
                } else {
                    now = soFar;
                }
                String remain = rest.substring(0, i) + rest.substring(i + 1);
                findSo(now, remain);
            }

 

结果:

123
124
125
126
127
132
134
135
136
137
142
143
145
146
147
152
153
154
156
157
162
163
164
165
167
172
173
174
175
176
213
214
215
216
217
231
234
235
236
237
241
243
245
246
247
251
253
254
256
257
261
263
264
265
267
271
273
274
275
276
312
314
315
316
317
321
324
325
326
327
341
342
345
346
347
351
352
354
356
357
361
362
364
365
367
371
372
374
375
376
412
413
415
416
417
421
423
425
426
427
431
432
435
436
437
451
452
453
456
457
461
462
463
465
467
471
472
473
475
476
512
513
514
516
517
521
523
524
526
527
531
532
534
536
537
541
542
543
546
547
561
562
563
564
567
571
572
573
574
576
612
613
614
615
617
621
623
624
625
627
631
632
634
635
637
641
642
643
645
647
651
652
653
654
657
671
672
673
674
675
712
713
714
715
716
721
723
724
725
726
731
732
734
735
736
741
742
743
745
746
751
752
753
754
756
761
762
763
764
765
count:210
View Code

 C++版本:

#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
int count;
void findSo(string soFar, string rest) {
        if (soFar.length() == 3) {
            count++;
            cout<<soFar<<endl;
        } else {
            for (int i = 0; i < rest.length(); i++) {
                string now = soFar + rest.at(i);
                string remain = rest.substr(0,i) + rest.substr(i + 1);
                findSo(now, remain);
            }

        }
}
int main()
{
    string str = "1234567";
    findSo("", str);
    cout<<"count:"<<count<<endl;
}

 从 m个元素中去n个元素的排列和组合差别仅仅在于

排列  string remain = rest.substr(0,i)+rest.substr(i + 1);

组合  string remain = rest.substr(i + 1);

所以组合代码也可以是:

#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
int count;
void findSo(string soFar, string rest)
{
    if (soFar.length() == 3)
    {
        count++;
        cout<<soFar<<endl;
    }
    else
    {
        for (int i = 0; i < rest.length(); i++)
        {
            string  now = soFar + rest.at(i);
            string remain = rest.substr(i + 1);
            findSo(now, remain);
        }

    }
}
int main()
{
    string str = "1234567";
    findSo("", str);
    cout<<"count:"<<count<<endl;
}

 ps:

 抽象的想一下 以1 2 3 4 5 6 7为例
for循环刚进来 递归肯定会得到7个 大分支
不好意思,他现在先要处理第一个分支
即("1","2,3,4,5,6,7)   在这个分支下不断的分
递归每得到一个分支就要穷尽这个分支
穷尽之后在返回到父节点的相邻分支 继续穷尽 

 

 

posted @ 2015-09-07 09:51  疾风剑  阅读(179)  评论(0编辑  收藏  举报