假設會員資料輸入: {"BETTY F M A A C C", "TOM M F A D C A", "SUE F M D D D D", "ELLEN F M A A C A", "JOE M F A A C A", "ED M F A D D A", "SALLY F M C D A B", "MARGE F M A A C C"}
using System; using System; using System.Collections.Generic; using System.Text;
namespaceMatchMaker { classMatchMaker { publicstring[] getBestMatches(string[] members, string currentUser, int sf) { Dictionary<string, Member> memberDic = ParseMembers(members); Dictionary<string, int> scoreDic = new Dictionary<string, int>(); Member currentMember = memberDic[currentUser];
List<string> result = new List<string>(); foreach (Member member in memberDic.Values) { if (member.Name == currentUser || currentMember.TargetGender != member.Gender) continue;
int score = 0; for (int i = 0; i < currentMember.Answers.Length; ++i) if (member.Answers[i] == currentMember.Answers[i]) ++score;
if (score < sf) continue;
int index = 0; while (index < result.Count) { if (scoreDic[result[index]] < score) break; ++index; } result.Insert(index, member.Name); scoreDic[member.Name] = score; }
return result.ToArray(); }
private Dictionary<string, Member> ParseMembers(string[] members) { Dictionary<string, Member> dic = new Dictionary<string, Member>(); foreach (string line in members) { string[] parts = line.Split(newchar[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); Member member = new Member(); member.Name = parts[0]; member.Gender = parts[1]; member.TargetGender = parts[2]; member.Answers = newstring[parts.Length - 3];
for (int i = 3, j = 0; i < parts.Length; ++i, ++j) member.Answers[j] = parts[i];