我有<?php if(is_user_logged_in()){?>
// your logout and other button code
<?php }else{?>
// your login button code
<?php } ?>
,其中课程List<TeamName>
包含TeamName
所以我有这样的事情:
List<MembersName>
...
List<TeamName> teamNameList //each item of "tnl" has public member "memberNameList"
现在我想要从每个团队中尽可能组合MemberName。
e.g。我有3个团队(class TeamName //each team has different member
{
public TName
public List<MemberName> memberNameLis;
}
class MemberName
{
public MName;
}
),他们的成员数量为 -
T1 = 3名成员,T2 = 2名成员,T3 = 1名成员,
所以现在我需要一些东西 -
T1M1 T2M1 T3M1
T1M1 T2M2 T3M1
T1M2 T2M1 T3M1
T1M2 T2M2 T3M1
T1M3 T2M1 T3M1
T1M3 T2M2 T3M1
T1, T2, T3
如何在Code中执行此操作? 我只是一个初学者,所以请原谅我没有以干净的方式提问。 请帮助:)
答案 0 :(得分:1)
假设您的团队列表为 -
List<TeamName> tnl;
...
...
...
// now you can call like -
IEnumerable<IEnumerable<MemberName>> allPossible = CartesianProduct(tnl.Select(c => c.mnl));
...
...
笛卡尔积方法将是相同的
IEnumerable<IEnumerable<T>> CartesianProduct<T>(IEnumerable<IEnumerable<T>> sequences)
{
IEnumerable<IEnumerable<T>> emptyProduct = new[] { Enumerable.Empty<T>() };
return sequences.Aggregate(
emptyProduct,
(accumulator, sequence) =>
from accseq in accumulator
from item in sequence
select accseq.Concat(new[] { item })
);
}
尝试一下,输入后会给你所需的结果。
如果你要循环 -
foreach (var item in r4)
{
foreach (var mn in item)
{
Console.Write("\t"+mn.Name);
}
Console.WriteLine();
}
你会得到答案 -
T1M1 T2M1 T3M1
T1M1 T2M2 T3M1
T1M2 T2M1 T3M1
T1M2 T2M2 T3M1
T1M3 T2M1 T3M1
T1M3 T2M2 T3M1
我测试了它。
答案 1 :(得分:0)
这是解决方案
List<TeamName> teamNameList = new List<TeamName>();
teamNameList.Add(new TeamName() { TName = "T1",
memberNameList = new List<MemberName> {
new MemberName { MName = "M1" },
new MemberName { MName = "M2" },
new MemberName { MName = "M3" } } });
teamNameList.Add(new TeamName() { TName = "T2",
memberNameList = new List<MemberName> {
new MemberName { MName = "M1" },
new MemberName { MName = "M2" } } });
teamNameList.Add(new TeamName() { TName = "T3",
memberNameList = new List<MemberName> { new MemberName { MName = "M1" } } });
List<List<MemberName>> combi = new List<List<MemberName>>();
//indexes keep index of each member for each team
List<int> indexes = new List<int>(teamNameList.Count);
//initialize them to the first members
for (int i = 0; i < teamNameList.Count; i++)
indexes.Add(0);
//We will enumerate all combinations in this loop
while (true)
{
//Create a new entry for combi
List<MemberName> members = new List<MemberName>();
//Select respective member based on the current index value for each team
for (int i = 0; i < teamNameList.Count; i++)
members.Add(new MemberName() { MName = teamNameList[i].TName + teamNameList[i].memberNameList[indexes[i]].MName });
combi.Add(members);
//Increment indexes and go to the next combination
for (int i = indexes.Count - 1; i >= 0; i--)
{
indexes[i]++;
//If we enumerated all members of the current team - start from the beginning of that members
//Else we should proceed with the next member
if (indexes[i] == teamNameList[i].memberNameList.Count)
indexes[i] = 0;
else
break;
}
//If we enumerated all combinations than all indexes will become zero because of the previous loop
if (indexes.Sum() == 0)
break;
}