这俩天讨论挺火的一个题目。
也看了高手解答,实现的代码很短(PS:个人觉得,该换行还是得换行)。
自己这天也闲着,写了下C#版实现,小弟不才用了100多行,不足之处多多指教。
扩展方法:
public static class Extension { public static bool IsIntegerThanZero(this int value) { return Regex.IsMatch(Convert.ToString(value), @"^[1-9]{1}[\d]*$"); } public static string PartCatchPhrase(this int value, int key, string catchPhrase) { string sResult = ""; if (string.IsNullOrWhiteSpace(catchPhrase)) { throw new ArgumentNullException("catchPhrase"); } if (!key.IsIntegerThanZero()) { throw new ArgumentNullException("key"); } Regex.Replace(Convert.ToString(value), string.Format(@"(?<{1}>{0})", key,"GROUP"+key.ToString()), m => { if (sResult == "") { sResult = !string.IsNullOrEmpty(m.Groups["GROUP" + key.ToString()].Value) ? catchPhrase : sResult; } return ""; }); return sResult; } public static string DivisibleCatchPhrase(this int value, int key, string catchPhrase,ref string result) { if (string.IsNullOrWhiteSpace(catchPhrase)) { throw new ArgumentNullException("catchPhrase"); } if (!key.IsIntegerThanZero()) { throw new ArgumentNullException("key"); } result += (value % key == 0 ? catchPhrase : ""); return result; } }
口号定义
public class CatchPhrase { public CatchPhrase() { } public CatchPhrase(int index, string catchWord) { this.Index = index; this.CatchWord = catchWord; } public int Index { get; set; } public string CatchWord { get; set; } public override string ToString() { return this.CatchWord; } public static explicit operator CatchPhrase(string value) { int nIndex = -1; string sWord = ""; if(Regex.IsMatch(value,@"^\d+")) { string sNumber = Regex.Match(value,@"^\d+").Value; int.TryParse(sNumber,out nIndex); sWord = value.Substring(sNumber.Length+1); return new CatchPhrase(nIndex, sWord); } return null; } }
再费些代码
public class CatchPhraseCollection : KeyedCollection{ protected override int GetKeyForItem(CatchPhrase item) { return item.Index; } public void Add(string item) { CatchPhrase cp = (CatchPhrase)item; if (cp != null) { this.Add(cp); } } }
演示代码
static void Main(string[] args) { CatchPhraseCollection cpc = new CatchPhraseCollection(); Console.Write("Please input the first special number and CatchWord(number,CatchWord): "); cpc.Add(Console.ReadLine()); Console.Write("Please input the second special number and CatchWord(number,CatchWord): "); cpc.Add(Console.ReadLine()); Console.Write("Please input the third special number and CatchWord(number,CatchWord): "); cpc.Add(Console.ReadLine()); int nIndex; Console.Write("Please input an integer number larger than 0: "); while (int.TryParse(Console.ReadLine(), out nIndex)) { if (nIndex.IsIntegerThanZero()) { string sOutput = ""; foreach(CatchPhrase item in cpc) { if(sOutput == "") { sOutput = nIndex.PartCatchPhrase(item.Index,item.CatchWord); } else break; } if (sOutput == "") { foreach (CatchPhrase item in cpc) { nIndex.DivisibleCatchPhrase(item.Index, item.CatchWord, ref sOutput); } } if (sOutput != "") Console.WriteLine(string.Format(" -> {0}",sOutput)); else Console.WriteLine(string.Format(" -> {0}",nIndex)); } // Console.Write("Please input an integer number larger than 0: "); } }
为啥弄这么啰嗦,主要是考虑,式样变更的问题,以后基本不用改动。