zoukankan      html  css  js  c++  java
  • java实现排列组合(通俗易懂)

    个人感觉这篇文章(原文地址见文章尾)写的排列组合问题,非常的好,而且是一步一步引出排列组合问题,我也是看了这篇文章,一步一步按照这个思路来,最后会了自己的一套排列组合

    也因此在算法竞赛中,两次用到了,成功解决了问题.

    第一个问题:

      首先,先让我们来看第一个问题, 有1,2,3,4这4个数字.可以重复的在里面选4次,问能得到多少种结果.easy

      1  1  1  1

      1  1  1  2

      1  1  1  3  

      1  1  1  4

      1  1  2  1

      1  1  2  2

      .......

      4  4  4  3

      4  4  4  4

      代码实现其实也很简单,大家可以看下代码,理解一下,再自己敲一下,应该可以很快敲出来

    import java.util.Stack;
    
    public class Main {
    	
    	public static Stack<Integer> stack = new Stack<Integer>();
    	public static void main(String[] args) {
    		int shu[] = {1,2,3,4};
    		f(shu,4,0);
    	}
    	/**
    	 * 
    	 * @param shu	待选择的数组
    	 * @param targ	要选择多少个次
    	 * @param cur	当前选择的是第几次
    	 */
    	private static void f(int[] shu, int targ, int cur) {
    		// TODO Auto-generated method stub
    		if(cur == targ) {
    			System.out.println(stack);
    			return;
    		}
    		
    		for(int i=0;i<shu.length;i++) {
    			stack.add(shu[i]);
    			f(shu, targ, cur+1);
    			stack.pop();
    			
    		}
    	}
    
    }
    

      输出:

    [1, 1, 1, 1]
    [1, 1, 1, 2]
    [1, 1, 1, 3]
    [1, 1, 1, 4]
    [1, 1, 2, 1]
    [1, 1, 2, 2]
    ............
    ............
    
    [4, 4, 3, 2]
    [4, 4, 3, 3]
    [4, 4, 3, 4]
    [4, 4, 4, 1]
    [4, 4, 4, 2]
    [4, 4, 4, 3]
    [4, 4, 4, 4]
    

      得到了想要的结果,此处结果又很多种4*4*4*4 = 256种结果。

    第二个问题:

      同理,  问题来了,这时候有点排列组合的意思了 1,2,3,4排列要的到的是

    1  2  3  4
    1  2  4  3
    1  3  4  2
    1  3  2  4
    ......
    4  2  1  2
    4  3  2  1
    

     有没有发现要的到排列的情况,这里stack里的元素是1,2,3,4都不能重复

    那么我在入栈的时候加个判断,如果比如1,已经在stack里面了,就不加进去,就不会得到  1   1  1  1 ...的情况了,就得到了排列

    import java.util.Stack;
    
    public class Main {
    	
    	public static Stack<Integer> stack = new Stack<Integer>();
    	public static void main(String[] args) {
    		int shu[] = {1,2,3,4};
    		f(shu,4,0);
    	}
    	/**
    	 * 
    	 * @param shu	待选择的数组
    	 * @param targ	要选择多少个次
    	 * @param cur	当前选择的是第几次
    	 */
    	private static void f(int[] shu, int targ, int cur) {
    		// TODO Auto-generated method stub
    		if(cur == targ) {
    			System.out.println(stack);
    			return;
    		}
    		
    		for(int i=0;i<shu.length;i++) {
    			if(!stack.contains(shu[i])) {
    				stack.add(shu[i]);
    				f(shu, targ, cur+1);
    				stack.pop();
    			}
    			
    		}
    	}
    
    }
    

      输出:

    [1, 2, 3, 4]
    [1, 2, 4, 3]
    [1, 3, 2, 4]
    [1, 3, 4, 2]
    [1, 4, 2, 3]
    [1, 4, 3, 2]
    [2, 1, 3, 4]
    [2, 1, 4, 3]
    [2, 3, 1, 4]
    [2, 3, 4, 1]
    [2, 4, 1, 3]
    [2, 4, 3, 1]
    [3, 1, 2, 4]
    [3, 1, 4, 2]
    [3, 2, 1, 4]
    [3, 2, 4, 1]
    [3, 4, 1, 2]
    [3, 4, 2, 1]
    [4, 1, 2, 3]
    [4, 1, 3, 2]
    [4, 2, 1, 3]
    [4, 2, 3, 1]
    [4, 3, 1, 2]
    [4, 3, 2, 1]
    

      

    这就是想要的排列结果了..   4 * 3 * 2 * 1 = 24种结果。

      

    第三个问题:

    那么组合问题来了,在1,2,3,4,中选3个有多少种组合方式

    1 2 3
    1 2 4
    1 3 4
    2 3 4
    
    共4种
    import java.util.Stack;
    
    public class Main {
    	
    	public static Stack<Integer> stack = new Stack<Integer>();
    	public static void main(String[] args) {
    		int shu[] = {1,2,3,4};
    		
    		f(shu,3,0,0); // 从这个数组4个数中选择三个
    	}
    	
    	/**
    	 * 
    	 * @param shu  元素
    	 * @param targ  要选多少个元素
    	 * @param has	当前有多少个元素
    	 * @param cur	当前选到的下标
    	 * 
    	 * 1	2	3     //开始下标到2
    	 * 1	2	4	  //然后从3开始
    	 */
    	private static void f(int[] shu, int targ, int has, int cur) {
    		if(has == targ) {
    			System.out.println(stack);
    			return;
    		}
    		
    		for(int i=cur;i<shu.length;i++) {
    			if(!stack.contains(shu[i])) {
    				stack.add(shu[i]);
    				f(shu, targ, has+1, i);
    				stack.pop();
    			}
    		}
    		
    	}
    }
    

     输出:

    [1, 2, 3]
    [1, 2, 4]
    [1, 3, 4]
    [2, 3, 4]
    

    最后的最后给大家附带个福利,字节的内推,有需要的可以自取,第一张是应届生校园招聘的内推码,第二张是社会招聘和在校生日常实习的,欢迎积极投递哦。

    原文地址:  https://blog.csdn.net/Ring_k/article/details/79575533

  • 相关阅读:
    PHP实现简易的模板引擎
    三种实现PHP伪静态页面的方法
    转 php简单伪静态实例
    Function mysql_db_query() is deprecated 错误解决
    PHP连接MySQL数据库的三种方式(mysql、mysqli、pdo)--续
    PHP连接MySQL数据库的三种方式(mysql、mysqli、pdo)
    mysql添加DATETIME类型字段导致Invalid default value错误的问题
    ASP程序中调用Now()总显示“上午”和“下午”,如何解决?
    解决程序开发中时间格式不对造成的问题
    “隐藏已知文件类型的扩展名”选项失败问题
  • 原文地址:https://www.cnblogs.com/zzlback/p/10947064.html
Copyright © 2011-2022 走看看