组合学:26个字母(含大小写)和10个数字组合为4位串的可能性测算

使用26个字母(含大小写,实际为52个字母)和10个数字组合一个4位的串码,问有多少种组合?

'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
, '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'

一.从数学角度推导出组合公式

组合数公式是指从 n 个不同元素中,任取 m(m≤n) 个元素并成一组,叫做从 n 个不同元素中取出 m 个元素的一个组合;从 n 个不同元素中取出 m(m≤n) 个元素的所有组合的个数,叫做 n 个不同元素中取出 m 个元素的组合数。用符号 C(n,m) 表示。

以数字集合n1,取m个元素做组合,该集合内无重复元素(有重复元素就要先去重)

n1 = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }

此例可初步推导出,在n个无重复元素集合中,取m个元素做组合(长度为x,组合时可复用单个元素),组成为不同的串码组合,可以有n的m次方(n^m或n^x)种组合;

此处n1=10,m=4;

由上得出C(n,m)=n1的m次方=n1^m=10^4=10000;

若n1=10,m=3;

得出C(n,m)=n1^m=10^3=1000;

二.使用java程序实现组合结果,并输出(此DEMO程序没有做去重复元素的逻辑实现)

/**
 * Copyright (C), 2000-2021, XXX有限公司
 * FileName: combination
 * Author: wangyetao
 * Date: 21-10-20 11:20:40
 * Description: 组合学:26个字母(含大小写)和10个数字组合为4位串的可能性测算
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 版本描述
 */
package simple.callback.cryptographyalgorithm;

/**
 * @ClassName: combination
 * @Description: java类描述
 * @Author: wangyetao
 * @Date: 21-10-20 11:20:40
 */
public class Combination {

    //四个数组,实际可用递归代替,这里只做直观的演示用
    private static char[] charLibs0 = new char[]{
            /*'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
            , 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
            , */'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
    };
    private static char[] charLibs1 = new char[]{
            /*'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
            , 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
            , */'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
    };
    private static char[] charLibs2 = new char[]{
            /*'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
            , 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
            , */'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
    };
    private static char[] charLibs3 = new char[]{
            /*'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
            , 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
            , */'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
    };
    //标位1
    //标位2
    //标位3
    //标位4
    //计数
    private static int count = 0;


    //TEST
    public static void main(String[] args) {


        for (int i = 0; i < charLibs0.length; i++) {
            for (int j = 0; j < charLibs1.length; j++) {
                for (int k = 0; k < charLibs2.length; k++) {
                    for (int l = 0; l < charLibs3.length; l++) {

                        System.out.println(charLibs0[i] + "" + charLibs1[j] + "" + charLibs2[k] + "" + charLibs3[l]); //输出单次组合结果
                        count++;

                    }
                }
            }
        }

        System.out.println("count:" + count);
    }



}

程序输出0(仅数字的组合):

0000
0001
0002

......

9997
9998
9999
count:10000

Process finished with exit code 0

 程序输出1(仅小写字母的组合):

......
zzzw
zzzx
zzzy
zzzz
count:456976

Process finished with exit code 0

程序输出2(大小写字母+数字的组合):

count:14776336

Process finished with exit code 0

在此记录与总结,如有错误请指正,感谢!2021年 10月 20日 星期三 13:11:32 CST。 

UPDATE1,2021年 10月 20日 星期三 18:04:15 CST

simple.callback.cryptographyalgorithm.CombinationV2.java

/**
 * Copyright (C), 2000-2021, XXX有限公司
 * FileName: CombinationV2
 * Author: wangyetao
 * Date: 21-10-20 15:10:31
 * Description:组合学:26个字母(含大小写)和10个数字组合为4位串码的可能性测算V2
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 版本描述
 */
package simple.callback.cryptographyalgorithm;

import java.util.ArrayList;

/**
 * @ClassName: CombinationV2
 * @Description: java类描述
 * @Author: wangyetao
 * @Date: 21-10-20 15:10:31
 */
public class CombinationV2 {

    //字符数组
    private static char[] charLibs = new char[]{
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
            , 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
            , 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'

    };
    //单例实例对象
    private static CombinationV2 instance = null;
    //存储所有生成的组合,一次装载到内存,也可通过其它中间件方式;volatile+同步锁,确保多线程环境下数据安全
    private volatile static ArrayList<String> libs = new ArrayList();
    //记数
    private static int count = 0;
    //标位
    private volatile static int nextIndex = -1;

