参数的排列组合3

场景描述:

我有个测试同事,要测试一个接口,这个接口有多个参数,而且有的参数取值有多个.

比如参数method,可以取值["a","b","c"],并且个数不确定,即method,可以取值ab,也可是是abc

如何获取排列组合的所有值呢? 
之前咱们是求排列组合的取值个数,现在要求取值 
首先我们考虑常规的情况: 
我们有n个盒子,分别放置n个元素 
第一回:我们从n个里面选择一个:有n种可能 
第二回:我们从n-1个里面选择一个:有n-1种可能 
第三回:我们从n-2个里面选择一个:有n-2种可能 
第四回:我们从n-3个里面选择一个:有n-3种可能 
…… 
最后我们只有一个可选 



 直接上代码:直接上代码:

Java代码   收藏代码
  1. /*** 
  2.     * @param base      :[a,b,c,d] 
  3.     * @param times 
  4.     * @param remaining : 剩余要选择的个数 
  5.     * @return 
  6.     */  
  7.    public static void assemble(List<String> result, StringBuffer buffer, String base[], int times, int remaining, boolean isSort) {  
  8.        if (remaining <= 1) {  
  9.            buffer.append(base[base.length - 1]);  
  10.            if(times==0||times==base.length-remaining+1){  
  11.             addElementBySort(result, buffer, isSort);  
  12.            }  
  13.              
  14.        } else {  
  15.            for (int i = 0; i < remaining; i++) {  
  16.                StringBuffer bufferTmp = new StringBuffer(buffer);  
  17.                bufferTmp.append(base[base.length - 1 - i]);  
  18.                if(times==0||times==base.length-remaining+1){  
  19.                 addElementBySort(result, bufferTmp, isSort);  
  20.                }  
  21.                assemble(result, bufferTmp, SystemHWUtil.aheadElement(base, base.length - 1 - i), times, remaining - 1, isSort);  
  22.            }  
  23.        }  
  24.   
  25.    }  

 参数说明:

参数名 含义 举例
base样本[a,b,c,d]
remaining剩余可选择的样本个数 
times组合的个数 



 

工具类完整代码:

Java代码   收藏代码
  1. package com.common.util;  
  2.   
  3. import com.string.widget.util.ValueWidget;  
  4.   
  5. import java.util.ArrayList;  
  6. import java.util.List;  
  7.   
  8. /** 
  9.  * Created by huangweii on 2016/1/23. 
  10.  */  
  11. public class AssembleUtil {  
  12.     /*** 
  13.      * @param base      :[a,b,c,d] 
  14.      * @param times 
  15.      * @param remaining : 剩余要选择的个数 
  16.      * @return 
  17.      */  
  18.     public static void assemble(List<String> result, StringBuffer buffer, String base[], int times, int remaining, boolean isSort) {  
  19.         if (remaining <= 1) {  
  20.             buffer.append(base[base.length - 1]);  
  21.             if(times==0||times==base.length-remaining+1){  
  22.                 addElementBySort(result, buffer, isSort);  
  23.             }  
  24.               
  25.         } else {  
  26.             for (int i = 0; i < remaining; i++) {  
  27.                 StringBuffer bufferTmp = new StringBuffer(buffer);  
  28.                 bufferTmp.append(base[base.length - 1 - i]);  
  29.                 if(times==0||times==base.length-remaining+1){  
  30.                     addElementBySort(result, bufferTmp, isSort);  
  31.                 }  
  32.                 assemble(result, bufferTmp, SystemHWUtil.aheadElement(base, base.length - 1 - i), times, remaining - 1, isSort);  
  33.             }  
  34.         }  
  35.   
  36.     }  
  37.   
  38.     /*** 
  39.      * @param base 
  40.      * @param times 
  41.      * @param isSort    : 是否对"acb"进行排序,<br />排序结果:"abc" 
  42.      * @return 
  43.      */  
  44.     public static List<String> assemble(String base[], int times,  boolean isSort) {  
  45. //        Set<String> result = new HashSet<String>();  
  46.         List<String> result = new ArrayList<String>();  
  47.         AssembleUtil.assemble(result, new StringBuffer(), base, times, base.length, true);  
  48.         return result;  
  49.     }  
  50.   
  51.     public static void addElementBySort(List<String> result, StringBuffer buffer, boolean isSort) {  
  52.         String str = buffer.toString();  
  53.         if (isSort) {  
  54.             str = ValueWidget.sortStr(str);  
  55.         }  
  56.         if (result.size() == 0 || (!result.contains(str))) {  
  57.             result.add(str);  
  58.         }  
  59.     }  
  60.   
  61.     /*** 
  62.      * 参数的取值个数,ab和ba算一种 
  63.      * 
  64.      * @param argCount 
  65.      * @return 
  66.      */  
  67.     public static int getAssembleSum(int argCount) {  
  68.         int sum = 0;  
  69.         for (int i = 0; i < argCount; i++) {  
  70.             int count = i + 1;//参数组合的个数  
  71.             sum += (SystemHWUtil.factorial(argCount, count) / SystemHWUtil.arrayArrange(count));  
  72.         }  
  73.         return sum;  
  74.     }  
  75.   
  76. }  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值