栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

生成所有可能的组合

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

生成所有可能的组合

using System;using System.Text;public static string[] GenerateCombinations(string[] Array1, int[] Array2){    if(Array1 == null) throw new ArgumentNullException("Array1");    if(Array2 == null) throw new ArgumentNullException("Array2");    if(Array1.Length != Array2.Length)        throw new ArgumentException("Must be the same size as Array1.", "Array2");    if(Array1.Length == 0)        return new string[0];    int outputSize = 1;    var current = new int[Array1.Length];    for(int i = 0; i < current.Length; ++i)    {        if(Array2[i] < 1) throw new ArgumentException("Contains invalid values.", "Array2");        if(Array1[i] == null) throw new ArgumentException("Contains null values.", "Array1");        outputSize *= Array2[i];        current[i] = 1;    }    var result = new string[outputSize];    for(int i = 0; i < outputSize; ++i)    {        var sb = new StringBuilder();        for(int j = 0; j < current.Length; ++j)        { sb.Append(Array1[j]); sb.Append(current[j].ToString()); if(j != current.Length - 1)     sb.Append(' ');        }        result[i] = sb.ToString();        int incrementIndex = current.Length - 1;        while(incrementIndex >= 0 && current[incrementIndex] == Array2[incrementIndex])        {     current[incrementIndex] = 1;     --incrementIndex;        }        if(incrementIndex >= 0) ++current[incrementIndex];    }    return result;}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/421548.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号