    //获取组合串码的总个数
    public int libsize() {
        return libs.size();
    }

    private CombinationV2() {
    }

    //获取单例
    public static CombinationV2 getInstance() {
        //class同步锁
        synchronized (CombinationV2.class) {
            if (instance == null) {
                long startTime = System.currentTimeMillis();
                init();
                instance = new CombinationV2();
                long consumeTime = System.currentTimeMillis() - startTime;
                System.out.println("首次创建单例实例对象耗时:" + consumeTime + "ms");
            }

            return instance;
        }
    }


    /**
     * 初始化生成并保存所有的不重复的4位串码组合
     * 此处暂用FOR嵌套实现。题外话:看有些技术社区文章说可用递归实现
     */
    private static void init() {

        for (int i = 0; i < charLibs.length; i++) {
            for (int j = 0; j < charLibs.length; j++) {
                for (int k = 0; k < charLibs.length; k++) {
                    for (int l = 0; l < charLibs.length; l++) {

                        libs.add(count, charLibs[i] + "" + charLibs[j] + "" + charLibs[k] + "" + charLibs[l]);
                        count++;

                    }
                }
            }
        }

    }


    //取串码
    public String next() {
        synchronized (CombinationV2.class) {
            if (libs != null && libs.size() > 0) {
                nextIndex++;
                if (nextIndex < libsize()) {
                    return libs.get(nextIndex);
                }
            }
        }
        return "";
    }


    //标位重置
    public void indexReset() {
        synchronized (CombinationV2.class) {
            nextIndex = -1;
        }
    }


}

simple.callback.cryptographyalgorithm.CombinationV2Test.java

/**
 * Copyright (C), 2000-2021, XXX有限公司
 * FileName: CombinationV2Test
 * Author: wangyetao
 * Date: 21-10-20 17:09:56
 * Description:测试用例
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 版本描述
 */
package simple.callback.cryptographyalgorithm;

import org.junit.Test;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @ClassName: CombinationV2Test
 * @Description: 测试用例类
 * @Author: wangyetao
 * @Date: 21-10-20 17:09:56
 */
public class CombinationV2Test {


    @Test
    public void testCombinationV2() throws InterruptedException {

    }

    public static void main(String[] args) {
        ExecutorService cachedThreadPool = Executors.newCachedThreadPool();

        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                task();
            }
        };

        //50*3=150
        cachedThreadPool.execute(runnable);
        cachedThreadPool.execute(runnable);
        cachedThreadPool.execute(runnable);
        cachedThreadPool.shutdownNow();
        cachedThreadPool.shutdown();
    }

    //5*5*2=50
    public static void task() {

        CombinationV2 combinationV2 = CombinationV2.getInstance();
        CombinationV2 combinationV21 = CombinationV2.getInstance();

        System.out.println("Thread-" + Thread.currentThread().getName() + "-libsize=" + combinationV2.libsize());
        for (int i = 0; i < 5; i++) {

            for (int j = 0; j < 5; j++) {
                System.out.println("Thread-" + Thread.currentThread().getName() + "-combinationV2:" + combinationV2.next());
                System.out.println("Thread-" + Thread.currentThread().getName() + "-combinationV21:" + combinationV21.next());
            }

        }
    }
}

DEMO输出结果:

