博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#版拉钩题目答案FizzBuzzWhizz
阅读量:6934 次
发布时间:2019-06-27

本文共 4717 字,大约阅读时间需要 15 分钟。

  hot3.png

这俩天讨论挺火的一个题目。

也看了高手解答,实现的代码很短(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: ");            }        }

143921_CrpI_242740.png

为啥弄这么啰嗦,主要是考虑,式样变更的问题,以后基本不用改动。

转载于:https://my.oschina.net/jickie/blog/262626

你可能感兴趣的文章
hexo
查看>>
云场景实践研究第85期:墨迹天气
查看>>
一个SAP开发人员的2017总结
查看>>
7216:Minecraft
查看>>
上接稳扎稳打Silverlight(20) - 2.0通信之WebClient, 以字符串的形式上传/下载数据
查看>>
perl连接mysql的例子
查看>>
windows server 2008虚拟化技术一览
查看>>
webpack2 实践
查看>>
Linux系统日志介绍分析
查看>>
Linux下Tomcat的启动、关闭、杀死进程
查看>>
FTP服务器的防火墙通用设置规则
查看>>
简单记事本及目录树形图的Java实现
查看>>
android 在使用ViewAnimationUtils.createCircularReveal()无法兼容低版本的情况下,另行实现圆形scale动画...
查看>>
Application Virtualization 4.5 部署之(一)
查看>>
获取ip地址解析归属地
查看>>
启用日志调试Kerberos登录验证问题
查看>>
saltstack二次开发构建自己的api
查看>>
动手打造自己强大的右键菜单
查看>>
探测调试器
查看>>
图案研究2--九格定义
查看>>