Code implementation of Java permutation combination algorithm

Source: Internet
Author: User

Original: Java Permutation algorithm code implementation

Source code: Http://www.zuidaima.com/share/1550463479024640.htm

Java permutation combination algorithm, the need to study children's shoes can be downloaded, the results of the operation are as follows:



Package com.zuidaima.test;/*** @author Www.zuidaima.com**/public class Pailie {public static void main (string[] args) {        Int[] ia = {1, 2, 3, 4,5,6,7,8,9,10};        int n = 4;        System.out.println ("permutation result:");    Permutation ("", IA, N);//SYSTEM.OUT.PRINTLN ("combination result:");//combination (IA, n); public static void permutation (String s, int[] ia, int n) {if (n = = 1) {for (int i = 0; i < IA.L Ength;            i++) {System.out.println (s+ia[i]);                }} else {for (int i = 0; i < ia.length; i++) {String ss = "";                SS = s+ia[i]+ "";                Create a sub-array with no element I int[] II = new INT[IA.LENGTH-1];                int index = 0;                    for (int j = 0; J < Ia.length; J + +) {if (J! = i) {ii[index++] = ia[j];            }} permutation (ss, II, N-1); }}} Public static void combination (int[] ia, int n) {combination ("", IA, N); public static void combination (String s, int[] ia, int n) {if (n = = 1) {for (int i = 0; i < IA.L Ength;            i++) {System.out.println (s+ia[i]);                }} else {for (int i = 0; i < ia.length-(n-1); i++) {String ss = "";                SS = s+ia[i]+ ",";                Establish a subarray starting from I int[] II = new INT[IA.LENGTH-I-1];                for (int j = 0; J < Ia.length-i-1; J + +) {Ii[j] = ia[i+j+1];            } combination (ss, II, N-1); }        }    }}

Code implementation of Java permutation combination algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.