首次创建单例实例对象耗时:15952ms
Thread-pool-1-thread-1-libsize=14776336
Thread-pool-1-thread-2-libsize=14776336
Thread-pool-1-thread-3-libsize=14776336
Thread-pool-1-thread-2-combinationV2:0001
Thread-pool-1-thread-1-combinationV2:0000
Thread-pool-1-thread-1-combinationV21:0004
Thread-pool-1-thread-1-combinationV2:0005
Thread-pool-1-thread-2-combinationV21:0003
Thread-pool-1-thread-3-combinationV2:0002
Thread-pool-1-thread-3-combinationV21:0008
Thread-pool-1-thread-3-combinationV2:0009
Thread-pool-1-thread-3-combinationV21:000a
Thread-pool-1-thread-3-combinationV2:000b
Thread-pool-1-thread-3-combinationV21:000c
Thread-pool-1-thread-2-combinationV2:0007
Thread-pool-1-thread-1-combinationV21:0006
Thread-pool-1-thread-2-combinationV21:000e
Thread-pool-1-thread-3-combinationV2:000d
Thread-pool-1-thread-2-combinationV2:000g
Thread-pool-1-thread-1-combinationV2:000f
Thread-pool-1-thread-2-combinationV21:000i
Thread-pool-1-thread-3-combinationV21:000h
Thread-pool-1-thread-2-combinationV2:000k
Thread-pool-1-thread-1-combinationV21:000j
Thread-pool-1-thread-2-combinationV21:000m
Thread-pool-1-thread-3-combinationV2:000l
Thread-pool-1-thread-2-combinationV2:000o
Thread-pool-1-thread-1-combinationV2:000n
Thread-pool-1-thread-2-combinationV21:000q
Thread-pool-1-thread-3-combinationV21:000p
Thread-pool-1-thread-2-combinationV2:000s
Thread-pool-1-thread-1-combinationV21:000r
Thread-pool-1-thread-2-combinationV21:000u
Thread-pool-1-thread-3-combinationV2:000t
Thread-pool-1-thread-2-combinationV2:000w
Thread-pool-1-thread-1-combinationV2:000v
Thread-pool-1-thread-2-combinationV21:000y
Thread-pool-1-thread-3-combinationV21:000x
Thread-pool-1-thread-2-combinationV2:000A
Thread-pool-1-thread-1-combinationV21:000z
Thread-pool-1-thread-2-combinationV21:000C
Thread-pool-1-thread-3-combinationV2:000B
Thread-pool-1-thread-2-combinationV2:000E
Thread-pool-1-thread-1-combinationV2:000D
Thread-pool-1-thread-2-combinationV21:000G
Thread-pool-1-thread-3-combinationV21:000F
Thread-pool-1-thread-2-combinationV2:000I
Thread-pool-1-thread-1-combinationV21:000H
Thread-pool-1-thread-2-combinationV21:000K
Thread-pool-1-thread-3-combinationV2:000J
Thread-pool-1-thread-2-combinationV2:000M
Thread-pool-1-thread-1-combinationV2:000L
Thread-pool-1-thread-2-combinationV21:000O
Thread-pool-1-thread-3-combinationV21:000N
Thread-pool-1-thread-2-combinationV2:000Q
Thread-pool-1-thread-1-combinationV21:000P
Thread-pool-1-thread-2-combinationV21:000S
Thread-pool-1-thread-3-combinationV2:000R
Thread-pool-1-thread-3-combinationV21:000V
Thread-pool-1-thread-2-combinationV2:000U
Thread-pool-1-thread-1-combinationV2:000T
Thread-pool-1-thread-2-combinationV21:000X
Thread-pool-1-thread-3-combinationV2:000W
Thread-pool-1-thread-2-combinationV2:000Z
Thread-pool-1-thread-1-combinationV21:000Y
Thread-pool-1-thread-2-combinationV21:0011
Thread-pool-1-thread-2-combinationV2:0013
Thread-pool-1-thread-2-combinationV21:0014
Thread-pool-1-thread-2-combinationV2:0015
Thread-pool-1-thread-2-combinationV21:0016
Thread-pool-1-thread-2-combinationV2:0017
Thread-pool-1-thread-2-combinationV21:0018
Thread-pool-1-thread-2-combinationV2:0019
Thread-pool-1-thread-2-combinationV21:001a
Thread-pool-1-thread-2-combinationV2:001b
Thread-pool-1-thread-2-combinationV21:001c
Thread-pool-1-thread-2-combinationV2:001d
Thread-pool-1-thread-2-combinationV21:001e
Thread-pool-1-thread-2-combinationV2:001f
Thread-pool-1-thread-2-combinationV21:001g
Thread-pool-1-thread-2-combinationV2:001h
Thread-pool-1-thread-2-combinationV21:001i
Thread-pool-1-thread-2-combinationV2:001j
Thread-pool-1-thread-2-combinationV21:001k
Thread-pool-1-thread-2-combinationV2:001l
Thread-pool-1-thread-2-combinationV21:001m
Thread-pool-1-thread-2-combinationV2:001n
Thread-pool-1-thread-2-combinationV21:001o
Thread-pool-1-thread-3-combinationV21:0010
Thread-pool-1-thread-3-combinationV2:001p
Thread-pool-1-thread-3-combinationV21:001q
Thread-pool-1-thread-3-combinationV2:001r
Thread-pool-1-thread-3-combinationV21:001s
Thread-pool-1-thread-3-combinationV2:001t
Thread-pool-1-thread-3-combinationV21:001u
Thread-pool-1-thread-3-combinationV2:001v
Thread-pool-1-thread-3-combinationV21:001w
Thread-pool-1-thread-3-combinationV2:001x
Thread-pool-1-thread-3-combinationV21:001y
Thread-pool-1-thread-3-combinationV2:001z
Thread-pool-1-thread-3-combinationV21:001A
Thread-pool-1-thread-3-combinationV2:001B
Thread-pool-1-thread-3-combinationV21:001C
Thread-pool-1-thread-3-combinationV2:001D
Thread-pool-1-thread-3-combinationV21:001E
Thread-pool-1-thread-3-combinationV2:001F
Thread-pool-1-thread-3-combinationV21:001G
Thread-pool-1-thread-3-combinationV2:001H
Thread-pool-1-thread-3-combinationV21:001I
Thread-pool-1-thread-3-combinationV2:001J
Thread-pool-1-thread-3-combinationV21:001K
Thread-pool-1-thread-3-combinationV2:001L
Thread-pool-1-thread-3-combinationV21:001M
Thread-pool-1-thread-3-combinationV2:001N
Thread-pool-1-thread-3-combinationV21:001O
Thread-pool-1-thread-3-combinationV2:001P
Thread-pool-1-thread-3-combinationV21:001Q
Thread-pool-1-thread-3-combinationV2:001R
Thread-pool-1-thread-3-combinationV21:001S
Thread-pool-1-thread-1-combinationV2:0012
Thread-pool-1-thread-1-combinationV21:001T
Thread-pool-1-thread-1-combinationV2:001U
Thread-pool-1-thread-1-combinationV21:001V
Thread-pool-1-thread-1-combinationV2:001W
Thread-pool-1-thread-1-combinationV21:001X
Thread-pool-1-thread-1-combinationV2:001Y
Thread-pool-1-thread-1-combinationV21:001Z
Thread-pool-1-thread-1-combinationV2:0020
Thread-pool-1-thread-1-combinationV21:0021
Thread-pool-1-thread-1-combinationV2:0022
Thread-pool-1-thread-1-combinationV21:0023
Thread-pool-1-thread-1-combinationV2:0024
Thread-pool-1-thread-1-combinationV21:0025
Thread-pool-1-thread-1-combinationV2:0026
Thread-pool-1-thread-1-combinationV21:0027
Thread-pool-1-thread-1-combinationV2:0028
Thread-pool-1-thread-1-combinationV21:0029
Thread-pool-1-thread-1-combinationV2:002a
Thread-pool-1-thread-1-combinationV21:002b
Thread-pool-1-thread-1-combinationV2:002c
Thread-pool-1-thread-1-combinationV21:002d
Thread-pool-1-thread-1-combinationV2:002e
Thread-pool-1-thread-1-combinationV21:002f
Thread-pool-1-thread-1-combinationV2:002g
Thread-pool-1-thread-1-combinationV21:002h
Thread-pool-1-thread-1-combinationV2:002i
Thread-pool-1-thread-1-combinationV21:002j
Thread-pool-1-thread-1-combinationV2:002k
Thread-pool-1-thread-1-combinationV21:002l
Thread-pool-1-thread-1-combinationV2:002m
Thread-pool-1-thread-1-combinationV21:002n
Thread-pool-1-thread-1-combinationV2:002o
Thread-pool-1-thread-1-combinationV21:002p

Process finished with exit code 0

最后小结:从 n 个不同元素中,任取 m(m≤n) 个元素并成一组(长度为x,组合时可复用单个元素),组成为不同的串码组合,则组合数C(n,m)=n^m或C(n,m)=n^x;
由52个字母与10个数字,组成的4位串码,排列组合数有C(n,m)=n^m=62^4=14776336种。

在此记录与总结,2021年 10月 20日 星期三 18:18:04 CST。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dnbug Blog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值