全排列、排列、排列组合

  排列组合问题小总结:

       排列组合方法

           1、for循环层层枚举

           2、交换递归 

全排列

/*给出一个字符串S(可能有重复的字符),按照字典序从小到大,输出S包括的字符组成的所有排列。例如:S = "1312",
输出为:
1123
1132
1213
1231
1312
1321
2113
2131
2311
3112
3121
3211
Input
输入一个字符串S(S的长度 <= 9,且只包括0 - 9的阿拉伯数字)
Output
输出S所包含的字符组成的所有排列
Sample Input
1312
Sample Output
1123
1132
1213
1231
1312
1321
2113
2131
2311
3112
3121
3211
*/

 JAVA代码如下:

import java.util.*;

public class Main{
    static int[] num,record;
    static boolean[] vis;
    static String string;
    
    static void dfs(int step) {
        if(step==string.length()) {
            for(int i=0;i<string.length();i++)
                System.out.print(record[i]);
            System.out.println();
            return;
        }
        
        for(int i=0;i<string.length();i++) {
            if(vis[i]) continue;
            record[step] = num[i];
            vis[i] = true;
            dfs(step+1);
            vis[i] = false;
            while(i<string.length()-1&&num[i]==num[i+1]) i++;
        }
            
    }
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        num = new int[10];
        record = new int[10];
        vis = new boolean[10];
        
        string = scanner.next();
        for(int i=0;i<string.length();i++) 
            num[i]= (int)string.charAt(i)-'0';
        Arrays.sort(num, 0,string.length());
    
        dfs(0);
        
    }
}

C代码如下:

#include<bits/stdc++.h>
using namespace std;
int main(){
    char str[15];
    while(~scanf("%s",str)){
        sort(str,str+strlen(str)); 
        do{
            printf("%s\n",str);
        }while(next_permutation(str,str+strlen(str)));
    }
    return 0;
} 
/*Ray又对数字的列产生了兴趣: 
现有四张卡片,用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数。 
Input
每组数据占一行,代表四张卡片上的数字(0<=数字<=9),如果四张卡片都是0,则输入结束。 
Output
对每组卡片按从小到大的顺序输出所有能由这四张卡片组成的4位数,千位数字相同的在同一行,同一行中每个四位数间用空格分隔。 
每组输出数据间空一行,最后一组数据后面没有空行。 
Sample Input
1 2 3 4
1 1 2 3
0 1 2 3
0 0 0 0
Sample Output
1234 1243 1324 1342 1423 1432
2134 2143 2314 2341 2413 2431
3124 3142 3214 3241 3412 3421
4123 4132 4213 4231 4312 4321

1123 1132 1213 1231 1312 1321
2113 2131 2311
3112 3121 3211

1023 1032 1203 1230 1302 1320
2013 2031 2103 2130 2301 2310
3012 3021 3102 3120 3201 3210*/

排列2

     for循环递归由小到大枚举

     重复问题解决方法

            while(i<4&&(num[i]==num[i+1]))  i++; 或者 利用LIst集合判断是否已经存在

import java.util.*;

public class Main{
    static int[] num,record;
    static boolean[] vis;
    static int cnt ;
    static LinkedList<Integer> list;
    
    static void dfs(int step) {
        if(step>4) {
            int sum = 0,x = 1;
            for(int i=4;i>=1;i--) {
                sum += record[i]*x;
                x *= 10;
            }
            if(!list.contains(sum))  list.add(sum);
            return;
        }
        for(int i=1;i<=4;i++) {
            if(vis[i]||step==1&&num[i]==0) continue;
            record[step] = num[i];
            vis[i] = true;
            dfs(step+1);
            vis[i] = false;
            //while(i<4&&(num[i]==num[i+1]))  i++;
        }
    }
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        num = new int[5];
        record = new int[5];
        vis = new boolean[5];
        
        boolean flag = false;
        
        while(scanner.hasNext()) {
            list = new LinkedList<Integer>();
            
            for(int i=1;i<=4;i++)
                num[i] = scanner.nextInt();
            if(num[1]==0&&num[2]==0&&num[3]==0&&num[4]==0) break;
            
            if(flag) System.out.println();
            flag = true;
            
            dfs(1);
  
            for(int i=0;i<list.size();i++) {
                if((i+1<list.size()&&list.get(i)/1000!=list.get(i+1)/1000)||i==list.size()-1) 
                    System.out.println(list.get(i));
                else 
                    System.out.print(list.get(i)+" ");    
            }
            
            
        }
        
    }
}

排列组合:

/*有n种物品,并且知道每种物品的数量。要求从中选出m件物品的排列数。例如有两种物品A,B,并且数量都是1,从中选2件物品,则排列有"AB","BA"两种。 
Input
每组输入数据有两行,第一行是二个数n,m(1<=m,n<=10),表示物品数,第二行有n个数,分别表示这n件物品的数量。
Output
对应每组数据输出排列数。(任何运算不会超出2^31的范围)
Sample Input
2 2
1 1
Sample Output
2*/
import java.util.Scanner;

public class Main {
    static int n,m;
    static int[] num;
    static int cnt;
    static void dfs(int num1) {
        if(num1==0) {
            cnt++;
        }else {
            for(int i=1;i<=n;i++)
                if(num[i]>0) {
                    num[i]--;
                    dfs(num1-1);
                    num[i]++;
                }
        }
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        num = new int[15];
        
        while(scanner.hasNext()) {
            cnt = 0;
            n = scanner.nextInt();
            m = scanner.nextInt();
            for(int i=1;i<=n;i++)
                num[i] = scanner.nextInt();
            dfs(m);
            System.out.println(cnt);
        }
    }

}

         

猜你喜欢

转载自www.cnblogs.com/Lemon1234/p/10608190.html