问题描述
如何生成使用LINQ / C#
我有运营商数组中的如 A对面的列表值的各种组合>< = + =
我也有可以回到我的每个项目的相反值在的array.Opposite值的函数>中
是<
等on.So考虑每一个反向操作,每个价值如何产生的各种可能的组合
样品:
题声明:
`字符串[] = arrSample新的字符串[] {!=,=}; //` arrSample可与物业values.The财产的任何对象数组可以接受像
运营商值=,=,>!,<等等。
预计输出:
中的各种组合考虑反向操作者会
输出序列1:=,=
输出Sequence2:!=,=
输出序列3:= =
输出Sequence4:!=,=
这竟然是比看起来难,但下面应该表现出的主意。
有两个步骤 - 创建的每个运营商数组列表对和它的反向,然后递归重排这些结合在一起
无效DoProblem()
{
//的String [] = arrSample新的String [] {=}=!;
的String [] = arrSample新的String [] {<=!,+};
的String [] [] = arrPairs(从arrSample运算选择新的String [] {运,GetReverse(OP)})ToArray的()。
名单,LT;数组> myList中=新名单<数组>();
myList.AddRange(arrPairs);
的foreach(以置换字符串x(0,myList上))
{
Console.WriteLine(x)的;
}
}
名单,LT;字符串>置换(INT一个,列表与LT;数组> X)
{
名单,LT;字符串> RETVAL =新的List<串GT;();
如果(A == x.Count)
{
retval.Add();
返回RETVAL;
}
的foreach(以x对象Y [一个])
{$ B $
{
的foreach(在置换(一个+ 1,x)的串×2) b retval.Add(y.ToString()+,+ x2.ToString());
}
}
返回RETVAL;
}
串GetReverse(字符串OP)
{
开关(OP){
的情况下=:
返回=!;
的情况下=!:
收益=;
案<:
返回>中;
案+:
回归 - ;
默认:
返回;
}
}
注:置换功能是基于置换的数组的数组答案在这里:的ArrayList数组的?
How to generate various combinations of opposite values using LinQ/C#I have a list of operators in an array like "> < = + ="
i also have a function which can return me the opposite value of each item in an array.Opposite value of ">"
is "<"
and so on.So considering the reverse operators for each and every value how to generate various possible combinations
Sample:Problem Statement :
`string[] arrSample = new string[]{"!=","="};` //arrSample can be any object array with property values.The property can accept operator values like
!=,=,>,< etc..
Expected Output:The various combinations considering the reverse operator would be
Output Sequence1: =,!=
Output Sequence2: !=,=
Output Sequence3: = , =
Output Sequence4: !=,!=
This turned out to be harder than it looks, but the following should demonstrate the idea.
There are two steps - create an array list pairs of each operator and its reverse and then recursively permute these together.
void DoProblem()
{
//string[] arrSample = new string[] { "!=", "=" };
string[] arrSample = new string[] { "!=", "<","+" };
string[][] arrPairs = (from op in arrSample select new string[]{op, GetReverse(op)}).ToArray();
List<Array> myList = new List<Array>();
myList.AddRange(arrPairs);
foreach (string x in Permute(0, myList))
{
Console.WriteLine(x);
}
}
List<string> Permute(int a, List<Array> x)
{
List<string> retval = new List<string>();
if (a == x.Count)
{
retval.Add("");
return retval;
}
foreach (Object y in x[a])
{
foreach (string x2 in Permute(a + 1, x))
{
retval.Add(y.ToString() + "," + x2.ToString());
}
}
return retval;
}
string GetReverse(string op)
{
switch (op) {
case "=":
return "!=";
case "!=":
return "=";
case "<":
return ">";
case "+":
return "-";
default:
return "";
}
}
NOTE: The permute function is based on the permute an Array of Arrays answer here: C# Permutation of an array of arraylists?
这篇关于阵列组合阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!