zoukankan      html  css  js  c++  java
  • 排列与组合

    组合:

    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)   在这个分支下不断的分
    递归每得到一个分支就要穷尽这个分支
    穷尽之后在返回到父节点的相邻分支 继续穷尽 

  • 相关阅读:
    Oracle 11g Release 1 (11.1) 单行函数——比较函数
    HTTP 协议演示——演示(55)
    Oracle 字符串分割函数 splitstr 和 splitstrbyseparators
    Bitmap 索引 vs. Btree 索引:如何选择以及何时使用?——15
    Oracle ——数据库 SQL 分页性能分析
    Oracle ——数据库 Hints
    Bitmap 索引 vs. Btree 索引:如何选择以及何时使用?——35
    Oracle 索引的数据结构
    Bitmap 索引 vs. Btree 索引:如何选择以及何时使用?——25
    回溯法>图的着色问题
  • 原文地址:https://www.cnblogs.com/hixin/p/4788097.html
Copyright © 2011-2022 走看看