首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

数组交叉循环搭配的排列组合有关问题

2012-02-11 
数组交叉循环搭配的排列组合问题有多个数组.如何组成一个二维数组如:数组一:红色,蓝色,白色数组二:大,中,

数组交叉循环搭配的排列组合问题
有多个数组.如何组成一个二维数组
如:
数组一:红色,蓝色,白色
数组二:大,中,小
最终组成的数组为:{红色,大}{红色,中}{红色,小}{蓝色,大}{蓝色,中}{蓝色,小}{白色,大}{白色,中}{白色,小}

条件:一维数组的个数不确定,有可能有三个,有可能有四个等.

请教高手如何实现

[解决办法]

JScript code
var a = ["红色", "蓝色", "白色"];var b = ["大", "中", "小 "]var c = [];for(var i in a)    for(var j in b)        c.push([a[i], b[j]]);alert(c);
[解决办法]
C# code
string[] arr1 = new string[] { "红色", "蓝色", "白色" };            string[] arr2 = new string[] { "大", "中", "小","超大" };            string[][] arr3 = new string[arr1.Length*arr2.Length][];            for (int i = 0; i < arr1.Length; i++)            {                for (int j = 0; j < arr2.Length; j++)                {                    string[] arr = new string[] { arr1[i], arr2[j] };                    arr3[i + arr1.Length*j] = arr;                }            }
[解决办法]
下面代码,加上必要using,直接运行,就是你要结果。

C# code
class Program    {        static void Main(string[] args)        {            testArray();        }        private static void testArray()        {            object[] a = {"红色", "蓝色", "白色","黑色"};            object[] b = {"大", "中", "小"};            int n = a.Length*b.Length;            object[][] c = new object[n][];            for (int i = 0; i < n; i++)            {                c[i] = new object[2];                c[i][0] = a[i / b.Length];                c[i][1] = b[i % b.Length];            }            System.Console.WriteLine("a = " + Array2String(a));            System.Console.WriteLine("b = " + Array2String(b));            System.Console.WriteLine("c = " + Array2String(c));            System.Console.ReadLine();        }        private static string Array2String(object[] arr){            StringBuilder sb = new StringBuilder();            sb.Append("{");            for (int i = 0;i<arr.Length;i++)            {                object o = arr[i];                if (o is Array)                    sb.Append(Array2String((object[])o));                else                    sb.Append(o);                if (i < arr.Length - 1)                {                    sb.Append(",");                }            }            sb.Append("}");            return sb.ToString();        }}
[解决办法]
C# code
    class testArray    {        private string[] str1 = { "红", "黄", "蓝" };        private string[] str2 = { "大", "中", "小" };        private string[,] str3 = null;        public void newArray()        {            int m = str1.Length;            int n = str2.Length;            str3 = new string[m, n];            for (int i = 0; i < str1.Length; i++)            {                for (int j = 0; j < str2.Length; j++)                {                    str3[i,j] = str1[i] + str2[j];                }            }        }        public void printNewArray()        {            for (int i = 0; i < str1.Length; i++)            {                for (int j = 0; j < str2.Length; j++)                {                    System.Console.WriteLine(str3[i,j]);                }            }        }        static void Main(string[] args)        {            testArray ta = new testArray();            ta.newArray();            ta.printNewArray();        }
[解决办法]
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{


string[] arr1 = new string[] { "红色", "蓝色", "白色" };
string[] arr2 = new string[] { "大", "中", "小" };
//泛型分组,接下就要靠你遍历获取数据凑成你要的格式!!!!!
Dictionary<string, List<string>> a = new Dictionary<string, List<string>>();

foreach (string i in arr1)
{
if (a.ContainsKey(i))
{
a[i].Add(i);

}
else
{
List<string> b = new List<string>();
a.Add(i, b);

}

}



}
}
}


[解决办法]
刚才那个有点错

现在改过来

本人测试过可以实现你的要求····

我想该结贴了~~~~


using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{

//初始化
string[] arr1 = new string[] { "红色", "蓝色", "白色" };
string[] arr2 = new string[] { "大", "中", "小" };

Dictionary<string, List<string>> a = new Dictionary<string, List<string>>();

foreach (string i in arr1)
{
List<string> b = new List<string>();
a.Add(i, b);
foreach (string j in arr2)
{

a[i].Add(j);
}

}


//分配
string[] arr3 = new string[arr1.Length*arr2.Length];
 
int k=0;
foreach(string i in a.Keys)
{
foreach(string j in a[i])
{

arr3[k] = i + "," + j;

k++;
}

}
///输出显示
foreach(string m in arr3)
{
Console.WriteLine(m);
}

}

}
}

[解决办法]
C# code
        struct Matrix        {            private string[] element;            public string[] Element            {                get { return element == null ? new string[0] : element; }                set { element = value; }            }            public Matrix(string[] s)            {                element = s;            }            public Matrix Multiply(Matrix m)            {                string[] s = new string[this.element.Length * m.element.Length];                int index = 0;                for (int i = 0; i < this.element.Length; i++)                {                    for (int j = 0; j < m.element.Length; j++)                    {                        s[index] = this.element[i] + "," + m.element[j];                        index++;                    }                }                return new Matrix(s);            }        }        public static void PrintResult(params string[][] arrays)        {            Matrix m = new Matrix();            foreach (string[] array in arrays)            {                if (m.Element.Length == 0)                    m = new Matrix(array);                else                    m = m.Multiply(new Matrix(array));            }            foreach (string s in m.Element)                Console.WriteLine(s);        }        static void Main(string[] args)        {            string[] s1 = { "红色", "蓝色", "白色" };            string[] s2 = { "大", "中", "小" };            string[] s3 = { "10克", "20克", "30克" };            PrintResult(s1);            Console.WriteLine("--------------------------");            PrintResult(s1, s2);            Console.WriteLine("--------------------------");            PrintResult(s1, s2, s3);        }/*输出:红色蓝色白色--------------------------红色,大红色,中红色,小蓝色,大蓝色,中蓝色,小白色,大白色,中白色,小--------------------------红色,大,10克红色,大,20克红色,大,30克红色,中,10克红色,中,20克红色,中,30克红色,小,10克红色,小,20克红色,小,30克蓝色,大,10克蓝色,大,20克蓝色,大,30克蓝色,中,10克蓝色,中,20克蓝色,中,30克蓝色,小,10克蓝色,小,20克蓝色,小,30克白色,大,10克白色,大,20克白色,大,30克白色,中,10克白色,中,20克白色,中,30克白色,小,10克白色,小,20克白色,小,30克*/ 

热点排行