使用して、これはdictionary
を使用することです解決することができる一つの方法tuple
とデータ型bool
の一致する値の複合キー。
Dictionary<Tuple<string, string>, bool>
複合キーTuple<sring,string>
は、参加者のすべての順列が含まれていますし、その適切なbool
値にそれらを一致させます。
例えば、のような値で満たさ辞書:
Dictionary<Tuple<"Judith","James">, true>
は...ジュディスはジェームズを選ぶことは有効であることを示すであろう。
参加者の可能な組み合わせごとに辞書を作成し、その値をtrue
に設定すると、プログラムの開始時に有効になります。
これはcartesian join using an array with itselfによって達成できます。
Dictionary<Tuple<string, string>, bool> dictionary = participants.SelectMany(left => participants, (left, right) => new Tuple<string, string>(left, right)).ToDictionary(item=> item, item=>true);
可能ピックのすべての順列を取得し、true
にそれらを設定した後、我々はリストにし、falseにその複合キー用の辞書値を変更する「選択することは許されない」を通過することができます。
dictionary[new Tuple<string, string>(personNotAllowing, notAllowedPerson)] = false;
あなたは次のようにループを使用して自分自身を選んでから参加者を削除することができます。
for(int abc=0;abc<participants.Length;abc++)
{
//remove clone set
Tuple<string, string> clonePair = Tuple.Create(participants[abc], participants[abc]);
dictionary.Remove(clonePair);
}
それとも単にfalseにクローンペアの値を変更することもできます。このプログラム例で
for(int abc=0;abc<participants.Length;abc++)
{
dictionary[Tuple.Create(participants[abc],participants[abc])] = false;
}
、私は参加者のstring[]
を作成し、それらが許可されていない人々のそれぞれのリストのためのstring[]
。次に私はデカルト結合を行い、参加者の配列はとなり、となります。これにより、すべての置換が行われ、初期値はtrue
ブール値になります。
私は参加者が偽に許可されていない辞書を変更し、例辞書を表示します。
その後、他のランダム参加者を選んで妥当であるかどうかをテストするランダム参加者のインスタンスを10個作成します。
参加者が別の参加者を選ぶたびに、その複合キーをチェックして、値がtrue
であるかどうかを調べます。
結果が有効な場合は、得られた参加者のすべての組み合わせがに設定され、false
に設定されます。
for(int j=0; j<participants.Length;j++)
{
//Make the partner never be able to be picked again
Tuple<string, string> currentPair2 = Tuple.Create(partner, participants[j]);
try
{
dictionary[currentPair2] = false;
}
catch
{
}
}
この概念は、コードを実行するとよくわかります。
デモ:
static void Main(string[] args)
{
//Create participants set
string[] participants = {"James","John","Tyrone","Rebecca","Tiffany","Judith"};
//Create not allowed lists
string[] jamesNotAllowedList = {"Tiffany", "Tyrone"};
string[] johnNotAllowedList = {};
string[] tyroneNotAllowedList = {};
string[] rebeccaNotAllowedList ={"James", "Tiffany"};
string[] judithNotAllowedList = {};
//Create list of not allowed lists
string[][] notAllowedLists = { jamesNotAllowedList, johnNotAllowedList, tyroneNotAllowedList, rebeccaNotAllowedList, judithNotAllowedList};
//Create dictionary<Tuple<string,string>, bool> from participants array by using cartesian join on itself
Dictionary<Tuple<string, string>, bool> dictionary = participants.SelectMany(left => participants, (left, right) => new Tuple<string, string>(left, right)).ToDictionary(item=> item, item=>true);
//Loop through each person who owns a notAllowedList
for (int list = 0; list < notAllowedLists.Length; list++)
{
//Loop through each name on the not allowed list
for (int person = 0; person<notAllowedLists[list].Length; person++)
{
string personNotAllowing = participants[list];
string notAllowedPerson = notAllowedLists[list][person];
//Change the boolean value matched to the composite key
dictionary[new Tuple<string, string>(personNotAllowing, notAllowedPerson)] = false;
Console.WriteLine(personNotAllowing + " did not allow " + notAllowedPerson);
}
}
//Then since a participant cant pick itself
for(int abc=0;abc<participants.Length;abc++)
{
//remove clone set
Tuple<string, string> clonePair = Tuple.Create(participants[abc], participants[abc]);
dictionary.Remove(clonePair);
}
//Display whats going on with this Dictionary<Tuple<string,string>, bool>
Console.WriteLine("--------Allowed?--Dictionary------------\n");
Console.WriteLine(string.Join(" \n", dictionary));
Console.WriteLine("----------------------------------------\n\n");
//Create Random Object
Random rand = new Random();
//Now that the data is organized in a dictionary..
//..Let's have random participants pick random participants
//For this demonstration lets try it 10 times
for (int i=0;i<20;i++)
{
//Create a new random participant
int rNum = rand.Next(participants.Length);
string randomParticipant = participants[rNum];
//Random participant picks a random participant
string partner = participants[rand.Next(participants.Length)];
//Create composite key for the current pair
Tuple<string, string> currentPair = Tuple.Create(partner,randomParticipant);
//Check if it's a valid choice
try
{
if (dictionary[currentPair])
{
Console.WriteLine(randomParticipant + " tries to pick " + partner);
Console.WriteLine("Valid.\n");
//add to dictionary
for(int j=0; j<participants.Length;j++)
{
//Make the partner never be able to be picked again
Tuple<string, string> currentPair2 = Tuple.Create(partner, participants[j]);
try
{
dictionary[currentPair2] = false;
}
catch
{
}
}
}
else
{
Console.WriteLine(randomParticipant + " tries to pick " + partner);
Console.WriteLine(">>>>>>>>Invalid.\n");
}
}
catch
{
//otherwise exception happens because the random participant
//And its partner participant are the same person
//You can also handle the random participant picking itself differently
//In this catch block
//Make sure the loop continues as many times as necessary
//by acting like this instance never existed
i = i - 1;
}
}
Console.ReadLine();
}
誰が誰を選ぶことができ、誰が選ぶことができないの基準は何ですか?なぜジェームズはタイロンを選ぶことができないはずですか?これに対してどのようなコードを書いたのですか?そのコードを共有してください。 –
'picks 'のリストを作成するときに、ランダムなものを選ぶ前に*除外してみてはどうでしょうか?それはいくつかの再起動を排除する必要があります –
'p.Name!= participant.Name'と'!Utilities.IsInList(p.Name、pickedParticipantNames) 'で何をやっているのでしょうか? 'Utilities.IsInList(string ref、IEnumerableソース)'は基本的に 'source'をチェックします。' ref'はその内部のどこかに存在し、ブール値のデータを返します。 –
Nii